This pattern provides an example on how to implement multi-region message replication with Amazon MQ for RabbitMQ. This solution demonstrates how to configure Amazon MQ for RabbitMQ leveraging the federation plugin across regions. You will learn how federation of exchanges enable asynchronous data replication between two RabbitMQ clusters to achieve multi-region architecture. This code base is deployed using Terraform to simplify the Rabbit MQ configuration.
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/activemq-rabbitmq-message-replication-terraform
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
- Terraform installed
In RabbitMQ, federation is a feature that allows one broker to federate an exchange to another broker. Federated exchanges copy messages from upstream broker exchanges to downstream broker exchanges. This solution use the RabbitMQ federation plugin to enable federation at exchange level and replicate messages from one region to another region. Also, Time to live(TTL) of the replicate message is set 60 seconds, to automatically expire the messages.In the unlikely event of a regional degradation or outage, you can switch application from one region to another region and start processing the messages from the RabbitMQ broker.
Note:
- In the event of switching application from one region to another region, your application need handle duplicates messages or be idempotent
- You cannot configure federation if the upstream queue or exchange is in a private broker. You can only configure federation between queues or exchanges in public brokers, or between an upstream queue or exchange in a public broker, and a downstream queue or exchange in a private broker.
Figure 1 – Architecture of Federated Amazon MQ Brokers
The following variables are needed for deployment of this code base and can be adjust
Parameter | Default Value | Description |
---|---|---|
env_name |
demo | The name of your stack, e.g. "demo" |
region |
us-east-1 | The AWS region in which primary resources are created and verifying HMAC tags. |
sec_region |
us-east-2 | The AWS region in which secondary resources are created |
user |
exampleuser | Admin user for the brokers |
password |
examplepassword | Admin user password |
regex |
^amq\. | Regular expression filter for federation |
ttl |
60000 | Time to live of messages in the second region, default is set low for demo purposes to see queue clear (60 seconds) |
The following locals varible is used to strcuture the queue policy from the provided ttl
variable above. This should not be directly modified
Local | Default Value | Description |
---|---|---|
queue_policy |
{"x-message-ttl": ${var.ttl}} | The JSON policy to set queue TTL on secondary queue |
-
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 amazonmq-rabbitmq-msg-replication
-
Customize the variables in
variable.tf
to match your deployment.Note: You do not need to adjust any variables, you can run this with default variables as is
-
Initialize the repository:
terraform init
-
Apply the infrastructure.
terraform apply
-
During the prompts.
- Enter yes
- Note: This deployement takes about 15 minutes to complete
-
Note the outputs from the deployment process. These contain the resource names which are used for testing.
-
Go to AWS console let’s verify that both brokers have been deployed
a. If you did not change the regions they will be in
us-east-1
andus-east-2
-
Take the terraform outputs for the admin consoles for both brokers.
a. Alternatively, you can get them from the AWS console in each region for the respective broker
-
Login to the
primary
andsecondary
brokersa. If you did not change the variables then the login will be:
i. User: exampleuser
ii. Password: examplepassword
-
On the
secondary
broker validate that federation is already establisheda. Click on the
Admin
tabb. Once on the
Admin
page click onFederation Status
on the right-hand sidec. Once loaded there will be the
primary_federation
connection listed 4 times for each of the existingamq
brokers:amq.direct
,amq.fanout
,amq.headers
,amq.match
,amq.topic
Figure 2 – Federation Status on Secondary Broker
-
On the
secondary
broker validate that thedemo
queue exisitsa. Click on the
Queues
tabb. Once loaded there will be the
demo
queueFigure 3 – Demo Queue on Secondary Broker
c. Click on
demo
to the drill into the queue and validate the binding to theamq.direct
exchangeFigure 4 – Confirmed Binding on Demo Queue
Sending Message to Primary Broker
-
Logged into the
primary
cluster, send a message to theamq.direct
exchangea. Click on the
Exchanges
tabb. Once loaded click on the
amq.direct
exchangec. Once loaded click on
Publish Message
to expand aread. Publish a message matching the below screenshot
i. Note: If you do not have the
#
routing key the message will not show up indemo
queue on the secondary clustere. Click on the gray
Publish Message
buttonFigure 5 – Test Message ready to publish on Primary Broker
Validating Message Was Received on Demo Queue
-
Validate the message showed up in
demo
queuea. Switch over to the
secondary
cluster or log back if logged outb. Click on the
Queues
tabc. Once loaded there will be the
demo
queued. On the table view under
Ready
there will be the number1
indicating that the message has been received and is ready for processing.Figure 6 – Message on demo queue on Secondary Broker
To avoid incurring future charges, delete the resources created by the Terraform script.
- Return to the directory where you deployed your terraform script.
- To destroy the infrastructure in AWS, run the command
When prompted do you want to destroy the infrastructure, type
terraform destroy
Y
and press enter.
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0