Skip to content

Commit ae11f3e

Browse files
authored
Merge pull request #2279 from biswanathmukherjee/biswanathmukherjee-feature-rabbitmq-lambda
Event driven Amazon MQ RabbitMQ message processing using AWS Lambda (Java)
2 parents b5786d7 + 74e5284 commit ae11f3e

File tree

7 files changed

+443
-0
lines changed

7 files changed

+443
-0
lines changed

rabbitmq-lambda-sam-java/README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Event driven Amazon MQ for RabbitMQ message processing using AWS Lambda (Java)
2+
3+
This sample project demonstrates event driven message processing from an Amazon MQ for RabbitMQ queue using an AWS Lambda function written in Java.
4+
5+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/rabbitmq-lambda-sam-java
6+
7+
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.
8+
9+
## Requirements
10+
11+
- [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) 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.
12+
- [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
13+
- [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
14+
- [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed
15+
- [Java 21 or above](https://docs.aws.amazon.com/corretto/latest/corretto-21-ug/downloads-list.html) installed
16+
- [Maven 3.9.6 or above](https://maven.apache.org/download.cgi) installed
17+
18+
19+
20+
## Deployment Instructions
21+
22+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
23+
```bash
24+
git clone https://github.com/aws-samples/serverless-patterns
25+
```
26+
27+
2. Change directory to the pattern directory:
28+
```bash
29+
cd serverless-patterns/rabbitmq-lambda-sam-java
30+
```
31+
32+
3. From the command line, execute the below command to build the Java based AWS Lambda function.
33+
```bash
34+
sam build
35+
```
36+
37+
4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
38+
```bash
39+
sam deploy --guided
40+
```
41+
4. During the prompts:
42+
43+
- Enter a stack name
44+
- Enter the desired AWS Region (e.g. us-east-1).
45+
- Enter a username. User Name can't contain commas (,), colons (:), equals signs (=), or spaces.
46+
- 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.
47+
- Allow SAM CLI to create IAM roles with the required permissions.
48+
- Keep default values to the rest of the parameters.
49+
50+
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.
51+
52+
5. 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.
53+
54+
## How it works
55+
56+
Please refer to the architecture diagram below:
57+
58+
![End to End Architecture](images/architecture.png)
59+
60+
Here's a breakdown of the steps:
61+
62+
1. **Amazon MQ**: A RabbitMQ single-instance broker is provisioned. A test queue with name `MyTestQueue` is created. A message is pushed to the queue.
63+
64+
2. **AWS Lambda**: An AWS Lambda function with an event source mapping (ESM) configured for `MyTestQueue` queue is created. The Lambda function is triggered when a new message is added to the queue. The Lambda function processes the message and logs the decoded the message content.
65+
66+
67+
## Testing
68+
69+
1. Log into the RabbitMQ Web Console using the `username` and `password` provided at the time of deployment. The console URL is available in the`RabbitMQWebConsole` key of `sam deploy` output.
70+
71+
2. Go to the `Queues and Streams` tab and click on `Add a new queue` link.
72+
73+
3. Set the `Name` field to `MyTestQueue` and click on the `Add queue` button. The new queue should be added and shortly show in a table. Click on the `MyTestQueue`. This will take you to the `Queue MyTestQueue` details page.
74+
75+
4. Click on the `Publish message` link.
76+
77+
5. Enter a `Payload` and click `Publish message` button.
78+
79+
6. Execute the below command to tail logs of the AWS Lambda function. Replace `MyMQMessageHandlerFunction` with the function name from output of the `sam deploy -g` command. Replace `your-region` with the region where you are deploying this pattern.
80+
```bash
81+
aws logs tail --follow /aws/lambda/{MyMQMessageHandlerFunction} --region {your-region}
82+
```
83+
84+
7. Check the AWS Lambda console log. It should print the message from the Amazon MQ queue.
85+
86+
8. Press `Ctrl + c` to stop tailing the logs.
87+
88+
89+
## Cleanup
90+
91+
1. To delete the resources deployed to your AWS account via AWS SAM, run the following command:
92+
93+
```bash
94+
sam delete
95+
```
96+
97+
98+
---
99+
100+
Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
101+
102+
SPDX-License-Identifier: MIT-0
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"title": "Event driven Amazon MQ for RabbitMQ message processing using AWS Lambda (Java)",
3+
"description": "This sample project demonstrates event driven message processing from an Amazon MQ for RabbitMQ queue using an AWS Lambda function written in Java.",
4+
"language": "Java",
5+
"level": "200",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"An Amazon MQ for RabbitMQ single-instance broker is provisioned. A test queue with name MyTestQueue is created. A message is pushed to the queue.",
11+
"An AWS Lambda function 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."
12+
]
13+
},
14+
"gitHub": {
15+
"template": {
16+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/rabbitmq-lambda-sam-java",
17+
"templateURL": "serverless-patterns/rabbitmq-lambda-sam-java",
18+
"projectFolder": "rabbitmq-lambda-sam-java",
19+
"templateFile": "template.yaml"
20+
}
21+
},
22+
"resources": {
23+
"bullets": [
24+
{
25+
"text": "Using Lambda with Amazon MQ",
26+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/with-mq.html"
27+
},
28+
{
29+
"text": "Creating and connecting to a RabbitMQ broker",
30+
"link": "https://docs.aws.amazon.com/amazon-mq/latest/developer-guide/getting-started-rabbitmq.html"
31+
}
32+
]
33+
},
34+
"deploy": {
35+
"text": [
36+
"sam build",
37+
"sam deploy --guided"
38+
]
39+
},
40+
"testing": {
41+
"text": [
42+
"See the GitHub repo for detailed testing instructions."
43+
]
44+
},
45+
"cleanup": {
46+
"text": [
47+
"Delete the stack: <code>sam delete</code>."
48+
]
49+
},
50+
"authors": [
51+
{
52+
"name": "Biswanath Mukherjee",
53+
"image": "https://d1rwvjey2iif32.cloudfront.net",
54+
"bio": "I am a Sr. Solutions Architect working at AWS India.",
55+
"linkedin": "biswanathmukherjee"
56+
},
57+
{
58+
"name": "Rakshith Rao",
59+
"image": "https://rao.sh/assets/img/profile_pic.png",
60+
"bio": "I am a Senior Solutions Architect at AWS and help our strategic customers build and operate their key workloads on AWS.",
61+
"linkedin": "rakshithrao"
62+
}
63+
]
64+
}
10.5 KB
Loading

rabbitmq-lambda-sam-java/pom.xml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.example</groupId>
7+
<artifactId>rabbitmq-lambda-sam-java</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
<name>Amazon MQ ActiveMQ to AWS Lambda</name>
11+
<properties>
12+
<maven.compiler.source>21</maven.compiler.source>
13+
<maven.compiler.target>21</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencyManagement>
18+
<dependencies>
19+
<dependency>
20+
<groupId>software.amazon.awssdk</groupId>
21+
<artifactId>bom</artifactId>
22+
<version>2.25.38</version>
23+
<type>pom</type>
24+
<scope>import</scope>
25+
</dependency>
26+
</dependencies>
27+
</dependencyManagement>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>com.amazonaws</groupId>
32+
<artifactId>aws-lambda-java-core</artifactId>
33+
<version>1.2.3</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.amazonaws</groupId>
37+
<artifactId>aws-lambda-java-events</artifactId>
38+
<version>3.11.3</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>software.amazon.awssdk</groupId>
42+
<artifactId>aws-core</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.rabbitmq</groupId>
46+
<artifactId>amqp-client</artifactId>
47+
<version>5.21.0</version>
48+
</dependency>
49+
50+
</dependencies>
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-surefire-plugin</artifactId>
56+
<version>3.0.0-M5</version>
57+
</plugin>
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-shade-plugin</artifactId>
61+
<version>3.2.4</version>
62+
<configuration>
63+
</configuration>
64+
<executions>
65+
<execution>
66+
<phase>package</phase>
67+
<goals>
68+
<goal>shade</goal>
69+
</goals>
70+
</execution>
71+
</executions>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
</project>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{
2+
"title": "Event driven Amazon MQ for RabbitMQ message processing using AWS Lambda",
3+
"description": "This sample project demonstrates event driven message processing from an Amazon MQ for RabbitMQ queue using an AWS Lambda function written in Java.",
4+
"language": "Java",
5+
"level": "200",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"An Amazon MQ for RabbitMQ single-instance broker is provisioned. A test queue with name MyTestQueue is created. A message is pushed to the queue.",
11+
"An AWS Lambda function 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."
12+
]
13+
},
14+
"gitHub": {
15+
"template": {
16+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/rabbitmq-lambda-sam-java",
17+
"templateURL": "serverless-patterns/rabbitmq-lambda-sam-java",
18+
"projectFolder": "rabbitmq-lambda-sam-java",
19+
"templateFile": "template.yaml"
20+
}
21+
},
22+
"resources": {
23+
"bullets": [
24+
{
25+
"text": "Using Lambda with Amazon MQ",
26+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/with-mq.html"
27+
},
28+
{
29+
"text": "Creating and connecting to a RabbitMQ broker",
30+
"link": "https://docs.aws.amazon.com/amazon-mq/latest/developer-guide/getting-started-rabbitmq.html"
31+
}
32+
]
33+
},
34+
"deploy": {
35+
"text": [
36+
"sam build",
37+
"sam deploy --guided"
38+
]
39+
},
40+
"testing": {
41+
"text": [
42+
"See the GitHub repo for detailed testing instructions."
43+
]
44+
},
45+
"cleanup": {
46+
"text": [
47+
"Delete the stack: <code>sam delete</code>."
48+
]
49+
},
50+
"authors": [
51+
{
52+
"name": "Biswanath Mukherjee",
53+
"image": "https://d1rwvjey2iif32.cloudfront.net",
54+
"bio": "I am a Sr. Solutions Architect working at AWS India.",
55+
"linkedin": "biswanathmukherjee"
56+
},
57+
{
58+
"name": "Rakshith Rao",
59+
"image": "https://rao.sh/assets/img/profile_pic.png",
60+
"bio": "I am a Senior Solutions Architect at AWS and help our strategic customers build and operate their key workloads on AWS.",
61+
"linkedin": "rakshithrao"
62+
}
63+
],
64+
"patternArch": {
65+
"icon1": {
66+
"x": 20,
67+
"y": 50,
68+
"service": "apigw",
69+
"label": "Amazon MQ"
70+
},
71+
"icon2": {
72+
"x": 80,
73+
"y": 50,
74+
"service": "lambda",
75+
"label": "AWS Lambda"
76+
},
77+
"line1": {
78+
"from": "icon1",
79+
"to": "icon2",
80+
"label": "event source mapping"
81+
}
82+
}
83+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.example;
2+
3+
import com.amazonaws.services.lambda.runtime.Context;
4+
import com.amazonaws.services.lambda.runtime.LambdaLogger;
5+
import com.amazonaws.services.lambda.runtime.RequestHandler;
6+
import com.amazonaws.services.lambda.runtime.events.RabbitMQEvent;
7+
import com.amazonaws.services.lambda.runtime.events.RabbitMQEvent.RabbitMessage;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Base64;
11+
12+
13+
public class MyMQMessageHandlerFunction implements RequestHandler<RabbitMQEvent, Void> {
14+
15+
16+
public Void handleRequest(RabbitMQEvent event, Context context) {
17+
18+
LambdaLogger logger = context.getLogger();
19+
logger.log("Received RabbitMQ event: " + event);
20+
21+
Map<String, List<RabbitMessage>> queueMap = event.getRmqMessagesByQueue();
22+
for (Map.Entry<String, List<RabbitMessage>> entry : queueMap.entrySet()) {
23+
logger.log("Queue name: " + entry.getKey());
24+
for (RabbitMessage message : entry.getValue()) {
25+
byte[] decodedBytes = Base64.getDecoder().decode(message.getData());
26+
logger.log("Message received: " + new String(decodedBytes));
27+
}
28+
}
29+
30+
return null;
31+
32+
}
33+
}

0 commit comments

Comments
 (0)