Skip to content

Commit 55a8c52

Browse files
committed
Add sns-lambda-<language>-sam patterns
1 parent 30a2ba6 commit 55a8c52

File tree

22 files changed

+1383
-0
lines changed

22 files changed

+1383
-0
lines changed

sns-lambda-dotnet-sam/.gitignore

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
*.swp
2+
*.*~
3+
project.lock.json
4+
.DS_Store
5+
*.pyc
6+
nupkg/
7+
8+
# AWS SAM
9+
.aws-sam/
10+
samconfig.toml
11+
.sam/
12+
packaged.yaml
13+
packaged-template.yaml
14+
template.packaged.yaml
15+
16+
# Visual Studio Code
17+
.vscode
18+
19+
# Rider
20+
.idea
21+
22+
# Visual Studio
23+
.vs/
24+
25+
# Fleet
26+
.fleet/
27+
28+
# Code Rush
29+
.cr/
30+
31+
# User-specific files
32+
*.suo
33+
*.user
34+
*.userosscache
35+
*.sln.docstates
36+
37+
# Build results
38+
[Dd]ebug/
39+
[Dd]ebugPublic/
40+
[Rr]elease/
41+
[Rr]eleases/
42+
x64/
43+
x86/
44+
build/
45+
bld/
46+
[Bb]in/
47+
[Oo]bj/
48+
[Oo]ut/
49+
msbuild.log
50+
msbuild.err
51+
msbuild.wrn
52+
53+
# Visual Studio
54+
*.VisualState.xml
55+
TestResult.xml
56+
[Bb]uild[Ll]og.*
57+
58+
# NuGet Packages
59+
*.nupkg
60+
# The packages folder can be ignored because of Package Restore
61+
**/packages/*
62+
# except build/, which is used as an MSBuild target.
63+
!**/packages/build/
64+
# Uncomment if necessary however generally it will be regenerated when needed
65+
#!**/packages/repositories.config
66+
67+
# User-specific files
68+
*.rsuser
69+
70+
# .NET Core
71+
project.lock.json
72+
project.fragment.lock.json
73+
artifacts/
74+
75+
# ASP.NET Scaffolding
76+
ScaffoldingReadMe.txt
77+
78+
# StyleCop
79+
StyleCopReport.xml
80+
81+
# Files built by Visual Studio
82+
*_i.c
83+
*_p.c
84+
*_h.h
85+
*.ilk
86+
*.meta
87+
*.obj
88+
*.iobj
89+
*.pch
90+
*.pdb
91+
*.ipdb
92+
*.pgc
93+
*.pgd
94+
*.rsp
95+
*.sbr
96+
*.tlb
97+
*.tli
98+
*.tlh
99+
*.tmp
100+
*.tmp_proj
101+
*_wpftmp.csproj
102+
*.log
103+
*.tlog
104+
*.vspscc
105+
*.vssscc
106+
.builds
107+
*.pidb
108+
*.svclog
109+
*.scc
110+
111+
# ReSharper
112+
_ReSharper*/
113+
*.[Rr]e[Ss]harper
114+
*.DotSettings.user
115+
116+
# DotCover
117+
*.dotCover
118+
119+
# Local History
120+
.localhistory/
121+
122+
# BeatPulse healthcheck temp database
123+
healthchecksdb
124+
125+
# Backup & report files from converting an old project file
126+
# to a newer Visual Studio version
127+
_UpgradeReport_Files/
128+
Backup*/
129+
UpgradeLog*.XML
130+
UpgradeLog*.htm
131+
ServiceFabricBackup/
132+
*.rptproj.bak
133+
134+
# SQL Server files
135+
*.mdf
136+
*.ldf
137+
*.ndf
138+
139+
# Node.js Tools for Visual Studio
140+
node_modules/

sns-lambda-dotnet-sam/README.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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&amp;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

Comments
 (0)