Home > Back-end >  terraform missing resource instance key,issue with for_each
terraform missing resource instance key,issue with for_each

Time:10-16

    resource "aws_sqs_queue" "CloudTrail_SQS"{

       for_each                   = var.sqs_queue_names
      name                       = each.value
      visibility_timeout_seconds = var.visibility_timeout_seconds
      max_message_size           = var.max_message_size
      message_retention_seconds  = var.message_retention_seconds
      delay_seconds              = var.delay_seconds
      receive_wait_time_seconds  = var.receive_wait_time_seconds
      redrive_policy = jsonencode({
        deadLetterTargetArn    = aws_sqs_queue.CloudTrail_SQS_DLQ[each.key].arn
        maxReceiveCount        = var.max_receive_count
    })

    tags = var.default_tags
    
    }

    resource "aws_sqs_queue" "CloudTrail_SQS_DLQ"{

    for_each                   = var.dead_queue_names
    name                       = each.value
    visibility_timeout_seconds = var.visibility_timeout_seconds
    max_message_size           = var.max_message_size
    message_retention_seconds  = var.message_retention_seconds
    delay_seconds              = var.delay_seconds
    receive_wait_time_seconds  = var.receive_wait_time_seconds

    tags = var.default_tags
    }

    

    resource "aws_sqs_queue_policy" "Cloudtrail_SQS_Policy" {
     queue_url = aws_sqs_queue.CloudTrail_SQS[each.key].id

My terragrunt plan returns this. it complains about for_each and when i try to fix that i get another errror, but unsure what to do with it:

    1.....Error: Missing resource instance key
│
│ on iam.tf line 2, in resource "aws_sqs_queue_policy" "Cloudtrail_SQS_Policy":
│ 2: queue_url = aws_sqs_queue.CloudTrail_SQS.id
│
│ Because aws_sqs_queue.CloudTrail_SQS has "for_each" set, its attributes
│ must be accessed on specific instances.
│
│ For example, to correlate with indices of a referring resource, use:
│ aws_sqs_queue.CloudTrail_SQS[each.key]

2...Error: Missing resource instance key
│
│ on iam.tf line 14, in resource "aws_sqs_queue_policy" "Cloudtrail_SQS_Policy":
│ 14: "Resource": "${aws_sqs_queue.CloudTrail_SQS.arn}",
│
│ Because aws_sqs_queue.CloudTrail_SQS has "for_each" set, its attributes
│ must be accessed on specific instances.
│
│ For example, to correlate with indices of a referring resource, use:
│ aws_sqs_queue.CloudTrail_SQS[each.key]

3.........
Error: Reference to "each" in context without for_each
│
│ on iam.tf line 27, in resource "aws_sqs_queue_policy" "CloudTrail_SQS_DLQ":
│ 27: queue_url = aws_sqs_queue.CloudTrail_SQS_DLQ[each.key].id
│
│ The "each" object can be used only in "module" or "resource" blocks, and
│ only when the "for_each" argument is set.

CodePudding user response:

There is no for_each in your Cloudtrail_SQS_Policy. Thus you can't use each.key.

Guess it should be:

    resource "aws_sqs_queue_policy" "Cloudtrail_SQS_Policy" {  
      for_each                   = var.dead_queue_names
      queue_url = aws_sqs_queue.CloudTrail_SQS[each.key].id
  • Related