RabbitMQ Cluster With Terraform On AWS

Avatar placeholderKonrad Rotkiewicz

July 31, 2017

8
 min read
RabbitMQ Cluster With Terraform On AWS

Creating RabbitMQ cluster is tricky. It is easy to do manually and hard if you’d like to automate it.

TL;DR Use this Terraform configuration to create RabbitMQ cluster in less than 5 minutes.

The simplest cluster requires 2 nodes and a load balancer. In AWS we are going to use ELB as a load balancer and put nodes in Auto Scaling group, so that if a node goes down (or became unhealthy) it will be replaced by a new one.

Our setup will be:

rabbitmq
                                                         

Using Terraform we can create Launch Configuration, Auto Scaling group and ELB. This is our ELB configuration:

CODE: https://gist.githubusercontent.com/krotkiewicz/edcc20d0aa1755b4f4673ca8218e1981.js

ELB is configured to replace a node if it becomes unhealthy after 5 minutes (there are 10 checks every 30 seconds). It listens on ports 5672 (AMQP) and 80 proxied to 15672 (HTTP interface).

What about nodes ? We use cloud-init to initialize a node and in there we configure RabbitMQ to run in Docker.

After RabbitMQ is running, the node has to join the cluster. To do that we call rabbitmqctl join_cluster <node> for each of the nodes.

To find out what are the other nodes in the cluster we prepared a bash script that query nodes in our Auto Scaling group:

CODE: https://gist.githubusercontent.com/krotkiewicz/38e724d29522ae9c44bdbffc16fc21e6.js

And then a script to join to these nodes:

CODE: https://gist.githubusercontent.com/krotkiewicz/bf241e3b06d7fea529d8fd2314b2efe1.js

The tricky part here is that to join a cluster, you have to stop the node first. So there is a chance that other node could also be stopped as well. To mitigate this problem we set sleep for some random amount of seconds before stopping the server Also, in case of errors, we perform sane amount of retries.

Last thing that requires explanation is that querying nodes in given Auto Scalinggroup. To to be able to do so you need to associate _IAM_ role with autoscaling:DescribeAutoScalingInstances and ec2:DescribeInstancespolicies. This can be done by our Terraform configuration automatically:

CODE: https://gist.githubusercontent.com/krotkiewicz/4dcbda7a63dc6bfd66ae53d059139a59.js

Using this Terraform configuration we successfully deployed many RabbitMQ clusters with up to 4 nodes.

Leave a comment if you find this useful or a question in case of troubles. Cheers!

Share on

Konrad RotkiewiczAvatar placeholder
Konrad Rotkiewicz
Seasoned Lead Full Stack Python Developer and System Engineer

Konrad has 6 years of strong background in Embedded & Networking programming, as well as experience leading multiple teams for UK and US customers, which has laid the foundations for him to become a natural Technology leader. A man obsessed with continuous improvements, Konrad is never satisfied with his results.

Read more posts →

Newsletter

Subscribe to our quarterly newsletter

Thank you! Check your email for a confirmation link.
Oops! Something went wrong while submitting the form.

Are you looking for a job?

Great, we're looking for tech-savvy people!

Go to careers

Tell us about your project

Get in touch and let’s build your project!
Contact us
White arrow right