This example demonstrates the delivery from SNS to SQS queues through subscriptions, triggering Lambda functions via SQS event source mappings, and accessing resources from within a Lambda function. The sample is deployed using LocalStack on your local machine and on a CI pipeline without accessing any real cloud resources.
The basic pipeline is:
- Publish to SNS Topic, initiated by user input.
- Transfer from SNS Topic to SQS queue, facilitated by an SNS subscription.
- SQS Queue triggers a Lambda invocation, configured through a Lambda event source mapping.
- Lambda function interacts with SQS using the AWS SDK for Python (
boto3
), executed within the Lambda. - SQS queue message reception, activated by user input.
- LocalStack &
localstack
CLI - Pulumi (
pulumi
) CLI - Docker
- AWS CLI &
awslocal
wrapper script
To start LocalStack, execute:
localstack start -d
You can use a local backend for Pulumi, which will store the state in a local file. Run the following command to configure the local backend:
pulumi login --local
Configure the Pulumi stack dev
using the following command:
pulumi stack init dev
Install the necessary dependencies with:
yarn install
Zip the Lambda function with the following command:
zip lambda.zip lambda.py
For stack preview and deployment, run:
pulumi up --yes
You might be prompted for
PULUMI_CONFIG_PASSPHRASE
orPULUMI_CONFIG_PASSPHRASE_FILE
. If you haven't set a passphrase, you can just pressEnter
.
After a successful deployment, you should see the following output:
Updating (dev):
Type Name Status
+ pulumi:pulumi:Stack sns-sqs-dev created (0.89s)
+ ├─ aws:sns:Topic trigger-event-topic created (1s)
+ ├─ aws:sqs:Queue lambda-event-queue created (26s)
+ ├─ aws:sqs:Queue lambda-result-queue created (25s)
+ ├─ aws:lambda:Function event-echo-lambda created (29s)
+ ├─ aws:sns:TopicSubscription lambda-event-queue-sub created (0.05s)
+ └─ aws:lambda:EventSourceMapping sqs-lambda-trigger created (0.05s)
Resources:
+ 7 created
Duration: 57s
Post deployment, verify the available SQS queues & SNS topics with the following commands:
awslocal sqs list-queues
awslocal sns list-topics
You can publish a message to the SNS topic via:
awslocal sns publish \
--topic arn:aws:sns:us-east-1:000000000000:trigger-event-topic \
--message '{"event_type":"testing","event_payload":"hello world"}'
You can receive the message from the SQS queue via:
awslocal sqs receive-message --wait-time-seconds 10 --visibility-timeout=0 \
--queue=http://sqs.us-east-1.localhost.localstack.cloud:4566/000000000000/lambda-result-queue
You will find the hello world
message in the output.
This code is available under the Apache 2.0 license.