Kafka 在terraform版本“>= 0.15”的terraform中获取错误“关键字“optional”不是有效的类型构造函数“

mm9b1k5b  于 2022-12-17  发布在  Apache
关注(0)|答案(1)|浏览(119)

在当前的terraform管道中,我以列表的形式传递主题

locals {
  test_topics = [
    {
      name                      = "topic1"
      is_public                 = true
      version                   = 1
      is_cleanup_policy_compact = true
      max_message_bytes         = "-1"
      partition_count = 3
    },
    {
      name                      = "topic2"
      is_public                 = true
      version                   = 1
      is_cleanup_policy_compact = true
      max_message_bytes         = "-1"
      partition_count = 4
    },
    {
      name                      = "topic3"
      is_public                 = true
      version                   = 1
      is_cleanup_policy_compact = true
      max_message_bytes         = "-1"
      partition_count = 5     
    },
    {
      name                      = "topic4"
      is_public                 = true
      version                   = 1
      is_cleanup_policy_compact = true
      max_message_bytes         = "-1"
    },
    {
      name                      = "topic5"
      is_public                 = true
      version                   = 1
      is_cleanup_policy_compact = true
      max_message_bytes         = "-1"
      partition_count = 5
    }
  ]
}

# example create topic it automatically assigns READ WRITE access to the service account and READ access to all PUBLIC topics
module "test_topics" {
  source = "../kafka_topic"
  topics = "${local.test_topics}"
  environment     = var.environment
  data_domain     = var.data_domain
  service_account = var.service_account
}

并在子模块中声明变量,如下所示

variable "topics" {
  type        = list(object({
    name = string
    is_public = bool
    is_cleanup_policy_compact = bool
    version = number
    max_message_bytes = number
    partition_count = number
}))
  description = "list of topics with their configuration"
  default = null
}

在child www.example.com中main.tf我们使用以下代码创建主题

resource "kafka_topic" "topic" {
  count              = length(var.topics)
  name               = "${lookup(var.topics[count.index], "is_public") ? "public" :"private"}_${var.environment}_${var.data_domain}_${lookup(var.topics[count.index], "name")}_${lookup(var.topics[count.index], "version")}"
  partitions         = lookup(var.topics[count.index], "partition_count") == null ? 6 : "${lookup(var.topics[count.index], "partition_count")}"
  replication_factor = 3
  config = {
    "cleanup.policy" = lookup(var.topics[count.index], "is_cleanup_policy_compact") ? "compact" : "delete"
    "max.message.bytes" =  lookup(var.topics[count.index], "max_message_bytes") != -1 ? "${lookup(var.topics[count.index], "max_message_bytes")}" : 1000012
  }
}

但当运行地形计划时,我遇到以下异常
属性“partition_count”是必需的。
注意:我在www.example.com中声明变量时也使用了partition_count = optional(number)variable.tf(以将该属性保留为可选字段),但得到了以下错误
关键字“optional”不是有效的类型构造函数
因为这可能是由于我目前使用的terraform版本是“〉= 0.12”,但当我尝试使用“〉= 0.15”版本时,得到了相同的错误“关键字“可选”不是有效的类型构造函数“错误。有什么方法可以解决这个问题吗?

fv2wmkja

fv2wmkja1#

尝试添加以下内容:

terraform {
  experiments = [module_variable_optional_attrs]
}

相关问题