Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

snowflake terraform create multiple table in one tf files

I am trying to create multiple table through tf in snowflake.
Below are the sample code.

resource "snowflake_table" "table" {
  database            = "AMAYA"
  schema              = "public"
  name                = "info"
  comment             = "A table."

  column {
    name     = "id"
    type     = "int"
    nullable = true

    default {
      sequence = snowflake_sequence.sequence.fully_qualified_name
    }
  }

  column {
    name     = "identity"
    type     = "NUMBER(38,0)"
    nullable = true

    identity {
      start_num = 1
      step_num  = 3
    }
  }

resource "snowflake_table" "table" {
  database            = "AMAYA"
  schema              = "public"
  name                = "arch_info"
  comment             = "A table."

  column {
    name     = "id"
    type     = "int"
    nullable = true

    default {
      sequence = snowflake_sequence.sequence.fully_qualified_name
    }
  }

  column {
    name     = "identity"
    type     = "NUMBER(38,0)"
    nullable = true

    identity {
      start_num = 1
      step_num  = 3
    }
  }
}

When I run this script I get the error.

 A snowflake_procedure resource named "table" was already declared at str.tf:16,1-38. Resource names must be unique per type in each module.

The only solution I have tried and worked is to create different files for different table. however I have 100 of tables to create, and was wondering if there is simpler way of putting all in one file and run the script

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can’t use the same name for a resource more than once, like tablebelow:

resource "snowflake_table" "table" {

Use different names:

resource "snowflake_table" "table_1" {

You should look into for_each and dynamic functions when needing to create lots of the same resource with different parameters:

Terraform for_each

Terraform dynamic

With those, you can create complex maps that are defined on input and automatically create the required amount of resources, something like below (just an example with a couple of parameters):

locals {

  snowflake_tables = {
    
    info = {
      database = "AMAYA"
      ...

      columns = {
        
        identity = {
          type     = "NUMBER(38,0)"
          nullable = true
          ...
        }
      }
    }
  }
}

resource "snowflake_table" "table" {
  
  for_each = local.snowflake_tables
  
  name     = each.key            # info
  database = each.value.database # AMAYA
  ...

  dynamic "column" {
    for_each = each.value.columns
    content {
      name     = setting.key
      type     = setting.value["setting"]
      nullable = setting.value["type"]
      ...
    }
  }
}

With this technique, all you do is add more objects to the map for tables and columns. I’ve set the example in locals but you could have this as a variable input instead in a .tfvars file etc.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading