Home > Software engineering >  Terraform for_each iteration with the file function
Terraform for_each iteration with the file function

Time:01-21

My requirement is to create a dynamic resource Confluent Schema. Below is the schema.tf file. Basically, need to include map type object and will be creating different Schema by passing name and file attributes. What changes to be done on below highlighted "schema" file parameter so that it can be included in the for_each block?

resource "confluent_schema" "sample_avro_schema" {
  schema_registry_cluster {
    id = confluent_schema_registry_cluster.essentials.id
  }
  rest_endpoint = confluent_schema_registry_cluster.essentials.rest_endpoint
  for_each      = toset(var.subject_name_avro)
  subject_name  = each.key
  format        = "AVRO"
  **schema        = file("modules/confluent_kafka_cluster_dedicated/schemas/sample_schema_avro.avsc")**
  credentials {
    key    = confluent_api_key.env-manager-schema-registry-api-key.id
    secret = confluent_api_key.env-manager-schema-registry-api-key.secret
  }
}

Variable declaration as below: variable.tf file

variable "subject_name_avro" {
  description = "AVRO Schema Name"
  type        = list(string)
  default     = ["avro-topic-value"]
}

And I am running this execution using .tfvars file:

subject_name_avro          = ["avro-topic-1-value"]

My requirement is to include below changes in .tfvars file. Kindly suggest what resource and variable level changes to be done to include schema file parameter dynamically.

subject_name_avro = [  
    {
        subject_name_avro  = "avro-topic-1-value"
        schema      = file("modules/confluent_kafka_cluster_dedicated/schemas/sample_schema_avro1.avsc")
    },  
    {
        subject_name_avro  = "avro-topic-2-value"
        schema      = file("modules/confluent_kafka_cluster_dedicated/schemas/sample_schema_avro2.avsc")
    },   
]

Sample file content "sample_schema_avro.avsc"

{
  "type": "record",
  "namespace": "io.confluent.developer.avro",
  "name": "Purchase",
  "fields": [
    {
      "name": "item",
      "type": "string"
    },
    {
      "name": "amount",
      "type": "double"
    },
    {
      "name": "customer_id",
      "type": "string"
    }
  ]
}

CodePudding user response:

You can't use file in a variabiles. You can use only path in your case:

subject_name_avro = [  
    {
        subject_name_avro  = "avro-topic-1-value"
        schema      = "./modules/confluent_kafka_cluster_dedicated/schemas/sample_schema_avro1.avsc"
    },  
    {
        subject_name_avro  = "avro-topic-2-value"
        schema      = "./modules/confluent_kafka_cluster_dedicated/schemas/sample_schema_avro2.avsc"
    },   
]

To iterate over this you can use count or for_each. With for_each it would be:

resource "confluent_schema" "sample_avro_schema" {
  
  for_each      = {for idx, val in var.subject_name_avro: idx => val}
 
  schema_registry_cluster {
    id = confluent_schema_registry_cluster.essentials.id
  }
  rest_endpoint = confluent_schema_registry_cluster.essentials.rest_endpoint
  subject_name  = each.value.subject_name_avro
  format        = "AVRO"
  **schema        = file(each.value.schema)
  credentials {
    key    = confluent_api_key.env-manager-schema-registry-api-key.id
    secret = confluent_api_key.env-manager-schema-registry-api-key.secret
  }
}
  • Related