Skip to content

Commit e7bb1d0

Browse files
committed
Add sqs-lambda-{python,nodejs,java,dotnet}-sam paatterns
1 parent ff79926 commit e7bb1d0

File tree

22 files changed

+1925
-0
lines changed

22 files changed

+1925
-0
lines changed

sqs-lambda-dotnet-sam/README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Processing Amazon SQS records with AWS Lambda (.NET)
2+
3+
This patterns shows how to process Amazon SQS messages using AWS Lambda. The AWS SAM template deploys an AWS Lambda function, an Amazon SQS queue, a dead-letter SQS queue, and the IAM permissions required to run the application. Lambda polls the SQS queue and invokes the Lambda function when new messages are available.
4+
5+
- To demonstrate error handling, each message has a 20% chance of random failure (remove this functionality for your own application).
6+
- Failed messages are automatically returned to the queue for retry using `batchItemFailures`.
7+
- After 3 failed processing attempts, messages are moved to the DLQ.
8+
- You should implement additional functionality to process messages that are sent to the DLQ.
9+
- Processing results are logged to Amazon CloudWatch Logs
10+
11+
Learn more about this pattern at Serverless Land Patterns: [serverlessland.com/patterns/sql-lambda](https://serverlessland.com/patterns/sqs-lambda-nodejs-sam)
12+
13+
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.
14+
15+
## Requirements
16+
17+
* [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.
18+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
19+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
20+
* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed
21+
22+
## Download Instructions
23+
24+
1. If you download this pattern as part of the AWS Toolkit for your IDE, the toolkit downloads the files into the directory you specify.
25+
26+
To download the patterns yourself:
27+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
28+
```
29+
git clone https://github.com/aws-samples/serverless-patterns
30+
```
31+
1. Change directory to the pattern directory:
32+
```
33+
cd sqs-lambda-nodejs-sam
34+
```
35+
36+
## Deployment Instructions
37+
38+
*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/).*
39+
40+
1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
41+
```
42+
sam deploy --guided
43+
```
44+
1. During the prompts:
45+
* Enter a stack name
46+
* Enter the desired AWS Region
47+
* Allow AWS SAM CLI to create IAM roles with the required permissions.
48+
49+
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.
50+
51+
1. Note the outputs from the AWS SAM deployment process. These contain the resource names and/or ARNs to use for testing.
52+
53+
## Example event payload from SQS to Lambda
54+
55+
```
56+
{
57+
"Records": [
58+
{
59+
"messageId": "fa2012345678e816-0a49-4681-ba8f-1234567890",
60+
"receiptHandle": "1234567890NmjC1234567890qODTr1234567890/XPPk/f0qU4tJtQ1234567890ihWDp8YHKhDr3V1234567890e9amjZhgg1234567890RodR1234567890lwDGpf6oLa8/B/1234567890/Pq+xP/1234567890/1234567890fIV6nFUGs71234567890zsj616CBx912M12345678908rxtUEj1234567890J8d1234567890yDcI9E12345678905mTyYZ41S2cP01NCA1234567890jcalHD1234567890Kio+HFQp1234567890OI7bTs5I7pZJ4pu+BnM8Bcki1234567890aNML5B7S12345678904eYKKcrunp1234567890Qhz7BUWPG41",
61+
"body": "Test message",
62+
"attributes": {
63+
"ApproximateReceiveCount": "1",
64+
"SentTimestamp": "1612966720445",
65+
"SenderId": "AIDAIENQZJOLO23YVJ4VO",
66+
"ApproximateFirstReceiveTimestamp": "1612966720455"
67+
},
68+
"messageAttributes": {},
69+
"md5OfBody": "82dfa5549ebc91234567890ece5f",
70+
"eventSource": "aws:sqs",
71+
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:patterns-sqs-to-lambda-MySqsQueue-1234567890",
72+
"awsRegion": "us-east-1"
73+
}
74+
]
75+
}
76+
77+
```
78+
There is also a sample file `\events\sqs-test-event.json` which contains a sample event payload with 10 items.
79+
80+
### Testing
81+
82+
Use the [AWS CLI](https://aws.amazon.com/cli/) to send a message to the SQS queue and observe the event delivered to the Lambda function:
83+
84+
1. Send 10 messages to the SQS message:
85+
86+
#### Bash
87+
```bash
88+
for i in {1..10}; do aws sqs send-message --queue-url <<ENTER_YOUR_SQS_QUEUE_URL>> --message-body "{\"message\": \"Test message-$i\"}"; done
89+
```
90+
91+
##### PowerShell
92+
```PowerShell
93+
1..10 | ForEach-Object { aws sqs send-message --queue-url <<ENTER_YOUR_SQS_QUEUE_URL>> --message-body "{`"message`": `"Test message-$_`"}" }
94+
```
95+
96+
2. Retrieve the logs from the Lambda function:
97+
```bash
98+
sam logs -n ENTER_YOUR_CONSUMER_FUNCTION_NAME
99+
```
100+
101+
## Cleanup
102+
103+
1. Delete the stack
104+
```bash
105+
sam delete --stack-name STACK_NAME
106+
```
107+
1. Confirm the stack has been deleted
108+
```bash
109+
aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus"
110+
```
111+
----
112+
Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
113+
114+
SPDX-License-Identifier: MIT-0
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
{
2+
"Records": [
3+
{
4+
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
5+
"receiptHandle": "MessageReceiptHandle-1",
6+
"body": "Test message-1",
7+
"attributes": {
8+
"ApproximateReceiveCount": "1",
9+
"SentTimestamp": "1523232000000",
10+
"SenderId": "123456789012",
11+
"ApproximateFirstReceiveTimestamp": "1523232000001"
12+
},
13+
"messageAttributes": {},
14+
"md5OfBody": "7b270e59b47ff90a553787216d55d91d",
15+
"eventSource": "aws:sqs",
16+
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
17+
"awsRegion": "us-east-1"
18+
},
19+
{
20+
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb79",
21+
"receiptHandle": "MessageReceiptHandle-2",
22+
"body": "Test message-2",
23+
"attributes": {
24+
"ApproximateReceiveCount": "1",
25+
"SentTimestamp": "1523232000000",
26+
"SenderId": "123456789012",
27+
"ApproximateFirstReceiveTimestamp": "1523232000001"
28+
},
29+
"messageAttributes": {},
30+
"md5OfBody": "7b270e59b47ff90a553787216d55d91d",
31+
"eventSource": "aws:sqs",
32+
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
33+
"awsRegion": "us-east-1"
34+
},
35+
{
36+
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb80",
37+
"receiptHandle": "MessageReceiptHandle-3",
38+
"body": "Test message-3",
39+
"attributes": {
40+
"ApproximateReceiveCount": "1",
41+
"SentTimestamp": "1523232000000",
42+
"SenderId": "123456789012",
43+
"ApproximateFirstReceiveTimestamp": "1523232000001"
44+
},
45+
"messageAttributes": {},
46+
"md5OfBody": "7b270e59b47ff90a553787216d55d91d",
47+
"eventSource": "aws:sqs",
48+
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
49+
"awsRegion": "us-east-1"
50+
},
51+
{
52+
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb81",
53+
"receiptHandle": "MessageReceiptHandle-4",
54+
"body": "Test message-4",
55+
"attributes": {
56+
"ApproximateReceiveCount": "1",
57+
"SentTimestamp": "1523232000000",
58+
"SenderId": "123456789012",
59+
"ApproximateFirstReceiveTimestamp": "1523232000001"
60+
},
61+
"messageAttributes": {},
62+
"md5OfBody": "7b270e59b47ff90a553787216d55d91d",
63+
"eventSource": "aws:sqs",
64+
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
65+
"awsRegion": "us-east-1"
66+
},
67+
{
68+
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb82",
69+
"receiptHandle": "MessageReceiptHandle-5",
70+
"body": "Test message-5",
71+
"attributes": {
72+
"ApproximateReceiveCount": "1",
73+
"SentTimestamp": "1523232000000",
74+
"SenderId": "123456789012",
75+
"ApproximateFirstReceiveTimestamp": "1523232000001"
76+
},
77+
"messageAttributes": {},
78+
"md5OfBody": "7b270e59b47ff90a553787216d55d91d",
79+
"eventSource": "aws:sqs",
80+
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
81+
"awsRegion": "us-east-1"
82+
},
83+
{
84+
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb83",
85+
"receiptHandle": "MessageReceiptHandle-6",
86+
"body": "Test message-6",
87+
"attributes": {
88+
"ApproximateReceiveCount": "1",
89+
"SentTimestamp": "1523232000000",
90+
"SenderId": "123456789012",
91+
"ApproximateFirstReceiveTimestamp": "1523232000001"
92+
},
93+
"messageAttributes": {},
94+
"md5OfBody": "7b270e59b47ff90a553787216d55d91d",
95+
"eventSource": "aws:sqs",
96+
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
97+
"awsRegion": "us-east-1"
98+
},
99+
{
100+
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb84",
101+
"receiptHandle": "MessageReceiptHandle-7",
102+
"body": "Test message-7",
103+
"attributes": {
104+
"ApproximateReceiveCount": "1",
105+
"SentTimestamp": "1523232000000",
106+
"SenderId": "123456789012",
107+
"ApproximateFirstReceiveTimestamp": "1523232000001"
108+
},
109+
"messageAttributes": {},
110+
"md5OfBody": "7b270e59b47ff90a553787216d55d91d",
111+
"eventSource": "aws:sqs",
112+
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
113+
"awsRegion": "us-east-1"
114+
},
115+
{
116+
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb85",
117+
"receiptHandle": "MessageReceiptHandle-8",
118+
"body": "Test message-8",
119+
"attributes": {
120+
"ApproximateReceiveCount": "1",
121+
"SentTimestamp": "1523232000000",
122+
"SenderId": "123456789012",
123+
"ApproximateFirstReceiveTimestamp": "1523232000001"
124+
},
125+
"messageAttributes": {},
126+
"md5OfBody": "7b270e59b47ff90a553787216d55d91d",
127+
"eventSource": "aws:sqs",
128+
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
129+
"awsRegion": "us-east-1"
130+
},
131+
{
132+
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb86",
133+
"receiptHandle": "MessageReceiptHandle-9",
134+
"body": "Test message-9",
135+
"attributes": {
136+
"ApproximateReceiveCount": "1",
137+
"SentTimestamp": "1523232000000",
138+
"SenderId": "123456789012",
139+
"ApproximateFirstReceiveTimestamp": "1523232000001"
140+
},
141+
"messageAttributes": {},
142+
"md5OfBody": "7b270e59b47ff90a553787216d55d91d",
143+
"eventSource": "aws:sqs",
144+
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
145+
"awsRegion": "us-east-1"
146+
},
147+
{
148+
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb87",
149+
"receiptHandle": "MessageReceiptHandle-10",
150+
"body": "Test message-10",
151+
"attributes": {
152+
"ApproximateReceiveCount": "1",
153+
"SentTimestamp": "1523232000000",
154+
"SenderId": "123456789012",
155+
"ApproximateFirstReceiveTimestamp": "1523232000001"
156+
},
157+
"messageAttributes": {},
158+
"md5OfBody": "7b270e59b47ff90a553787216d55d91d",
159+
"eventSource": "aws:sqs",
160+
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
161+
"awsRegion": "us-east-1"
162+
}
163+
]
164+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"title": "Process Amazon SQS records with AWS Lambda (.NET)",
3+
"description": "This pattern shows how to process Amazon SQS messages using AWS Lambda.",
4+
"language": "dotnet",
5+
"level": "200",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"The AWS SAM template deploys an AWS Lambda function, an Amazon SQS queue, a dead-letter SQS queue, and the IAM permissions required to run the application. Lambda polls the SQS queue and invokes the Lambda function when new messages are available."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/awslabs/sqs-lambda-dotnet-sam",
16+
"templateURL": "serverless-patterns/sqs-lambda-dotnet-sam",
17+
"projectFolder": "sqs-lambda-dotnet-sam"
18+
}
19+
},
20+
"resources": {
21+
"bullets": [
22+
(https://aws.amazon.com/blogs/compute/introducing-an-enhanced-local-ide-experience-for-aws-lambda-developers/).*
23+
24+
{
25+
"text": "Introducing an enhanced local IDE experience for AWS Lambda developers",
26+
"link": "https://aws.amazon.com/blogs/compute/introducing-an-enhanced-local-ide-experience-for-aws-lambda-developers/"
27+
}
28+
]
29+
},
30+
"deploy": {
31+
"text": [
32+
"sam build",
33+
"sam deploy --guided"
34+
]
35+
},
36+
"testing": {
37+
"text": [
38+
"See the GitHub repo for detailed testing instructions."
39+
]
40+
},
41+
"cleanup": {
42+
"text": [
43+
"Delete the stack: <code>sam delete</code>."
44+
]
45+
},
46+
"authors": [
47+
{
48+
"name": "Julian Wood",
49+
"image": "/assets/images/resources/jrwood.jpg",
50+
"bio": "Julian Wood helps developers and builders learn about and love how serverless technologies can transform the way they build and run applications. I was an infrastructure architect and manager in global enterprises and start-ups for more than 25 years before going all in on serverless at AWS.",
51+
"linkedin": "woodjulian",
52+
"twitter": "@julian_wood"
53+
}
54+
]
55+
}
56+

0 commit comments

Comments
 (0)