generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 100
docs(examples): add SAM example for large-messages module #2319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sasanchezramirez
wants to merge
4
commits into
aws-powertools:main
Choose a base branch
from
sasanchezramirez:docs/1353-large-messages-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bba1a8b
docs(examples): initial scaffolding for large-messages module
sasanchezramirez 9b2e860
docs(examples): add SAM template with S3 and SQS resources
sasanchezramirez 0be4bd8
docs(examples): complete large-messages example implementation
sasanchezramirez 44cf89b
docs(examples): add producer tool and address PR feedback
sasanchezramirez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # 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.aws.amazon.com/powertools/java/latest/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. | ||
|
|
||
| ### Run the Large Message Producer | ||
|
|
||
| To test the handling of large messages involving S3 offloading, verify you have the SQS Queue URL and the S3 Bucket name created by the stack. | ||
|
|
||
| 1. **Build the producer tool:** | ||
| ```bash | ||
| cd examples/powertools-examples-large-messages/tools | ||
| mvn compile | ||
| ``` | ||
|
|
||
| 2. **Run the producer:** | ||
| ```bash | ||
| mvn exec:java -Dexec.args="<SQS_QUEUE_URL> <S3_BUCKET_NAME>" | ||
| ``` | ||
| Replace `<SQS_QUEUE_URL>` and `<S3_BUCKET_NAME>` with the output values from the deployment. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>software.amazon.lambda.examples</groupId> | ||
| <artifactId>powertools-examples-large-messages</artifactId> | ||
| <version>2.8.0</version> | ||
| <name>Powertools for AWS Lambda (Java) - Examples - Large Messages</name> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>software.amazon.lambda</groupId> | ||
| <artifactId>powertools-large-messages</artifactId> | ||
| <version>${project.version}</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>com.amazonaws</groupId> | ||
| <artifactId>aws-lambda-java-events</artifactId> | ||
| <version>3.11.4</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>software.amazon.lambda</groupId> | ||
| <artifactId>powertools-logging-log4j</artifactId> | ||
| <version>${project.version}</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.aspectj</groupId> | ||
| <artifactId>aspectjrt</artifactId> | ||
| <version>${aspectj.version}</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-deploy-plugin</artifactId> | ||
| <version>3.1.4</version> | ||
| <configuration> | ||
| <skip>true</skip> | ||
| </configuration> | ||
| </plugin> | ||
|
|
||
| <plugin> | ||
| <groupId>dev.aspectj</groupId> | ||
| <artifactId>aspectj-maven-plugin</artifactId> | ||
| <version>1.14</version> | ||
| <configuration> | ||
| <source>${maven.compiler.source}</source> | ||
| <target>${maven.compiler.target}</target> | ||
| <complianceLevel>${maven.compiler.target}</complianceLevel> | ||
| <aspectLibraries> | ||
| <aspectLibrary> | ||
| <groupId>software.amazon.lambda</groupId> | ||
| <artifactId>powertools-logging</artifactId> | ||
| </aspectLibrary> | ||
| </aspectLibraries> | ||
| </configuration> | ||
| <executions> | ||
| <execution> | ||
| <goals> | ||
| <goal>compile</goal> | ||
| </goals> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
|
|
||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-shade-plugin</artifactId> | ||
| <version>3.6.1</version> | ||
| <executions> | ||
| <execution> | ||
| <phase>package</phase> | ||
| <goals> | ||
| <goal>shade</goal> | ||
| </goals> | ||
| <configuration> | ||
| <createDependencyReducedPom>false</createDependencyReducedPom> | ||
| <transformers> | ||
| <transformer | ||
| implementation="org.apache.logging.log4j.maven.plugins.shade.transformer.Log4j2PluginCacheFileTransformer" /> | ||
| </transformers> | ||
| </configuration> | ||
| </execution> | ||
| </executions> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.apache.logging.log4j</groupId> | ||
| <artifactId>log4j-transform-maven-shade-plugin-extensions</artifactId> | ||
| <version>0.1.0</version> | ||
| </dependency> | ||
| </dependencies> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
49 changes: 49 additions & 0 deletions
49
examples/powertools-examples-large-messages/src/main/java/helloworld/App.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * 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.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import software.amazon.lambda.powertools.largemessages.LargeMessages; | ||
| import software.amazon.lambda.powertools.logging.Logging; | ||
|
|
||
| /** | ||
| * Example handler showing how to use LargeMessageProcessor functionally. | ||
| * This approach gives you more control than the @LargeMessage annotation. | ||
| */ | ||
| public final class App implements RequestHandler<SQSEvent, String> { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(App.class); | ||
|
|
||
| @Logging | ||
| @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"; | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
examples/powertools-examples-large-messages/src/main/resources/log4j2.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <Configuration> | ||
| <Appenders> | ||
| <Console name="JsonAppender" target="SYSTEM_OUT"> | ||
| <JsonTemplateLayout eventTemplateUri="classpath:LambdaJsonLayout.json" /> | ||
| </Console> | ||
| </Appenders> | ||
| <Loggers> | ||
| <Logger name="JsonLogger" level="INFO" additivity="false"> | ||
| <AppenderRef ref="JsonAppender" /> | ||
| </Logger> | ||
| <Root level="info"> | ||
| <AppenderRef ref="JsonAppender" /> | ||
| </Root> | ||
| </Loggers> | ||
| </Configuration> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>software.amazon.lambda.examples</groupId> | ||
| <artifactId>powertools-examples-large-messages-tools</artifactId> | ||
| <version>1.0.0</version> | ||
|
|
||
| <properties> | ||
| <maven.compiler.source>11</maven.compiler.source> | ||
| <maven.compiler.target>11</maven.compiler.target> | ||
| <aws.sdk.version>2.29.0</aws.sdk.version> | ||
| <sqs-extended.version>2.1.2</sqs-extended.version> | ||
| </properties> | ||
|
|
||
| <dependencyManagement> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>software.amazon.awssdk</groupId> | ||
| <artifactId>bom</artifactId> | ||
| <version>${aws.sdk.version}</version> | ||
| <type>pom</type> | ||
| <scope>import</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </dependencyManagement> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.amazonaws</groupId> | ||
| <artifactId>amazon-sqs-java-extended-client-lib</artifactId> | ||
| <version>${sqs-extended.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>software.amazon.awssdk</groupId> | ||
| <artifactId>s3</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>software.amazon.awssdk</groupId> | ||
| <artifactId>sqs</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.slf4j</groupId> | ||
| <artifactId>slf4j-simple</artifactId> | ||
| <version>2.0.16</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.codehaus.mojo</groupId> | ||
| <artifactId>exec-maven-plugin</artifactId> | ||
| <version>3.1.0</version> | ||
| <configuration> | ||
| <mainClass>software.amazon.lambda.powertools.largemessages.tools.LargeMessageProducer</mainClass> | ||
| <classpathScope>compile</classpathScope> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not forgot to close the markdown block again.