Skip to content

replace null_resource for executing snowsql with the terraform-provider-snowsql #74

@aidanmelen

Description

@aidanmelen

I just recently released terraform-provider-snowsql. This project seemed like it could benefit from this new provider!

For example, the snowpipe-base sub-module uses on the null_resource to execute snowsql statements.

resource "null_resource" "create_snowflake_table" {

  provisioner "local-exec" {
    command = <<EOT
{
      snowsql --accountname $SNOWFLAKE_ACCOUNT.$SNOWFLAKE_REGION --username $SNOWFLAKE_USER --query 'CREATE TABLE IF NOT EXISTS ${var.database}.${var.schema}.${local.formatted_s3_path} ${local.formatted_rows}' || \
      (echo 'Make sure $SNOWFLAKE_ACCOUNT, $SNOWFLAKE_REGION and $SNOWFLAKE_USER env vars are set and the snowsql tool is on the path'; exit 1;)
}
EOT
  }
}

which can be declare with:

terraform {
  required_version = ">= 0.13.0"

  required_providers {
    snowflake = {
      source  = "chanzuckerberg/snowflake"
      version = ">= v0.25.18"
    }
    snowsql = {
      source  = "aidanmelen/snowsql"
      version = ">= 0.1.0"
    }
    random = ">= 2.1"
  }
}

# both providers use the same code to authenticate with snowflake
provider "snowflake" {}
provider "snowsql" {}

# ...

resource "snowsql_exec" "dcl" {
  name = local.name

  create {
    statements = "CREATE TABLE IF NOT EXISTS ${var.database}.${var.schema}.${local.formatted_s3_path} ${local.formatted_rows}${snowflake_role.role.name};"
  }

  delete {
    statements = "DROP TABLE IF EXISTS ${var.database}.${var.schema}.${local.formatted_s3_path} ${local.formatted_rows}"
  }

  delete_on_create = true
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions