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"]
}
我从terragrunt配置中调用的模块
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
}
更新后的代码。我从terragrunt配置中调用模块来创建2个SQS和2个DeadLetterQueue。我想要terraform创建"cloudtrail_sqs_management_event";并将其与"cloudtrail_dlq_management_event"关联,同时创建"cloudtrail_sqs_data_event"。并将其关联"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"
请更改:
type = set(string)
到
type = list(string)
原因是set
不能被索引访问。