【问题标题】:Unable to run ForEach for list in terraform无法在 terraform 中为列表运行 ForEach
【发布时间】:2023-04-02 03:30:01
【问题描述】:

我正在尝试创建 Servicebus 主题和订阅,并在引用此 link 并更新了一些内容之后,我尝试为每个主题创建运行,它抛出了错误

│给定的“for_each”参数值不合适:“for_each” 参数必须是映射或字符串集,并且您
gs,你│提供了一个元组类型的值。

这是我正在尝试的

variable "topics" {
  type        = any
  default     = [
    {
      name = "testtopic"
      subscriptions = [
        {
          name = "testsubscription"
          rules = [{
            name = "SucceededRule",
            sql_filter = "Status = 'Succeeded'"
          }]
        },
        {
          name = "readysubscription"
          rules = [{
            name = "ReadyRule",
            sql_filter = "Status = 'Ready'"
          }]
        }
      ]
    }
  ]
  description = "List of topics."
}

locals {
  topics = [
    for topic in var.topics : merge({
      name                       = ""
      status                     = "Active"
      auto_delete_on_idle        = null
      default_message_ttl        = null
      enable_batched_operations  = null
      enable_express             = null
      enable_partitioning        = null
      max_size                   = null
      enable_duplicate_detection = null
      enable_ordering            = null
      authorization_rules        = []
      subscriptions              = []

      duplicate_detection_history_time_window = null
    }, topic)
  ]}

维护

resource "azurerm_servicebus_topic" "topic" {
  for_each = toset(local.topics)

  name                = each.value.name
  resource_group_name = var.resource_group_name
  namespace_name      = azurerm_servicebus_namespace.servicebus.name

  status                       = each.value.status
  auto_delete_on_idle          = each.value.auto_delete_on_idle
  default_message_ttl          = each.value.default_message_ttl
  enable_batched_operations    = each.value.enable_batched_operations
  enable_express               = each.value.enable_express
  enable_partitioning          = each.value.enable_partitioning
  max_size_in_megabytes        = each.value.max_size
  requires_duplicate_detection = each.value.enable_duplicate_detection
  support_ordering             = each.value.enable_ordering

  duplicate_detection_history_time_window = each.value.duplicate_detection_history_time_window
}

【问题讨论】:

  • 由于, topic),您的主题变量确实是一个元组。不知道为什么要在元组中添加主题
  • 我也试过没有那个,它没有帮助。我得到的是同样的错误
  • 你试过for_each = local.topics吗?
  • 是的,同样的反应。
  • 我假设这是 locals 中的使用大部分都很好(即某些值似乎未使用)。从语法上看,它似乎也很好,而且我很难将您的错误消息与提供的代码相协调。这可以重现吗?

标签: terraform azureservicebus terraform-provider-azure azure-servicebus-topics


【解决方案1】:

您可以使用count 创建所有资源:

resource "azurerm_servicebus_topic" "topic" {
    count = len(local.topics)

    name                = each.value.name
    resource_group_name = var.resource_group_name
    namespace_name      = azurerm_servicebus_namespace.servicebus.name

    status              = local.topics[count.index].status
    ...
}

这避免了试图将一堆对象变成一个集合的混乱。

编辑:

如果您需要 for_each 按名称提取对象,请使用地图。选择一个您知道将是唯一的键 - 在这种情况下,我使用了“名称”:

locals {
    topics = {}
        for topic in var.topics : topic.name = merge({
        name                       = ""
        status                     = "Active"
        ...
        duplicate_detection_history_time_window = null
    }, topic)
]}

本地现在是一个对象。它需要是一张地图:

 resource "azurerm_servicebus_topic" "topic" {
    for_each = tomap(local.topics)

    name                = each.value.name
    resource_group_name = var.resource_group_name
    namespace_name      = azurerm_servicebus_namespace.servicebus.name

    status              = each.value.status
    ...
}

【讨论】:

  • 是的,我有 count 的代码版本,可以正常工作。我正在浏览一些关于 for_each 的文章,我想将它迁移到我面临这个问题的 foreach 中。 medium.com/@business_99069/…
猜你喜欢
  • 2021-09-13
  • 2021-12-13
  • 2020-01-01
  • 2021-08-25
  • 2020-10-04
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多