【问题标题】:Terraform for_each not recognizing all values in variableTerraform for_each 无法识别变量中的所有值
【发布时间】:2022-01-14 17:50:18
【问题描述】:

我定义了以下变量:

variable "animals" {
  description = "Animal S3 buckets"
  default = {
    cat = {
      name = "cat-logs",
      description  = "Logs for cat bucket"
    },
    dog = {
      name = "config-logs",
      description  = "Logs for dog bucket"
    },
    mouse = {
      name = "mouse-logs",
      description  = "Logs for mouse bucket"
    },
    fish = {
      name = "fish-logs",
      description = "Logs for fish bucket"
  }
}

#Queue
resource "aws_sqs_queue" "animal_queues" {
  for_each = var.animals
  name     = "${each.value.name}"
  redrive_policy = jsonencode({
    deadLetterTargetArn = aws_sqs_queue.s3_replication_dl[each.key].arn
    maxReceiveCount     = 50
  })
}

#DL Queue
resource "aws_sqs_queue" "animal_dl" {
  for_each = var.animals
  name     = "${each.value.name}-dl"
}

我会说,当变量中只有“cat”和“dog”时,我已经成功运行了这个。目前,猫和狗有一个sqs队列。从那时起,我添加了两个新元素:“鼠标”和“鱼”

现在运行代码时,无论是尝试应用还是销毁,都会出现如下错误:

Error: Invalid index
  on main.tf line 303, in resource "aws_sqs_queue" "animal_queues":
 303:     deadLetterTargetArn = aws_sqs_queue.animal_dl[each.key].arn
    |----------------
    | aws_sqs_queue.animal_dl is object with 2 attributes
    | each.key is "mouse" 
    | each.key is "fish"
The given key does not identify an element in this collection value.

不确定这里出了什么问题,因为它以前工作过。非常感谢任何指导。

【问题讨论】:

    标签: foreach terraform


    【解决方案1】:

    我能够重现错误并最终通过将这一行将deadLetterTargetArn = aws_sqs_queue.s3_replication_dl[each.key].arn 更改为deadLetterTargetArn = join("",["aws_sqs_queue.s3_replication_dl",each.key,".arn"]) 来修复它

    我使用 Terraform v0.15.5 进行了测试 在 v0.12.0 中引入了 join 函数

    这是我使用 join 函数后得到的输出... lmk 如果这是您所期望的;

    Terraform will perform the following actions:
    
      # aws_sqs_queue.animal_queues["cat"] will be created
      + resource "aws_sqs_queue" "animal_queues" {
          + arn                               = (known after apply)
          + content_based_deduplication       = false
          + deduplication_scope               = (known after apply)
          + delay_seconds                     = 0
          + fifo_queue                        = false
          + fifo_throughput_limit             = (known after apply)
          + id                                = (known after apply)
          + kms_data_key_reuse_period_seconds = (known after apply)
          + max_message_size                  = 262144
          + message_retention_seconds         = 345600
          + name                              = "cat-logs"
          + name_prefix                       = (known after apply)
          + policy                            = (known after apply)
          + receive_wait_time_seconds         = 0
          + redrive_policy                    = jsonencode(
                {
                  + deadLetterTargetArn = "aws_sqs_queue.s3_replication_dlcat.arn"
                  + maxReceiveCount     = 50
                }
            )
          + tags_all                          = (known after apply)
          + url                               = (known after apply)
          + visibility_timeout_seconds        = 30
        }
    
      # aws_sqs_queue.animal_queues["dog"] will be created
      + resource "aws_sqs_queue" "animal_queues" {
          + arn                               = (known after apply)
          + content_based_deduplication       = false
          + deduplication_scope               = (known after apply)
          + delay_seconds                     = 0
          + fifo_queue                        = false
          + fifo_throughput_limit             = (known after apply)
          + id                                = (known after apply)
          + kms_data_key_reuse_period_seconds = (known after apply)
          + max_message_size                  = 262144
          + message_retention_seconds         = 345600
          + name                              = "config-logs"
          + name_prefix                       = (known after apply)
          + policy                            = (known after apply)
          + receive_wait_time_seconds         = 0
          + redrive_policy                    = jsonencode(
                {
                  + deadLetterTargetArn = "aws_sqs_queue.s3_replication_dldog.arn"
                  + maxReceiveCount     = 50
                }
            )
          + tags_all                          = (known after apply)
          + url                               = (known after apply)
          + visibility_timeout_seconds        = 30
        }
    
      # aws_sqs_queue.animal_queues["fish"] will be created
      + resource "aws_sqs_queue" "animal_queues" {
          + arn                               = (known after apply)
          + content_based_deduplication       = false
          + deduplication_scope               = (known after apply)
          + delay_seconds                     = 0
          + fifo_queue                        = false
          + fifo_throughput_limit             = (known after apply)
          + id                                = (known after apply)
          + kms_data_key_reuse_period_seconds = (known after apply)
          + max_message_size                  = 262144
          + message_retention_seconds         = 345600
          + name                              = "fish-logs"
          + name_prefix                       = (known after apply)
          + policy                            = (known after apply)
          + receive_wait_time_seconds         = 0
          + redrive_policy                    = jsonencode(
                {
                  + deadLetterTargetArn = "aws_sqs_queue.s3_replication_dlfish.arn"
                  + maxReceiveCount     = 50
                }
            )
          + tags_all                          = (known after apply)
          + url                               = (known after apply)
          + visibility_timeout_seconds        = 30
        }
    
      # aws_sqs_queue.animal_queues["mouse"] will be created
      + resource "aws_sqs_queue" "animal_queues" {
          + arn                               = (known after apply)
          + content_based_deduplication       = false
          + deduplication_scope               = (known after apply)
          + delay_seconds                     = 0
          + fifo_queue                        = false
          + fifo_throughput_limit             = (known after apply)
          + id                                = (known after apply)
          + kms_data_key_reuse_period_seconds = (known after apply)
          + max_message_size                  = 262144
          + message_retention_seconds         = 345600
          + name                              = "mouse-logs"
          + name_prefix                       = (known after apply)
          + policy                            = (known after apply)
          + receive_wait_time_seconds         = 0
          + redrive_policy                    = jsonencode(
                {
                  + deadLetterTargetArn = "aws_sqs_queue.s3_replication_dlmouse.arn"
                  + maxReceiveCount     = 50
                }
            )
          + tags_all                          = (known after apply)
          + url                               = (known after apply)
          + visibility_timeout_seconds        = 30
        }
    
    Plan: 4 to add, 0 to change, 0 to destroy.
    

    【讨论】:

    • 谢谢。此语法确实通过了计划阶段,但在应用阶段失败,因为它无法识别资源名称 - 表示资源不存在。
    • 在您的原始帖子中,在 aws_sqs_queue.animal_queues 资源配置中,deadLetterTargetArn = aws_sqs_queue.s3_replication_dl[each.key].arn 但是没有描述资源 aws_sqs_queue.s3_replication_dl。我必须在测试时添加它。我已经删除了我所做的那个测试计划。那是你运行 terraform apply 时缺少的资源吗?
    猜你喜欢
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2019-06-06
    • 2021-01-06
    • 2021-01-18
    • 2021-02-27
    • 2013-05-15
    相关资源
    最近更新 更多