Amazon SNS to AWS Lambda

SNS → AWS Lambda

Create a Lambda function that subscribes to an SNS topic

The SAM template deploys a Lambda function, an SNS topic and the IAM permissions required to run the application. SNS invokes the Lambda function when new messages are available.
When messages are sent to the SNS topic, they are delivered as a JSON event payload to the Lambda function.

< Back to all patterns

GitHub icon Download this pattern (.zip)

GitHub icon View this pattern on GitHub


Clone repo

git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/sns-lambda

Deploy

sam buildsam deploy --guided


Testing

Use the AWS CLI to send a message to the SNS topic and observe the event delivered to the Lambda function:
1. Send the SNS message: aws sns publish --topic-arn ENTER_SNS_TOPIC_ARN FROM_OUTPUT --subject testSubject --message testMessage.
2. Retrieve the logs from the Lambda function: sam logs -n ENTER_YOUR_CONSUMER_FUNCTION_NAME.

Cleanup

1. Delete the stack: sam delete --stack-name STACK_NAME.
2. Confirm the stack has been deleted: aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus"

Created by:

James Beswick

James Beswick

I am a self-confessed serverless geek and enjoy helping developers build faster with serverless! I have been software developer and product manager before becoming a Developer Advocate. Ask me anything!

Follow on LinkedIn