SNS subscription

Hi, I’m trying to declare a few SQS queues, SNS topic and subscriptions in a single step, but unable to establish how I can pass the SQS queue ARNs to the subscription step:

provider “aws” {
region = “eu-west-1”
}

resource “aws_sqs_queue” “sqs_queue” {
for_each = toset(var.sqs_queue)

name = each.value
}

resource “aws_sns_topic” “sns_topic” {
name = “myTopic”
}

resource “aws_sns_topic_subscription” “sns_topic_subscription” {
for_each = toset(var.sqs_queue)

topic_arn = aws_sns_topic.sns_topic.arn
protocol = “sqs”
endpoint = aws_sqs_queue.sqs_queue[???]
}

If anyone with more experience, and understanding of Terraform, who can give me some pointers I’d appreciate it, as reading about index, element etc. is not producing a working solution. TIA.

Hi there,
could you please provide the definition of var.sqs_queue so we can get a proper understanding of the case?

I get the feeling that because you are not working with a map you won’t be able to reference to each.key with aws_sqs_queue.sqs_queue[each.key].arn.

The solution I am proposing would be to declare var.sqs_queue as a map and use each of its key names as the keys for the sqs_queue map created by the for_each in aws_sqs_queue resource.

Hope this helps !