Adding a subscription to an SNS topic in CloudFormation is easily done with a few lines:

But that’s actually not enough. While creating a subscription in the AWS Console is a single step it implicitly creates a Lambda permission for SNS to invoke the function. This needs to be added as a separate resource in CloudFormation:

This will allow SNS to successfully invoke the function.

If you send a JSON message to your SNS topic that will be encapsulated in the event sent to Lambda (as JSON) and you will have to unpack it in your function like in this example for node.js runtime:

4 thoughts on “Subscribing AWS Lambda Function to SNS Topic with CloudFormation

    • If you send a JSON payload to SNS (in my case I was forwarding scaling notification from an Auto Scaling Group) you’ll get the JSON as a string in the message body of the SNS message. In your Lambda function you need to unpack the string into a data structure. (I’ll add a snippet of example code)

Leave a Reply to shiv Cancel reply

Your email address will not be published. Required fields are marked *