Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform SNS Subscription Multiple Emails

What does the formatting of multiple email addresses for the aws_sns_topic_subscription Terraform resource look like?

resource "aws_sns_topic_subscription" "target" {
  topic_arn = aws_sns_topic.some_sns_topic.arn
  protocol  = "email"
  endpoint  = "[email protected],[email protected]"
}

I've tried many combinations for the endpoint parameter:

endpoint  = "[email protected],[email protected]"
endpoint  = "[email protected]", "[email protected]"
endpoint  = ["[email protected]", "[email protected]"]

I've found nothing online or in the Terraform docs on how to do this. Thanks in advance.

like image 904
Roka Avatar asked Mar 24 '26 17:03

Roka


2 Answers

endpoint accepts only one email address if the protocol is email type.

If you have multiple email addresses, you may want to use for_each to create a subscription for each address.

resource "aws_sns_topic_subscription" "target" {
  for_each  = toset(["[email protected]", "[email protected]"])
  topic_arn = aws_sns_topic.some_sns_topic.arn
  protocol  = "email"
  endpoint  = each.value
}
like image 62
Ervin Szilagyi Avatar answered Mar 26 '26 08:03

Ervin Szilagyi


This is what I have used so far:

locals {
  emails = ["[email protected]", "[email protected]", "[email protected]"]
}

resource "aws_sns_topic_subscription" "target" { 
  count     = length(local.emails)
  topic_arn = aws_sns_topic.some_sns_topic.arn 
  protocol  = "email" 
  endpoint  = local.emails[count.index] 
}
like image 26
fgomez Avatar answered Mar 26 '26 09:03

fgomez