2022-12-09

Snowflake Resource Management with Terraform

Snowflake Resource Management with Terraform

Snowflake resources can be managed using Terraform. The specific resources that can be managed are as follows:

  • Database
  • Schema
  • Warehouse
  • Stage
  • Role
  • User
  • Snowpipe

Creating Tasks by Reading SQL from Files

For resources that Terraform does not provide out-of-the-box, you can read SQL files to handle them.

task.tf
resource "snowflake_task" "serverless_task" {
  comment = "my serverless task"

  database = "EXIST_DB"
  schema   = "EXIST_SCHEMA"

  name          = "SERVERLESS_TASK"
  schedule      = "10 MINUTE"
  sql_statement = file("./task.sql")

  enabled = false
}
task.sql
select 1
;

https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/task

Terraform also provides the templatefile function to pass variables to SQL statements from Terraform.

https://developer.hashicorp.com/terraform/language/functions/templatefile

Executing CI/CD

By executing Terraform in tools like GitHub Actions, you can automate the changes to Snowflake resources. For example, you can automate the following:

  • Running terraform plan when creating a Pull Request and commenting the result in the Pull Request.
  • Running terraform apply when merging a Pull Request.

https://developer.hashicorp.com/terraform/tutorials/automation/github-actions

References

https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs
https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/pipe
https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/task
https://developer.hashicorp.com/terraform/language/functions/templatefile
https://developer.hashicorp.com/terraform/tutorials/automation/github-actions

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!