|
| 1 | +# Process Amazon SNS notification messages with AWS Lambda (.NET) |
| 2 | + |
| 3 | +This patterns shows how to process Amazon SNS messages using AWS Lambda. The AWS SAM template deploys an AWS Lambda function, an Amazon SNS topic, and the IAM permissions required to run the application. Lambda subscribes to the SNS topic to process notifications messages. When you publish a message to the SNS topic, SNS sends the message to the Lambda service asynchronously. The Lambda service invokes the the Lambda function. |
| 4 | + |
| 5 | +- Processing results are logged to Amazon CloudWatch Logs |
| 6 | + |
| 7 | +Learn more about this pattern at Serverless Land Patterns: [https://serverlessland.com/patterns/sns-lambda-dotnet-sam](https://serverlessland.com/patterns/sns-lambda-dotnet-sam) |
| 8 | + |
| 9 | +:heavy_exclamation_mark: 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](https://aws.amazon.com/pricing/) 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 Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed |
| 17 | + |
| 18 | +## Download Instructions |
| 19 | + |
| 20 | +If you download this pattern as part of the AWS Toolkit for your IDE, the toolkit downloads the files into the directory you specify. |
| 21 | + |
| 22 | +To download the patterns yourself: |
| 23 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 24 | + ``` |
| 25 | + git clone https://github.com/aws-samples/serverless-patterns |
| 26 | + ``` |
| 27 | +1. Change directory to the pattern directory: |
| 28 | + ``` |
| 29 | + cd sns-lambda-dotnet-sam |
| 30 | + ``` |
| 31 | +
|
| 32 | +## Build Instructions |
| 33 | +
|
| 34 | +*For additional information on features to help you author, build, debug, test, and deploy Lambda applications more efficiently when using Visual Studio Code, see [Introducing an enhanced local IDE experience for AWS Lambda developers](https://aws.amazon.com/blogs/compute/introducing-an-enhanced-local-ide-experience-for-aws-lambda-developers?trk=2dd77e51-cb93-4970-a61a-5993781e5576&sc_channel=el).* |
| 35 | +
|
| 36 | +1. From the command line, use AWS SAM to build the AWS resources for the pattern as specified in the `template.yml` file: |
| 37 | + ``` |
| 38 | + sam build |
| 39 | + ``` |
| 40 | +* You can also use `--use-container` to build your function inside a Lambda-like Docker container: |
| 41 | + ``` |
| 42 | + sam build --guided --use-container |
| 43 | + ``` |
| 44 | +
|
| 45 | +## Deployment Instructions |
| 46 | +
|
| 47 | +1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the `template.yaml` file: |
| 48 | + ``` |
| 49 | + sam deploy --guided |
| 50 | + ``` |
| 51 | +1. During the prompts: |
| 52 | +
|
| 53 | +1. During the prompts: |
| 54 | + * Enter a stack name. |
| 55 | + * Enter the desired AWS Region. |
| 56 | + * Allow AWS SAM CLI to create IAM roles with the required permissions. |
| 57 | +
|
| 58 | + Once you have run `sam deploy -guided` mode once and saved arguments to a configuration file `samconfig.toml`, you can use `sam deploy` in future to use these defaults. |
| 59 | +
|
| 60 | +1. Note the outputs from the AWS SAM deployment process. These contain the resource names and/or ARNs to use for testing. |
| 61 | + |
| 62 | +## Example event payload from SNS to Lambda |
| 63 | +
|
| 64 | +``` |
| 65 | +{ |
| 66 | + "Records": [ |
| 67 | + { |
| 68 | + "EventVersion": "1.0", |
| 69 | + "EventSubscriptionArn": "arn:aws:sns:us-east-1:123456789012:sns-lambda:21be56ed-a058-49f5-8c98-aedd2564c486", |
| 70 | + "EventSource": "aws:sns", |
| 71 | + "Sns": { |
| 72 | + "SignatureVersion": "1", |
| 73 | + "Timestamp": "2019-01-02T12:45:07.000Z", |
| 74 | + "Signature": "tcc6faL2yUC6dgZdmrwh1Y4cGa/ebXEkAi6RibDsvpi+tE/1+82j...65r==", |
| 75 | + "SigningCertURL": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-ac565b8b1a6c5d002d285f9598aa1d9b.pem", |
| 76 | + "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e", |
| 77 | + "Message": "Hello from SNS!", |
| 78 | + "MessageAttributes": { |
| 79 | + "Test": { |
| 80 | + "Type": "String", |
| 81 | + "Value": "TestString" |
| 82 | + }, |
| 83 | + "TestBinary": { |
| 84 | + "Type": "Binary", |
| 85 | + "Value": "TestBinary" |
| 86 | + } |
| 87 | + }, |
| 88 | + "Type": "Notification", |
| 89 | + "UnsubscribeUrl": "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:123456789012:test-lambda:21be56ed-a058-49f5-8c98-aedd2564c486", |
| 90 | + "TopicArn":"arn:aws:sns:us-east-1:123456789012:sns-lambda", |
| 91 | + "Subject": "TestInvoke" |
| 92 | + } |
| 93 | + } |
| 94 | + ] |
| 95 | + } |
| 96 | + |
| 97 | +``` |
| 98 | +There is also a sample file `\events\test-event.json` which contains a sample event payload. |
| 99 | +
|
| 100 | +### Testing |
| 101 | +
|
| 102 | +Use the [AWS CLI](https://aws.amazon.com/cli/) to send a message to the SNS topic and observe the event delivered to the Lambda function: |
| 103 | +
|
| 104 | +1. Send 10 messages to the SNS topic: |
| 105 | +
|
| 106 | +#### Bash |
| 107 | +```bash |
| 108 | +for i in {1..10}; do aws sns publish --topic-arn <<ENTER_YOUR_SNS_TOPIC_ARN>> --message "{\"message\": \"Test message-$i\"}"; done |
| 109 | +``` |
| 110 | + |
| 111 | +##### PowerShell |
| 112 | +```PowerShell |
| 113 | +1..10 | ForEach-Object { aws sns publish --topic-arn <<ENTER_YOUR_SNS_TOPIC_ARN>> --message "{`"message`": `"Test message-$_`"}" } |
| 114 | +``` |
| 115 | + |
| 116 | +2. Retrieve the logs from the Lambda function: |
| 117 | +```bash |
| 118 | +sam logs -n ENTER_YOUR_CONSUMER_FUNCTION_NAME |
| 119 | +``` |
| 120 | + |
| 121 | +## Cleanup |
| 122 | + |
| 123 | +1. Delete the stack |
| 124 | + ```bash |
| 125 | + sam delete --stack-name STACK_NAME |
| 126 | + ``` |
| 127 | +1. Confirm the stack has been deleted |
| 128 | + ```bash |
| 129 | + aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus" |
| 130 | + ``` |
| 131 | +---- |
| 132 | +Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 133 | + |
| 134 | +SPDX-License-Identifier: MIT-0 |
0 commit comments