AWS Lambda with SAM Template to subscribe an SQS to an SNS Topic.

David Sandor
Build Succeeded
Published in
3 min readJan 11, 2019

--

This block diagram describes an SNS Topic that has an SQS Subscription and a Lambda that fires off when a message is received by the SQS Queue. If you are like me, you created a simple SAM Template that defined the Lambda AWS::Serverless::Function definition that has an SQS Queue as its event source. While that is most of the solution you would find that messages never flow into your SQS Queue. You refresh the console and there are always zero messages.

Basic Lambda SQS Event handler with a SQS Queue buffering SNS Messages

But wait.. I can fire a Lambda straight off of the SNS Messages. True, you can, but in some cases your Lambda may be doing operations against an external API that could be down or have issues. In that case you may want to allow the SQS Message to be automatically retried. More on that in another post.

You have to add an AWS::SQS::QueuePolicy to the SQS Queue to allow the SNS Topic subscription to flow messages into your SQS Queue. I found that the documentation surrounding this process is a bit vague. So here is a full working example.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
MyLambdaFunction:
Type: AWS::Serverless::Function
Properties:
Handler: my-lambda.handler
Runtime: nodejs8.10
Role…

--

--