Skip to content

aws-samples/aws-serverless-sns-fanout

AWS Serverless Amazon SNS Fanout Sample

The sample in this repository demonstrates how to implement the fanout messaging pattern to execute Lambda functions in two ways, using an AWS Lambda function and using an Amazon SNS published message.

Requirements

AWS Command Line Interface (CLI)

The examples are configured, packaged and deployed using the AWS Serverless Application Model (SAM). To use SAM, you must install and configure the AWS Command Line Interface (CLI). Please see the link below for more detail to install and configure the CLI:

Amazon S3 Bucket

The AWS Serverless Application Model (SAM) will be used to package the project in a zip archive and upload to S3 for deployment. Before using SAM, you must create an Amazon S3 bucket in the account and region that you will use for uploading artifacts, and configure it with permissions for access using the AWS credentials configured in the prior step. Please see the link below for more detail to create an Amazon S3 bucket:

Clone GitHub Repository

The examples below can be executed from a local workstation after cloning this Git repository locally. Please see the link below for more detail to clone the repository:

Instructions

Package and Deploy

Once this repository has been cloned, the sample can be packaged and deployed using the AWS CLI and SAM.

To package the project, execute the following command in the project directory, replacing S3_BUCKET with the Amazon S3 bucket that you created in the requirements setup. For more information on the CloudFormation Package command, please see this link.

aws cloudformation package --template-file template.yml --s3-bucket S3_BUCKET -artifacts --output-template-file output-template.yml

Once the package command has completed, the files in the directory will have been packaged into a zip archive, uploaded to the specified Amazon S3 bucket, and an output AWS CloudFormation template will be created with the S3 location of the archive.

The output AWS CloudFormation template is ready to be deployed using the following command. Please replace the following with values for your stack:

  • STACK_NAME with your custom stack name
  • S3_BUCKET_NAME with the name of the S3 Bucket to receive uploads for processing
  • SNS_TOPIC_NAME with the name of the SNS Topic for messaging publishing
aws cloudformation deploy --template-file output-template.yml --stack-name STACK_NAME --capabilities CAPABILITY_IAM --parameter-overrides S3BucketName=S3_BUCKET_NAME SnsTopicName=SNS_TOPIC_NAME

The S3 Bucket name and SNS Topic name must be specified and not generated to avoid a cyclical dependency when creating subscriptions, which is covered in more detail here: How do I avoid the error "Unable to validate the following destination configurations" when using S3 event notifications in CloudFormation?

Validate

Once the CloudFormation stack has been created, you can test both methods for fanout by uploading files to the S3 bucket using different S3 prefixes.

To test the Lambda fanout strategy:

  1. Use the S3 Bucket name when launching the stack and upload a file to the prefix /uploads/lambda/

  2. Navigate to the Lambda service in the AWS Console and confirm that the MediaInfoFunction, TranscodeAudioFunction, and FanoutFunction Lambda functions were successfully invoked.

To test the SNS fanout strategy:

  1. Use the S3 Bucket name when launching the stack and upload a file to the prefix /uploads/sns/.

  2. Navigate to the Lambda service in the AWS Console and confirm that the MediaInfoFunction and TranscodeAudioFunction Lambda functions were successfully invoked. The FanoutFunction Lambda function should not be invoked.

About

Serverless Fanout example with Lambda and SNS

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages