Skip to content

Commit 37f9c62

Browse files
committed
Update sqs-lambda-{dotnet,java.nodejs,python}-sam patterns
1 parent 0ca75de commit 37f9c62

File tree

11 files changed

+144
-169
lines changed

11 files changed

+144
-169
lines changed

sqs-lambda-dotnet-sam/README.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
# Processing Amazon SQS records with AWS Lambda (.NET)
1+
# Process Amazon SQS records with AWS Lambda (.NET)
22

33
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.
44

5-
- To demonstrate error handling, any message containing the text "error" will be reported as a failure.
65
- Failed messages are automatically returned to the queue for retry using `batchItemFailures`.
76
- After 3 failed processing attempts, messages are moved to the DLQ.
87
- You should implement additional functionality to process messages that are sent to the DLQ.
98
- Processing results are logged to Amazon CloudWatch Logs
109

11-
Learn more about this pattern at Serverless Land Patterns: [serverlessland.com/patterns/sql-lambda](https://serverlessland.com/patterns/sqs-lambda-dotnet-sam)
10+
Learn more about this pattern at Serverless Land Patterns: [https://serverlessland.com/patterns/sqs-lambda-dotnet-sam)](https://serverlessland.com/patterns/sqs-lambda-dotnet-sam)
1211

1312
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.
1413

@@ -33,28 +32,35 @@ To download the patterns yourself:
3332
cd sqs-lambda-dotnet-sam
3433
```
3534
36-
## Deployment Instructions
35+
## Build Instructions
3736
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/).*
37+
*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).*
3938
40-
1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
39+
1. From the command line, use AWS SAM to build the AWS resources for the pattern as specified in the template.yml file:
4140
```
42-
sam deploy --guided
41+
sam build
4342
```
44-
4543
* You can also use `--use-container` to build your function inside a Lambda-like Docker container:
4644
```
47-
sam deploy --guided --use-container
45+
sam build --guided --use-container
46+
```
47+
48+
## Deployment Instructions
49+
50+
1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yaml file:
51+
```
52+
sam deploy --guided
4853
```
54+
1. During the prompts:
4955
50-
2. During the prompts:
56+
1. During the prompts:
5157
* Enter a stack name
5258
* Enter the desired AWS Region
5359
* Allow AWS SAM CLI to create IAM roles with the required permissions.
5460
5561
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.
5662
57-
3. Note the outputs from the AWS SAM deployment process. These contain the resource names and/or ARNs to use for testing.
63+
1. Note the outputs from the AWS SAM deployment process. These contain the resource names and/or ARNs to use for testing.
5864
5965
## Example event payload from SQS to Lambda
6066
@@ -87,7 +93,7 @@ There is also a sample file `\events\test-event.json` which contains a sample ev
8793
8894
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:
8995
90-
1. Send 10 messages to the SQS message:
96+
1. Send 10 messages to the SQS queue:
9197
9298
#### Bash
9399
```bash

sqs-lambda-dotnet-sam/example-pattern.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"bullets": [
2323
{
2424
"text": "Using Lambda with Amazon SQS",
25-
"link": "https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html?trk=2dd77e51-cb93-4970-a61a-5993781e5576&sc_channel=el"
25+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html?trk=2dd77e51-cb93-4970-a61a-5993781e5576&sc_channel=el"
2626
},
2727
{
2828
"text": "Introducing an enhanced local IDE experience for AWS Lambda developers",
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>processsqsmessagebatch</groupId>
5+
<artifactId>ProcessSQSMessageBatch</artifactId>
6+
<version>1.0</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<maven.compiler.source>21</maven.compiler.source>
10+
<maven.compiler.target>21</maven.compiler.target>
11+
</properties>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>com.amazonaws</groupId>
16+
<artifactId>aws-lambda-java-core</artifactId>
17+
<version>1.2.2</version>
18+
</dependency>
19+
<dependency>
20+
<groupId>com.amazonaws</groupId>
21+
<artifactId>aws-lambda-java-events</artifactId>
22+
<version>3.11.0</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>junit</groupId>
26+
<artifactId>junit</artifactId>
27+
<version>4.13.2</version>
28+
<scope>test</scope>
29+
</dependency>
30+
</dependencies>
31+
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-shade-plugin</artifactId>
37+
<version>3.2.4</version>
38+
<configuration>
39+
</configuration>
40+
<executions>
41+
<execution>
42+
<phase>package</phase>
43+
<goals>
44+
<goal>shade</goal>
45+
</goals>
46+
</execution>
47+
</executions>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import com.amazonaws.services.lambda.runtime.Context;
4+
import com.amazonaws.services.lambda.runtime.RequestHandler;
5+
import com.amazonaws.services.lambda.runtime.events.SQSEvent;
6+
import com.amazonaws.services.lambda.runtime.events.SQSBatchResponse;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
public class ProcessSQSMessageBatch implements RequestHandler<SQSEvent, SQSBatchResponse> {
12+
@Override
13+
public SQSBatchResponse handleRequest(SQSEvent sqsEvent, Context context) {
14+
15+
List<SQSBatchResponse.BatchItemFailure> batchItemFailures = new ArrayList<SQSBatchResponse.BatchItemFailure>();
16+
String messageId = "";
17+
for (SQSEvent.SQSMessage message : sqsEvent.getRecords()) {
18+
try {
19+
//process your message
20+
System.out.println("Processed message: " + message.getBody());
21+
} catch (Exception e) {
22+
//Add failed message identifier to the batchItemFailures list
23+
batchItemFailures.add(new SQSBatchResponse.BatchItemFailure(message.getMessageId()));
24+
}
25+
}
26+
return new SQSBatchResponse(batchItemFailures);
27+
}
28+
}

sqs-lambda-java-sam/README.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
# Processing Amazon SQS records with AWS Lambda (Java)
1+
# Process Amazon SQS records with AWS Lambda (Java)
22

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.
3+
This pattern 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.
44

5-
- To demonstrate error handling, each message has a 20% chance of random failure (remove this functionality for your own application).
65
- Failed messages are automatically returned to the queue for retry using `batchItemFailures`.
76
- After 3 failed processing attempts, messages are moved to the DLQ.
87
- You should implement additional functionality to process messages that are sent to the DLQ.
98
- Processing results are logged to Amazon CloudWatch Logs
109

11-
Learn more about this pattern at Serverless Land Patterns: [serverlessland.com/patterns/sql-lambda](https://serverlessland.com/patterns/sqs-lambda-nodejs-sam)
10+
Learn more about this pattern at Serverless Land Patterns: [https://serverlessland.com/patterns/sqs-lambda-java-sam](https://serverlessland.com/patterns/sqs-lambda-java-sam)
1211

1312
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.
1413

@@ -21,7 +20,7 @@ Important: this application uses various AWS services and there are costs associ
2120

2221
## Download Instructions
2322

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.
23+
If you download this pattern as part of the AWS Toolkit for your IDE, the toolkit downloads the files into the directory you specify.
2524

2625
To download the patterns yourself:
2726
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
@@ -30,17 +29,30 @@ To download the patterns yourself:
3029
```
3130
1. Change directory to the pattern directory:
3231
```
33-
cd sqs-lambda-nodejs-sam
32+
cd sqs-lambda-java-sam
3433
```
3534
36-
## Deployment Instructions
35+
## Build Instructions
36+
37+
*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).*
38+
39+
1. From the command line, use AWS SAM to build the AWS resources for the pattern as specified in the template.yml file:
40+
```
41+
sam build
42+
```
43+
* You can also use `--use-container` to build your function inside a Lambda-like Docker container:
44+
```
45+
sam build --guided --use-container
46+
```
3747
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/).*
48+
## Deployment Instructions
3949
40-
1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
50+
1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yaml file:
4151
```
4252
sam deploy --guided
4353
```
54+
1. During the prompts:
55+
4456
1. During the prompts:
4557
* Enter a stack name
4658
* Enter the desired AWS Region
@@ -56,32 +68,32 @@ To download the patterns yourself:
5668
{
5769
"Records": [
5870
{
59-
"messageId": "fa2012345678e816-0a49-4681-ba8f-1234567890",
60-
"receiptHandle": "1234567890NmjC1234567890qODTr1234567890/XPPk/f0qU4tJtQ1234567890ihWDp8YHKhDr3V1234567890e9amjZhgg1234567890RodR1234567890lwDGpf6oLa8/B/1234567890/Pq+xP/1234567890/1234567890fIV6nFUGs71234567890zsj616CBx912M12345678908rxtUEj1234567890J8d1234567890yDcI9E12345678905mTyYZ41S2cP01NCA1234567890jcalHD1234567890Kio+HFQp1234567890OI7bTs5I7pZJ4pu+BnM8Bcki1234567890aNML5B7S12345678904eYKKcrunp1234567890Qhz7BUWPG41",
61-
"body": "Test message",
71+
"messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
72+
"receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a...",
73+
"body": "test",
6274
"attributes": {
6375
"ApproximateReceiveCount": "1",
64-
"SentTimestamp": "1612966720445",
76+
"SentTimestamp": "1545082649183",
6577
"SenderId": "AIDAIENQZJOLO23YVJ4VO",
66-
"ApproximateFirstReceiveTimestamp": "1612966720455"
78+
"ApproximateFirstReceiveTimestamp": "1545082649185"
6779
},
6880
"messageAttributes": {},
69-
"md5OfBody": "82dfa5549ebc91234567890ece5f",
81+
"md5OfBody": "098f6bcd4621d373cade4e832627b4f6",
7082
"eventSource": "aws:sqs",
71-
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:patterns-sqs-to-lambda-MySqsQueue-1234567890",
83+
"eventSourceARN": "arn:aws:sqs:us-east-1:111122223333:my-queue",
7284
"awsRegion": "us-east-1"
7385
}
7486
]
7587
}
7688

7789
```
78-
There is also a sample file `\events\sqs-test-event.json` which contains a sample event payload with 10 items.
90+
There is also a sample file `\events\test-event.json` which contains a sample event payload with 10 items.
7991
8092
### Testing
8193
8294
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:
8395
84-
1. Send 10 messages to the SQS message:
96+
1. Send 10 messages to the SQS queue:
8597
8698
#### Bash
8799
```bash

sqs-lambda-java-sam/pom.xml

Lines changed: 0 additions & 72 deletions
This file was deleted.

sqs-lambda-java-sam/src/main/java/com/example/processor/SQSMessageProcessor.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)