import os
import snowflake.connector
from titan.blueprint import Blueprint, print_plan
from titan.resources import Grant, Role, Warehouse
# Configure resources by instantiating Python objects.
role = Role(name="transformer")
warehouse = Warehouse(
name="transforming",
warehouse_size="large",
auto_suspend=60,
)
usage_grant = Grant(priv="usage", to=role, on=warehouse)
# Titan compares your config to a Snowflake account. Create a Snowflake
# connection to allow Titan to connect to your account.
connection_params = {
"account": os.environ["SNOWFLAKE_ACCOUNT"],
"user": os.environ["SNOWFLAKE_USER"],
"password": os.environ["SNOWFLAKE_PASSWORD"],
"role": "SYSADMIN",
}
session = snowflake.connector.connect(**connection_params)
# Create a Blueprint and pass your resources into it. A Blueprint helps you
# validate and deploy a set of resources.
bp = Blueprint(resources=[
role,
warehouse,
usage_grant,
])
# Blueprint works like Terraform. Calling plan(...) will compare your config
# to the state of your Snowflake account and return a list of changes.
plan = bp.plan(session)
print_plan(plan) # =>
"""
» titan core
» Plan: 4 to add, 0 to change, 0 to destroy.
+ urn::ABCD123:warehouse/transforming {
+ name = "transforming"
+ owner = "SYSADMIN"
+ warehouse_type = "STANDARD"
+ warehouse_size = "LARGE"
...
}
+ urn::ABCD123:role/transformer {
+ name = "transformer"
+ owner = "USERADMIN"
+ tags = None
+ comment = None
}
+ urn::ABCD123:grant/TRANSFORMER?priv=USAGE&on=warehouse/TRANSFORMING {
+ priv = "USAGE"
+ on = "transforming"
+ on_type = "WAREHOUSE"
+ to = TRANSFORMER
...
}
"""
# Calling apply(...) will convert your plan into the right set of SQL commands
# and run them against your Snowflake account.
bp.apply(session, plan) # =>
"""
[TITAN_USER:SYSADMIN] > USE SECONDARY ROLES ALL
[TITAN_USER:SYSADMIN] > CREATE WAREHOUSE TRANSFORMING warehouse_type = STANDARD ...
[TITAN_USER:SYSADMIN] > USE ROLE USERADMIN
[TITAN_USER:USERADMIN] > CREATE ROLE TRANSFORMER
[TITAN_USER:USERADMIN] > USE ROLE SYSADMIN
[TITAN_USER:SYSADMIN] > GRANT USAGE ON WAREHOUSE transforming TO TRANSFORMER
"""
You can use the CLI to generate a plan, apply a plan, or export resources. To use the CLI, install the Python package and call titan from the command line.
The CLI allows you to plan and apply a Titan Core YAML config. You can specify a single input file or a directory of configs.
In addition to plan and apply, the CLI also allows you to export resources. This makes it easy to generate a config for an existing Snowflake environment.
To connect with Snowflake, the CLI uses environment variables. These environment variables are supported:
SNOWFLAKE_ACCOUNT
SNOWFLAKE_USER
SNOWFLAKE_PASSWORD
SNOWFLAKE_DATABASE
SNOWFLAKE_SCHEMA
SNOWFLAKE_ROLE
SNOWFLAKE_WAREHOUSE
SNOWFLAKE_MFA_PASSCODE
SNOWFLAKE_AUTHENTICATOR
CLI Example
Show the help message
titan--help# Usage: titan [OPTIONS] COMMAND [ARGS]...# # titan core helps you manage your Snowflake environment.# # Options:# --help Show this message and exit.# # Commands:# apply Apply a resource config to a Snowflake account# connect Test the connection to Snowflake# export Generate a resource config for existing Snowflake resources# plan Compare a resource config to the current state of Snowflake
Apply a resource config to Snowflake
# Create a resource config filecat<<EOF>titan.ymlroles: - name: transformerwarehouses: - name: transforming warehouse_size: LARGE auto_suspend: 60grants: - to_role: transformer priv: usage on_warehouse: transformingEOF# Set connection variablesexport SNOWFLAKE_ACCOUNT="my-account"export SNOWFLAKE_USER="my-user"export SNOWFLAKE_PASSWORD="my-password"# Generate a plantitanplan--configtitan.yml# Apply the configtitanapply--configtitan.yml
The Titan Core Python package installs the CLI script titan. You can alternatively use Python CLI module syntax if you need fine-grained control over the Python environment.
python-mtitanplan--configtitan.yml
Using the GitHub Action
The Titan Core GitHub Action allows you to automate the deployment of Snowflake resources using a git-based workflow.