This sample project demonstrates event driven message processing from an Amazon MQ ActiveMQ queue using an AWS Lambda function written in Java.
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/activemq-lambda-sam-java
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 for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
- Create an AWS account 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.
- AWS CLI installed and configured
- Git Installed
- AWS Serverless Application Model (AWS SAM) installed
- Java 21 or above installed
- Maven 3.9.6 or above installed
-
Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
git clone https://github.com/aws-samples/serverless-patterns
-
Change directory to the pattern directory:
cd serverless-patterns/activemq-lambda-sam-java
-
From the command line, execute the below command to build the Java based AWS Lambda function.
sam build
-
From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
sam deploy --guided
-
During the prompts:
- Enter a stack name
- Enter the desired AWS Region (e.g. us-east-1).
- Enter a username. User Name can't contain commas (,), colons (:), equals signs (=), or spaces.
- Enter a password. Password must be minimum 12 characters, at least 4 unique characters. Can't contain commas (,), colons (:), equals signs (=), spaces or non-printable ASCII characters.
- Allow SAM CLI to create IAM roles with the required permissions.
- Keep default values to the rest of the parameters.
Once you have run
sam deploy --guided
mode once and saved arguments to a configuration file (samconfig.toml), you can usesam deploy
in future to use these defaults. -
Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for next step as well as testing.
Please refer to the architecture diagram below:
Here's a breakdown of the steps:
-
Amazon MQ: An ActiveMQ single-instance broker is provisioned. A test queue with name
MyTestQueue
is created. A message is pushed to the queue. -
AWS Lambda: AWS Lambda has an event source mapping (ESM) configured for
MyTestQueue
queue. The Lambda function is triggered by the new message on the Amazon MQ queue. The Lambda function processes the message and logs the decoded the message content.
-
Log into the ActiveMQ Web Console using the
username
andpassword
provided at the time of deployment. The console URL is available in theActiveMQWebConsole
key ofsam deploy
output. -
Click on the
Queues
tab to open the list of queues. -
Click on the
Send To
link underOperations
tab next to theMyTestQueue
queue. -
Enter a
Message Body
and clickSend
button. -
Execute the below command to tail logs of the AWS Lambda function. Please replace
MyMQMessageHandlerFunction
from thesam deploy -g
output and also your region.aws logs tail --follow /aws/lambda/{MyMQMessageHandlerFunction} --region {your-region}
-
Check the AWS Lambda console log. It should print the message from the Amazon MQ queue.
-
Press
Ctrl + c
to stop tailing the logs.
- To delete the resources deployed to your AWS account via AWS SAM, run the following command:
sam delete
Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0