如何访问特定实例的属性

How to access attributes of specific instances

下面是我的代码片段,具有创建 2 个 SQS 的预期行为,并且 DeadLetterQueue.I 已经能够在 here.I 的伟大思想的帮助下修复之前的一些错误消息当我执行 terraform 时出现以下错误消息 plan.the 错误消息在我的代码下方。

.tfvars

sqs_queue_names = ["CloudTrail_Event_One", "CloudTrail_SQS_Event_Two"]

variable.tf

variable "sqs_queue_names"{
   type = set(string)
}
output.tf

output "sqs_queue_arn" {
  value       =  aws_sqs_queue.CloudTrail_SQS.arn
  description = "The ARN of the SQS queue."
}

output "sqs_queue_id"{
    value       =    aws_sqs_queue.CloudTrail_SQS.id
    description = "The URL for the created Amazon SQS queue."
}
data "aws_iam_policy_document" "policy_document"{
  statement{
    actions = [
      "sqs:GetQueueUrl",
      "sqs:ReceiveMessage"
  
    ]
    effect = "Allow"
    resources =[
      "${aws_sqs_queue.CloudTrail_SQS[each.key].arn}
ERROR MESSAGES:

Error: Missing resource instance key
│ 
│   on output.tf line 10, in output "sqs_queue_arn":
│   10:   value       =  aws_sqs_queue.CloudTrail_SQS.arn
│ 
│ Because aws_sqs_queue.CloudTrail_SQS has "for_each" set, its attributes
│ must be accessed on specific instances.

Error: Missing resource instance key
│ 
│   on output.tf line 11, in output "sqs_queue_id":
│   11:   value       =  aws_sqs_queue.CloudTrail_SQS.id
│ 
│ Because aws_sqs_queue.CloudTrail_SQS has "for_each" set, its attributes
│ must be accessed on specific instances.

on iam_role.tf line 40, in data "aws_iam_policy_document" "policy_document":
│ 40: "${aws_sqs_queue.CloudTrail_SQS[each.key].arn}"
│
│ The "each" object can be used only in "module" or "resource" blocks, and
│ only when the "for_each" argument is set.

如果您想 return 列出 使用 for_each 创建的 SQS 队列属性,那么您可以使用 values:

output "sqs_queue_arn" {
  value       =  values(aws_sqs_queue.CloudTrail_SQS)[*].arn
  description = "The ARN of the SQS queue."
}

output "sqs_queue_id"{
    value       =    values(aws_sqs_queue.CloudTrail_SQS)[*].id
    description = "The URL for the created Amazon SQS queue."
}

与您将使用的数据源类似:

    resources = values(aws_sqs_queue.CloudTrail_SQS)[*].arn