简体   繁体   中英

how to associate each SQS and DLQ created using for_each with one another in orderly format

Terragrunt.hcl

terraform {
  source = "../..//infrastructure/module"
}


include {
  path = find_in_parent_folders()
}

inputs = {
  sqs_queue_names                  = ["CloudTrail_SQS_Management_Event", "CloudTrail_SQS_Data_Event"]
  dead_queue_names                 = ["CloudTrail_DLQ_Management_Event", "CloudTrail_DLQ_Data_Event"]
}

Module that i am calling from my terragrunt configuration

Variable.tf
variable "sqs_queue_names"{
  description = "The name of different SQS to be created"
  type        = set(string)
}

variable "dead_queue_names"{
  description = "The name of different Dead Queues to be created"
  type        = set(string)
}
resource "aws_sqs_queue" "CloudTrail_SQS"{

    for_each                   = {for idx, val in var.sqs_queue_names: idx => val}
    name                       = each.value
    redrive_policy = jsonencode({
        deadLetterTargetArn    = values(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                   = toset(var.dead_queue_names)
    name                       = each.value
   

    tags = var.default_tags
}

An updated code.I am calling the module from my terragrunt configuration to create 2 SQS and 2 DeadLetterQueue.I want terraform to create "CloudTrail_SQS_Management_Event" and Associate it with "CloudTrail_DLQ_Management_Event",also to create CloudTrail_SQS_Data_Event" and associate it "CloudTrail_DLQ_Data_Event"

Error Message:
Error: Invalid index
│ 
│   on main.tf line 15, in resource "aws_sqs_queue" "CloudTrail_SQS":
│   15:         deadLetterTargetArn    = values(aws_sqs_queue.CloudTrail_SQS_DLQ)[each.key].arn
│     ├────────────────
│     │ aws_sqs_queue.CloudTrail_SQS_DLQ is object with 2 attributes
│     │ each.key is "CloudTrail_SQS_Management_Event"

Please change:

type        = set(string)

into

type        = list(string)

The reason is that set can't be accessed by an index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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