Skip to content

Commit 33eb76d

Browse files
author
Pankaj Agrawal
committed
Example usage with relevant permissions
1 parent 7cce85f commit 33eb76d

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

example/HelloWorldFunction/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
<artifactId>powertools-parameters</artifactId>
3434
<version>0.4.0-beta</version>
3535
</dependency>
36+
<dependency>
37+
<groupId>software.amazon.lambda</groupId>
38+
<artifactId>powertools-sqs</artifactId>
39+
<version>0.4.0-beta</version>
40+
</dependency>
3641
<dependency>
3742
<groupId>com.amazonaws</groupId>
3843
<artifactId>aws-lambda-java-core</artifactId>
@@ -90,6 +95,10 @@
9095
<groupId>software.amazon.lambda</groupId>
9196
<artifactId>powertools-metrics</artifactId>
9297
</aspectLibrary>
98+
<aspectLibrary>
99+
<groupId>software.amazon.lambda</groupId>
100+
<artifactId>powertools-sqs</artifactId>
101+
</aspectLibrary>
93102
</aspectLibraries>
94103
</configuration>
95104
<executions>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package helloworld;
2+
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 org.apache.logging.log4j.LogManager;
7+
import org.apache.logging.log4j.Logger;
8+
import software.amazon.lambda.powertools.logging.PowertoolsLogging;
9+
import software.amazon.lambda.powertools.sqs.SqsBatchProcessor;
10+
import software.amazon.lambda.powertools.sqs.SqsMessageHandler;
11+
12+
import static com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage;
13+
14+
public class AppSqsEvent implements RequestHandler<SQSEvent, String> {
15+
private static final Logger LOG = LogManager.getLogger(AppSqsEvent.class);
16+
17+
@Override
18+
@SqsBatchProcessor(SampleMessageHandler.class)
19+
@PowertoolsLogging(logEvent = true)
20+
public String handleRequest(SQSEvent input, Context context) {
21+
return "{\"statusCode\": 200}";
22+
}
23+
24+
public class SampleMessageHandler implements SqsMessageHandler<Object> {
25+
26+
@Override
27+
public String process(SQSMessage message) {
28+
if("19dd0b57-b21e-4ac1-bd88-01bbb068cb99".equals(message.getMessageId())) {
29+
throw new RuntimeException(message.getMessageId());
30+
}
31+
LOG.info("Processing message with details {}", message);
32+
return message.getMessageId();
33+
}
34+
}
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package helloworld;
2+
3+
import java.util.List;
4+
5+
import com.amazonaws.services.lambda.runtime.Context;
6+
import com.amazonaws.services.lambda.runtime.RequestHandler;
7+
import com.amazonaws.services.lambda.runtime.events.SQSEvent;
8+
import org.apache.logging.log4j.LogManager;
9+
import org.apache.logging.log4j.Logger;
10+
import software.amazon.lambda.powertools.sqs.PowertoolsSqs;
11+
import software.amazon.lambda.powertools.sqs.SQSBatchProcessingException;
12+
13+
import static java.util.Collections.emptyList;
14+
15+
public class AppSqsEventUtil implements RequestHandler<SQSEvent, List<String>> {
16+
private static final Logger LOG = LogManager.getLogger(AppSqsEventUtil.class);
17+
18+
@Override
19+
public List<String> handleRequest(SQSEvent input, Context context) {
20+
try {
21+
22+
return PowertoolsSqs.batchProcessor(input, (message) -> {
23+
if ("19dd0b57-b21e-4ac1-bd88-01bbb068cb99".equals(message.getMessageId())) {
24+
throw new RuntimeException(message.getMessageId());
25+
}
26+
27+
LOG.info("Processing message with details {}", message);
28+
return message.getMessageId();
29+
});
30+
31+
} catch (SQSBatchProcessingException e) {
32+
LOG.info("Exception details {}", e.getMessage(), e);
33+
LOG.info("Success message Returns{}", e.successMessageReturnValues());
34+
LOG.info("Failed messages {}", e.getFailures());
35+
LOG.info("Failed messages Reasons {}", e.getExceptions());
36+
return emptyList();
37+
}
38+
}
39+
}

example/events/eventSqs.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"Records": [
3+
{
4+
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb99",
5+
"receiptHandle": "MessageReceiptHandle",
6+
"body": "Hello from SQS!",
7+
"attributes": {
8+
"ApproximateReceiveCount": "1",
9+
"SentTimestamp": "1523232000000",
10+
"SenderId": "123456789012",
11+
"ApproximateFirstReceiveTimestamp": "1523232000001"
12+
},
13+
"messageAttributes": {},
14+
"md5OfBody": "7b270e59b47ff90a553787216d55d999",
15+
"eventSource": "aws:sqs",
16+
"eventSourceARN": "arn:aws:sqs:eu-west-1:123456789:powertools-example-TestSqsQueue-1JW5W8N9",
17+
"awsRegion": "eu-west-1"
18+
},
19+
{
20+
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
21+
"receiptHandle": "MessageReceiptHandle",
22+
"body": "Hello from SQS!",
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:eu-west-1:123456789:powertools-example-TestSqsQueue-1JW5W8N9",
33+
"awsRegion": "eu-west-1"
34+
}
35+
]
36+
}

example/template.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,57 @@ Resources:
125125
Value: aGVsbG8gd29ybGQ=
126126
Description: Base64 SSM Parameter for lambda-powertools-java powertools-parameters module
127127

128+
TestSqsQueue:
129+
Type: AWS::SQS::Queue
130+
131+
HelloWorldSqsEventFunction:
132+
Type: AWS::Serverless::Function
133+
Properties:
134+
CodeUri: HelloWorldFunction
135+
Handler: helloworld.AppSqsEvent::handleRequest
136+
Runtime: java8
137+
MemorySize: 512
138+
Tracing: Active
139+
Policies:
140+
- Statement:
141+
- Sid: AdditionalPermisssionForPowertoolsSQSUtils
142+
Effect: Allow
143+
Action:
144+
- sqs:GetQueueUrl
145+
- sqs:DeleteMessageBatch
146+
Resource: !GetAtt TestSqsQueue.Arn
147+
Events:
148+
TestSQSEvent:
149+
Type: SQS
150+
Properties:
151+
Queue: !GetAtt TestSqsQueue.Arn
152+
BatchSize: 10
153+
154+
TestAnotherSqsQueue:
155+
Type: AWS::SQS::Queue
156+
157+
HelloWorldSqsEventUtilFunction:
158+
Type: AWS::Serverless::Function
159+
Properties:
160+
CodeUri: HelloWorldFunction
161+
Handler: helloworld.AppSqsEventUtil::handleRequest
162+
Runtime: java8
163+
MemorySize: 512
164+
Tracing: Active
165+
Policies:
166+
- Statement:
167+
- Sid: AdditionalPermisssionForPowertoolsSQSUtils
168+
Effect: Allow
169+
Action:
170+
- sqs:GetQueueUrl
171+
- sqs:DeleteMessageBatch
172+
Resource: !GetAtt TestAnotherSqsQueue.Arn
173+
Events:
174+
TestSQSEvent:
175+
Type: SQS
176+
Properties:
177+
Queue: !GetAtt TestAnotherSqsQueue.Arn
178+
BatchSize: 10
128179

129180
Outputs:
130181
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function

0 commit comments

Comments
 (0)