|
| 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 | + |
| 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 |
0 commit comments