The reason behind this AWS Lambda function is to create a general-purpose function that can capture the payload of an Amazon SNS message and log it to an S3 bucket.
Amazon SNS provides a mechanism for notifying interested subscribers of messages received on a topic. One of the subscriber types are is an AWS Lambda function.
This lambda function, once deployed, expects to be invoked by an SNS topic publish event. On invoking the function, the payload of the SNS message is inspected and written out to the S3 bucket defined in the bucketName
environment variable of the function.
The flow is depicted below:
The source code is based on a NodeJS package structure and uses NPM to import libraries that facilitate certain core functions. The following are the key files needed:
- src/lambda.js: The lambda function to transform the payload
- package.json: Defines the packaging tasks and the npm modules in use
- Need to create an AWS account
- Need to setup AWS CLI with the credentials of your AWS account
- NodeJS and NPM need to be installed
- Define an S3 bucket that will store the SNS messages
- Define an IAM Role that allows the function to write to the S3 bucket
- Define the AWS Lambda function
- Add an environment variable to the lambda function called
bucketName
that corresponds to the Amazon S3 bucket that the SNS message payload needs to be written to. - Assign the IAM role to the Lambda function, which will provide the permissions needed to write to the S3 bucket
- Create Amazon Simple Notification Service (SNS) Topic through the console
- Create a subscription that will invoke the lambda function
- Clone or fork this repository
- Ensure that you've followed the Requirements section above
- Run
npm run build
to install dependencies, package the Lambda function and node modules in a zip and finally deploys the Lambda function to AWS using the AWS CLI.
See LICENSE for further details.