diff --git a/examples/pom.xml b/examples/pom.xml index 511cdd5a9..1fdbd85aa 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -45,6 +45,7 @@ powertools-examples-batch powertools-examples-validation powertools-examples-cloudformation + powertools-examples-large-messages diff --git a/examples/powertools-examples-large-messages/README.md b/examples/powertools-examples-large-messages/README.md new file mode 100644 index 000000000..528ad2c5c --- /dev/null +++ b/examples/powertools-examples-large-messages/README.md @@ -0,0 +1,27 @@ +# Powertools for AWS Lambda (Java) - Large Messages Example + +This project contains an example of a Lambda function using the **Large Messages** module of Powertools for AWS Lambda (Java). For more information on this module, please refer to the [documentation](https://docs.powertools.aws.dev/lambda-java/utilities/large_messages/). + +The example demonstrates an SQS listener that processes messages using the `LargeMessages` functional utility. It handles the retrieval of large payloads offloaded to S3 automatically. + +## Deploy the sample application + +This sample is based on Serverless Application Model (SAM). To deploy it, check out the instructions for getting +started with SAM in [the examples directory](../README.md). + +## Test the application + +Since this function is triggered by an SQS Queue, you can test it by sending a message to the queue created by the SAM template. + +1. **Find your Queue URL:** + Run the following command (replacing `LargeMessageExample` with the name of your deployed stack): + ```bash + aws cloudformation describe-stacks --stack-name LargeMessageExample --query "Stacks[0].Outputs[?OutputKey=='QueueURL'].OutputValue" --output text + +2. **Send a Test Message:** + Note: To test the actual "Large Message" functionality (payload offloading), you would typically use the SQS Extended Client in a producer application. However, you can verify the Lambda trigger with a standard message: + ```bash + aws sqs send-message --queue-url [YOUR_QUEUE_URL] --message-body '{"message": "Hello from CLI"}' + +3. **Verify Logs:** + Go to AWS CloudWatch Logs and check the Log Group for your function. You should see the processed message logged by the application. \ No newline at end of file diff --git a/examples/powertools-examples-large-messages/pom.xml b/examples/powertools-examples-large-messages/pom.xml new file mode 100644 index 000000000..9b2c92f0a --- /dev/null +++ b/examples/powertools-examples-large-messages/pom.xml @@ -0,0 +1,113 @@ + + 4.0.0 + + software.amazon.lambda.examples + powertools-examples-large-messages + 2.8.0 + Powertools for AWS Lambda (Java) - Examples - Large Messages + + + 11 + 11 + 2.21.1 + 1.9.21 + 2.1.1 + + + + + software.amazon.lambda + powertools-large-messages + ${project.version} + + + + com.amazonaws + amazon-sqs-java-extended-client-lib + ${amazon-sqs-java-extended-client-lib.version} + + + + com.amazonaws + aws-lambda-java-events + 3.11.4 + + + + software.amazon.lambda + powertools-logging-log4j + ${project.version} + + + org.aspectj + aspectjrt + ${aspectj.version} + + + + + + + org.apache.maven.plugins + maven-deploy-plugin + 3.1.4 + + true + + + + + dev.aspectj + aspectj-maven-plugin + 1.14 + + ${maven.compiler.source} + ${maven.compiler.target} + ${maven.compiler.target} + + + software.amazon.lambda + powertools-logging + + + + + + + compile + + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.6.1 + + + package + + shade + + + false + + + + + + + + + org.apache.logging.log4j + log4j-transform-maven-shade-plugin-extensions + 0.1.0 + + + + + + \ No newline at end of file diff --git a/examples/powertools-examples-large-messages/src/main/java/helloworld/App.java b/examples/powertools-examples-large-messages/src/main/java/helloworld/App.java new file mode 100644 index 000000000..275d0ad3c --- /dev/null +++ b/examples/powertools-examples-large-messages/src/main/java/helloworld/App.java @@ -0,0 +1,47 @@ +/* + * Copyright 2023 Amazon.com, Inc. or its affiliates. + * Licensed under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package helloworld; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; +import com.amazonaws.services.lambda.runtime.events.SQSEvent; +import com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import software.amazon.lambda.powertools.largemessages.LargeMessages; + +/** + * Example handler showing how to use LargeMessageProcessor functionally. + * This approach gives you more control than the @LargeMessage annotation. + */ +public final class App implements RequestHandler { + + private static final Logger LOG = LogManager.getLogger(App.class); + + @Override + public String handleRequest(final SQSEvent event, final Context context) { + LOG.info("Received event with {} records", event.getRecords().size()); + + for (SQSMessage message : event.getRecords()) { + LargeMessages.processLargeMessage(message, (processedMessage) -> { + LOG.info("Processing message ID: {}", processedMessage.getMessageId()); + LOG.info("Processing body content: {}", processedMessage.getBody()); + return "Processed"; + }); + } + + return "SUCCESS"; + } +} diff --git a/examples/powertools-examples-large-messages/src/main/resources/log4j2.xml b/examples/powertools-examples-large-messages/src/main/resources/log4j2.xml new file mode 100644 index 000000000..5dede7b58 --- /dev/null +++ b/examples/powertools-examples-large-messages/src/main/resources/log4j2.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/examples/powertools-examples-large-messages/template.yaml b/examples/powertools-examples-large-messages/template.yaml new file mode 100644 index 000000000..4fc3368a6 --- /dev/null +++ b/examples/powertools-examples-large-messages/template.yaml @@ -0,0 +1,62 @@ +AWSTemplateFormatVersion: "2010-09-09" +Transform: AWS::Serverless-2016-10-31 +Description: > + Large Message Handling Example using SQS and S3 offloading + +Globals: + Function: + Timeout: 30 + Runtime: java11 + MemorySize: 512 + Tracing: Active + Environment: + Variables: + JAVA_TOOL_OPTIONS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1" + POWERTOOLS_LOG_LEVEL: INFO + POWERTOOLS_SERVICE_NAME: LargeMessageExample + +Resources: + LargeMessageBucket: + Type: AWS::S3::Bucket + Properties: + LifecycleConfiguration: + Rules: + - Id: DeleteOldMessages + Status: Enabled + ExpirationInDays: 1 + MyLargeMessageQueue: + Type: AWS::SQS::Queue + Properties: + VisibilityTimeout: 30 + LargeMessageProcessingFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: . + Handler: helloworld.App::handleRequest + + Policies: + - S3CrudPolicy: + BucketName: !Ref LargeMessageBucket + - SQSPollerPolicy: + QueueName: !GetAtt MyLargeMessageQueue.QueueName + + Environment: + Variables: + POWERTOOLS_LARGE_MESSAGES_BUCKET: !Ref LargeMessageBucket + + Events: + SQSEvent: + Type: SQS + Properties: + Queue: !GetAtt MyLargeMessageQueue.Arn + BatchSize: 1 +Outputs: + LargeMessageBucketName: + Description: "S3 Bucket for large payloads" + Value: !Ref LargeMessageBucket + QueueURL: + Description: "SQS Queue URL" + Value: !Ref MyLargeMessageQueue + FunctionArn: + Description: "Lambda Function ARN" + Value: !GetAtt LargeMessageProcessingFunction.Arn