繁体   English   中英

Terraform for_each 文件迭代 function

[英]Terraform for_each iteration with the file function

我的要求是创建一个动态资源 Confluent Schema。 下面是schema.tf文件。 基本上,需要包含 map 类型 object 并将通过传递名称和文件属性创建不同的架构。 对下面突出显示的“schema”文件参数进行哪些更改才能将其包含在 for_each 块中?

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.tf文件

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

我正在使用 .tfvars 文件运行此执行:

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

我的要求是在 .tfvars 文件中包含以下更改。 请建议要进行哪些资源和变量级别更改以动态包含架构文件参数

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_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"
    }
  ]
}

您不能在变量中使用file 您只能在您的情况下使用路径:

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"
    },   
]

要对此进行迭代,您可以使用countfor_each 对于for_each它将是:

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
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM