|
| 1 | + |
| 2 | +# Lambda to Amazon SNS (CDK) |
| 3 | + |
| 4 | +This pattern deploys an SNS Topic and a Lambda Function that will send a custom message to the topic once triggered. |
| 5 | +The CDK application contains the minimum IAM resources required to run the application. |
| 6 | + |
| 7 | +Learn more about this pattern at: https://serverlessland.com/patterns/lambda-sns-cdk |
| 8 | + |
| 9 | +Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. |
| 10 | + |
| 11 | +## Requirements |
| 12 | + |
| 13 | +* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. |
| 14 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 15 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 16 | +* [AWS Cloud Development Kit](https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html) (AWS CDK >= 1.124.0) Installed |
| 17 | + |
| 18 | +## Language |
| 19 | +Python |
| 20 | + |
| 21 | +## Framework |
| 22 | +CDK |
| 23 | + |
| 24 | +## Services From/To |
| 25 | +AWS Lambda to Amazon SNS |
| 26 | + |
| 27 | +## Deployment Instructions |
| 28 | + |
| 29 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 30 | + ```bash |
| 31 | + git clone https://github.com/aws-samples/serverless-patterns |
| 32 | + ``` |
| 33 | +2. Change directory to the pattern directory: |
| 34 | + ```bash |
| 35 | + cd lambda-sns-cdk/src |
| 36 | + ``` |
| 37 | +3. Create a virtual environment for python: |
| 38 | + ```bash |
| 39 | + python3 -m venv .venv |
| 40 | + ``` |
| 41 | +4. Activate the virtual environment: |
| 42 | + ```bash |
| 43 | + source .venv/bin/activate |
| 44 | + ``` |
| 45 | + |
| 46 | + If you are in Windows platform, you would activate the virtualenv like this: |
| 47 | + |
| 48 | + ``` |
| 49 | + % .venv\Scripts\activate.bat |
| 50 | + ``` |
| 51 | + |
| 52 | +5. Install python modules: |
| 53 | + ```bash |
| 54 | + python3 -m pip install -r requirements.txt |
| 55 | + ``` |
| 56 | + |
| 57 | + If the previous command doesn't work, try this one bellow: |
| 58 | + ```bash |
| 59 | + pip3 install -r requirements.txt |
| 60 | + ``` |
| 61 | +6. From the command line, use CDK to synthesize the CloudFormation template and check for errors: |
| 62 | + ```bash |
| 63 | + cdk synth |
| 64 | + ``` |
| 65 | +7. From the command line, use CDK to deploy the stack: |
| 66 | + ```bash |
| 67 | + cdk deploy |
| 68 | + ``` |
| 69 | +8. Note the outputs from the CDK deployment process. These contain the resource names and/or ARNs which are used for testing. |
| 70 | +
|
| 71 | +
|
| 72 | +
|
| 73 | +## How it works |
| 74 | +
|
| 75 | +The CDK stack deploys the resources and the IAM permissions required to run the application. |
| 76 | +
|
| 77 | +The SNS topic specified in the stack app.py will be the target of the Lambda Function. The function reads the event to determine which SNS topic will receive the message body. |
| 78 | +
|
| 79 | +
|
| 80 | +## Testing |
| 81 | +
|
| 82 | +Tests can be done using aws-cli or directly on the console. Follow the steps below to test from the command line. |
| 83 | +
|
| 84 | +
|
| 85 | +1. Invoke the lambda function with a sample message |
| 86 | +
|
| 87 | +```` |
| 88 | +aws lambda invoke --function-name <FUNCTION_NAME> --cli-binary-format raw-in-base64-out --payload '{ "topic_arn": "<TOPIC_ARN>", "message": "Hello, SNS topic!" }' response.json" |
| 89 | +```` |
| 90 | +
|
| 91 | +2. A successful publish to the topic will cause the Lambda to return a 200 status code. Go to CloudWatch and verify the related Log Group. You should see an Info level log with a success message and the Message ID of the successfully published payload. |
| 92 | +
|
| 93 | +
|
| 94 | +
|
| 95 | +## Cleanup |
| 96 | +
|
| 97 | +1. Delete the stack |
| 98 | + ```bash |
| 99 | + aws cloudformation delete-stack --stack-name STACK_NAME |
| 100 | + ``` |
| 101 | +1. Confirm the stack has been deleted |
| 102 | + ```bash |
| 103 | + aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus" |
| 104 | + ``` |
| 105 | +
|
| 106 | +
|
| 107 | +
|
| 108 | +## Tutorial |
| 109 | +See [this useful workshop](https://cdkworkshop.com/30-python.html) on working with the AWS CDK for Python projects. |
| 110 | +
|
| 111 | +## Useful commands |
| 112 | +
|
| 113 | +* `cdk ls` list all stacks in the app |
| 114 | +* `cdk synth` emits the synthesized CloudFormation template |
| 115 | +* `cdk deploy` deploy this stack to your default AWS account/region |
| 116 | +* `cdk diff` compare deployed stack with current state |
| 117 | +* `cdk docs` open CDK documentation |
| 118 | +
|
| 119 | +
|
| 120 | +Enjoy! |
0 commit comments