1

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"
    }
  ]
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
sHaShAnk K
  • 27
  • 3
  • 1
    You forgot to explain what's wrong with the current code. Any errors? Also you did not provide the file content. – Marcin Jan 20 '23 at 10:18
  • @Marcin - Hello, I do not have any error with the above code. I am able to create different schema, but with only one parameter i.e. Name. Basically, now I want to include Name and Schema file parameters combined together and pass it dynamically thorugh .tfvars file. I am stuck at how to pass file parameters inside the for_each block and what resource and variables changes need to be made. Kindly suggest. – sHaShAnk K Jan 20 '23 at 10:28

1 Answers1

1

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
  }
}
Marcin
  • 215,873
  • 14
  • 235
  • 294