Home > Blockchain >  How to access attributes of specific instances
How to access attributes of specific instances

Time:10-18

Below is my code snippet with the expected behaviour to create 2 SQS and DeadLetterQueue.I have been able to fix some of the previous error messages with the help of great minds on here.I am getting the following error message when i do terraform plan.the error messages are below my code.

.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.

CodePudding user response:

If you want to return lists of SQS queue attributes created using for_each, then you can use 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."
}

Similarly for the data source you would use:

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