In this post, I want to show you how you can provision Snowflake components with Terraform in our dbt projects.
Terraform is an open-source Infrastructure as a code (IaC) tool that is used to manage the cloud services with a declarative, version controlled configuration format. It is cloud-agnostic so it works on AWS, GCP, Azure and many other cloud providers. Its benefits include that you can write maintainable, reusable, modular pieces of code. So it fits really well with the mindset that makes so many of us love using dbt.
First you need to install the Terraform CLI. To do this, follow the instructions here: install-cli
Note that this varies from OS to OS, on MacOS, for example, I had to run these commands:
$ brew tap hashicorp/tap
$ brew install hashicorp/tap/terraform
After a successful installation, we need to initialize the Terraform project. To do this we need a new directory, then create the main.tf file with the following content:
main.tf call - modules, locals and data-sources to create all resources
variable.tf - contains declarations of variables used in main.tf
secret.tfvars - sensitive variables, such as passwords
Without the use of Terraform on projects, Snowflake dependencies need to be created manually. For example, in a new project, one thing that you definitely need in a Snowflake tenant is at least one warehouse:
There is nothing fundamentally wrong with this approach, but there is no clear way to manage this kind of code in your dbt project. You can save it to a new repo or to one of the corners of your project, but there is no trivial solution to manage these external dependencies, and to make it parametrizable, reusable. Managing these external resources can cause technical debt.
The first step is to set up the required providers. This is necessary because the technical user on whose behalf we run the commands needs different roles for different operations.(This user must be at least SYS, SECURITY admin within Snowflake.)
Add the following to the contents of main.tf:
provider "snowflake" {
username = <snowflake-username>
password = <snowflake-password>
account = <snowflake-account-name>
region = <snowflake-region-name>
alias = "sys_admin"
role = "SYSADMIN"
}
(of course, sensitive information will not be stored in main.tf later)
To create a Snowflake warehouse in Terraform is as simple as this:
resource snowflake_warehouse w {
name = "test"
comment = "foo"
warehouse_size = "small"
}
It’s looks something like this:
We successfully provisioned a Snowflake warehouse from Terraform.
Now we are going to make the process more sophisticated. We will derive each value into a variable, for example, if you need more than one warehouse (which you probably do).
With the following DRY approach, it’s easy to automate the creation of warehouses in a configurable and reusable way. You can simply add something this to your variable.tf:
So, as described above, with Terraform we can dynamically generate the necessary resources to provision Snowflake components. I think this is a good way to demonstrate the power of Terraform.
Here is high level view of our final bootstrapping project:
In this post, I tried to show you how to provision Snowflake infrastructure with Terraform. Development of the dbt Cloud Terraform provider started 3 months ago ( Oct 3, 2021), which will allow us to put dbt Cloud resources under version control in the future as well.
Democratizing multi-agent systems by creating user-centric digital products around them requires an entirely new outlook on the development process. Find out how to tackle new challenges in frontend, backend, and UX/UI design!
Traditional data warehouses are hitting their limits, but complete overhauls aren't always the answer. Through the lens of our recent case study, discover how incremental Data Vault implementation can deliver dramatic improvements without disrupting operations. From 98% faster processing to flexible business rules, learn practical approaches to modernization that you can start implementing today.
We want to work with you.
Hiflylabs is your partner in building your future. Share your ideas and let's work together!