Creating multiple AWS SNS topic and subscriptions

Hi,

I’m trying to create a module that will create multiple AWS SNS topics and subscriptions. I can create multiple topics no problem, but when trying to create the subscriptions to use the topics ARN I’m getting an error.

Inappropriate value for attribute "topic_arn": string required.

The code I am using is below -

main.tf
resource “aws_sns_topic” “this” {
count = var.create_sns_topic && length(var.name) > 0 ? length(var.name) : 0
name = var.name[count.index]
display_name = var.display_name
}

resource “aws_sns_topic_subscription” “this” { count = var.create_sns_topic && length(var.name) > 0 ? length(var.name) : 0
topic_arn = aws_sns_topic.this[count.index]
protocol = “var.protocol”
endpoint = “var.endpoint”
}

outputs.tf
output “sns_topic_arn” {
description = “ARN of SNS topic”
value = aws_sns_topic.this.*.arn
}

variables.tf
variable “create_sns_topic” {
type = bool
default = true
}
variable “name” {
type = list(string)
default = [“test1”, “test2”, “test3”]
}
variable “display_name” {
type = string
default = “test”
}

Can anyone offer any advice?

Maybe aws_sns_topic.this[count.index].arn?

Thank you! Its worked.