Traffine I/O

日本語

2022-12-09

TerraformによるSnowflakeのリソース管理

TerraformによるSnowflakeのリソース管理

SnowflakeのリソースはTerraformで管理することができます。管理できる具体的なリソースは以下のとおりです。

  • データベース
  • スキーマ
  • ウェアハウス
  • ステージ
  • ロール
  • ユーザー
  • Snowpipe

ファイルからSQLを読み込んでタスクを作成

その他Terraformが標準で用意していないリソースはSQLファイルを読み込んで対応することができます。

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からSQL文に変数を与えるtemplatefile関数もあります。

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

CICDの実行

GitHub ActionsなどでTerraformの実行を行うことで、Snowflakeのリソースの変更を自動化することができます。例えば以下のような自動化をすることができます。

  • Pull Requestを作成したときにterraform planを実行し、その結果をPull Requestにコメント
  • Pull Requestをマージしたときにterraform applyを実行

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
https://zenn.dev/yamnaku/articles/6f1d45640125b2
https://zenn.dev/rinchsan/articles/de981e561eb36ebfab70

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!