How to create email subscription to AWS sns_topic_subscription in terraform?

5,408 views
Skip to first unread message

egul...@gmail.com

unread,
Oct 3, 2016, 2:02:41 PM10/3/16
to Terraform
Hello everyone,

Since 'email' option is NOT SUPPORTED in terraform. How does anyone handle creation of subscriptions in terraform?

In cloudformation it's just 4 lines of code and I'm surprised that this simple feature is missing here.

Any advice appreciated.


Thank you

Dean Wilson

unread,
Oct 4, 2016, 6:27:10 AM10/4/16
to terrafo...@googlegroups.com
On 3 October 2016 at 19:02, <egul...@gmail.com> wrote:

> Since 'email' option is NOT SUPPORTED in terraform. How does anyone handle
> creation of subscriptions in terraform?

I wrap a CloudFormation template and call that from Terraform. It
doesn't handle the "Accept Subscription" email but it does create the
subscription. The code was written for 0.6 but there's a PR that
should add support for 0.7 and data sources that I need to test.

Hope this helps,
Dean
--
Dean Wilson http://www.unixdaemon.net

egul...@gmail.com

unread,
Oct 5, 2016, 11:49:53 AM10/5/16
to Terraform
Hi Dean,

I tried code from your github - tf_sns_email placeholder but 'tf plan' fails with this error:


* Resource 'aws_cloudformation_stack.sns-topic' does not have attribute 'outputs.ARN' for variable 'aws_cloudformation_stack.sns-topic.outputs.ARN'


Any suggestions?

Thank you

Ian Duffy

unread,
Oct 5, 2016, 11:56:06 AM10/5/16
to terrafo...@googlegroups.com
This is how we do this:

resource "aws_sns_topic" "instance-alerts" {
  name = "instance-alerts"

  provisioner "local-exec" {
    command = "aws sns subscribe --topic-arn ${self.arn} --protocol email --notification-endpoint notifi...@blah.com"
  }
}


--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/3f2893b8-700a-4691-9911-508757d80e94%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

egul...@gmail.com

unread,
Oct 5, 2016, 2:23:10 PM10/5/16
to Terraform

This is awesome!

One thing I would add is that if you have ASG & SNS Topic created already then this code will not add email subscription.
The way I fixed it is by changing the name of SNS topic then terraform replaced launch_configuration with new one and added new SNS topic with email subscription.



Thank you
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

VARUN GABA

unread,
Jun 15, 2017, 4:36:18 AM6/15/17
to Terraform

--topic-arn invalid parameter was what i got when tried this.. any idea ?
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

egul...@gmail.com

unread,
Jun 16, 2017, 10:57:26 AM6/16/17
to Terraform
How does your code look like?

Sharmila Kale

unread,
Feb 12, 2018, 3:35:27 PM2/12/18
to Terraform
Thanks! It worked for me as well. Just one thing. It's not working with a comma-separated email list. And if I give an outlook email group, it does not send any subscription email to that group. Any idea?

Thanks

On Wednesday, October 5, 2016 at 10:56:06 AM UTC-5, Ian Duffy wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
Message has been deleted
Message has been deleted

Nikhil Pawar

unread,
Apr 18, 2018, 2:52:28 PM4/18/18
to Terraform
HI Ian ,

how do we add multiple email scubscription ?
& also not understood self.arn ( i am new to terraform ) 
Can you please explain a bit if possible 

Nikhil

 

t...@s16.partners

unread,
Jun 25, 2018, 7:18:34 AM6/25/18
to Terraform
I imagine you've got this sorted by now, but for anyone else that comes across this post, this seems to work (bear in mind I'm quite new to TF).

resource "aws_sns_topic" "billing_notifications" {
name = "billing-notifications"

provisioner "local-exec" {
command = "aws sns subscribe --topic-arn ${self.arn} --protocol email --notification-endpoint ${var.emails["someone"]}"
}

provisioner "local-exec" {
command = "aws sns subscribe --topic-arn ${self.arn} --protocol email --notification-endpoint ${var.emails["esomeone_else"]}"
}
}


There may be a nicer way of handling this, but it doesn't look like TF templates have a loop or iterate function.

t...@s16.partners

unread,
Jun 25, 2018, 7:21:12 AM6/25/18
to Terraform
I had an invalid TopicArn error too, but had forgot to change my AWS CLI credentials to the same ones I was using in TF. The provisioner that calls the AWS CLI command does not use the credentials in TF - it uses the ones configured against your CLI.

Rob Coward

unread,
Jun 25, 2018, 9:20:55 AM6/25/18
to terrafo...@googlegroups.com
How about using a null_resource with a count iterator ?

resource "aws_sns_topic" "billing_notifications" {
  name = "billing-notifications”

}

resource “null_resource” “sns_subscribe” {
depends_on = [ “aws_sns_topic.billing_notifications” ]
triggers = {
sns_topic_arn = “${aws_sns_topic.billing_notifications.arn}”
}
count = “${length(var.emails)}”

provisioner "local-exec" {
command = "aws sns subscribe --topic-arn ${aws_sns_topic.billing_notifications.arn} --protocol email --notification-endpoint ${element(var.emails, count.index)}"
}
}

Regards,
--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/43cb4a91-e5ec-4e03-ae46-2fa8813aa577%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages