AWS Auto scale Instance-Based on RabbitMQ Custom Metrics

Posted on Oct 11, 2023

AWS Auto scale Instance-Based on RabbitMQ Custom Metrics

In this article, we will go over how to auto-scale in/out an AWS instance using RabbitMQ metrics using Terraform.

Prerequisite:

In a Domain Driven Design architecture, we are using RabbitMQ to publish messages. In RabbitMQ, each subscriber is a queue that binds to different events from different domains.

We have a RabbitMQ and workers deployed across multiple EC2 instances. So, we wanted to implement auto-scaling, which will add machines to burn-down queues with many messages — this is critical for the business to achieve fast eventual consistency and reduce RabbitMQ load operationally.

Problem:

Handling events in handlers can range from quick and light to CPU-intensive operations that take several minutes. AWS’s auto-scaling group enables simple configuration based on CloudWatch metrics. The problem is that we couldn't find a metric that would indicate the need for additional worker nodes to scale in/out the worker nodes.


Solution:

To scale in/out our worker machines, we created custom CloudWatch metrics and alerts based on them.

Please follow this article for this: Ingesting and monitoring custom metrices in CloudWatch with AWS Lambda.

The final step is to set up the auto-scaling group to scale in and out based on this metric using Terraform.

The launch template for this would be like:

resource "aws_launch_template" "launch_template" {
   name_prefix = "worker-node"
   image_id = var.ami_id
  iam_instance_profile {
    arn = aws_iam_instance_profile.instance_profile.arn
  }
  monitoring {
    enabled = true
  }
  instance_type                        = var.instance_type
  instance_initiated_shutdown_behavior = "terminate"
  key_name                             = var.key_name
  vpc_security_group_ids               = [aws_security_group.instance.id]
  tag_specifications {
    resource_type = "instance"
    tags = {
      Name = "Worker-server"
    }
  }
  lifecycle {
    create_before_destroy = true
  }
}

Create autoscaling group as:

resource "aws_autoscaling_group" "asg" {
  name_prefix      = "worker-node"
  min_size         = var.min_size
  max_size         = var.max_size
  desired_capacity = var.desired_capacity
  launch_template {
    id      = aws_launch_template.launch_template.id
    version = aws_launch_template.launch_template.latest_version
  }
  vpc_zone_identifier = var.private_subnets
  instance_refresh {
    strategy = "Rolling"
    preferences {
      min_healthy_percentage = 50
    }
  }
 lifecycle {
    create_before_destroy = true
  }
  tag {
    key                 = "Name"
    value               = "Worker-server"
    propagate_at_launch = true
  }
  force_delete = true
}

Creating scale-up CloudWatch alarm:

resource "aws_autoscaling_policy" "scale_up_using_q" {
  name                   = "worker-node-scale_up_using_q"
  autoscaling_group_name = aws_autoscaling_group.asg.name
  adjustment_type        = "ChangeInCapacity"
  scaling_adjustment     = 1
  cooldown               = 300
}
resource "aws_cloudwatch_metric_alarm" "scale_up_using_q" {
  alarm_name          = "worker-node-scale_up_using_q"
  alarm_description   = "Monitors RabbitMQ queue size for server ASG"
  alarm_actions       = [aws_autoscaling_policy.scale_up_using_q.arn]
  comparison_operator = "GreaterThanOrEqualToThreshold"
  namespace           = "QueueMetrics"
  metric_name         = "TotalMessages"
  threshold           = var.KPI/var.Avgprocessing-time
  evaluation_periods  = "1"
  period              = "300"
  statistic           = "Average"
}

Creating scale-down CloudWatch alarm:

#scale down using queue size
resource "aws_autoscaling_policy" "scale_down_using_q" {
  name                   = "worker-node-scale_down_using_q"
  autoscaling_group_name = aws_autoscaling_group.asg.name
  adjustment_type        = "ChangeInCapacity"
  scaling_adjustment     = -1
  cooldown               = 300
}
resource "aws_cloudwatch_metric_alarm" "scale_down_using_q" {
  alarm_name          = "worker-node-scale_down_using_q"
  alarm_description   = "Monitors RabbitMQ queue size for server ASG"
  alarm_actions       = [aws_autoscaling_policy.scale_down_using_q.arn]
  comparison_operator = "LessThanThreshold"
  namespace           = "QueueMetrics"
  metric_name         = "TotalMessages"
  threshold           = var.kpi/var.avgProcessing-time
  evaluation_periods  = "1"
  period              = "300"
  statistic           = "Average"
}

You will have to give manual inputs for kpi, which is the most prolonged acceptable latency, and avgProcessing-time, if we exceed the threshold for scaling the worker node.

Read more: How to Host Static Websites on AWS S3?

Give the necessary permissions and IAM roles to run this terraform code and the commands terraform init and terraform apply. And you’ll have an instance that scales in and out based on your RabbitMQ metrics.

AWS Auto scale Instance-Based on RabbitMQ Custom Metrics
Anup Giri

Latest Blogs

New AWS Announcement for October 2023

New AWS Announcement for October 2023


New AWS Announcement for October 2023

Adex International

Nov 08, 2023

Sustainability in the AWS Well-Architected Framework: A Comprehensive Guide

Sustainability in the AWS Well-Architected Framework: A Comprehensive Guide


Sustainability in the AWS Well-Architected Framework: A Comprehensive Guide

Adex International

Oct 19, 2023

AWS New Announcement Sept 2023

AWS New Announcement Sept 2023


AWS New Announcement Sept 2023

Adex International

Oct 17, 2023

Migrate Gitlab PostgreSQL Database to Custom Location Using Ansible

Migrate Gitlab PostgreSQL Database to Custom Location Using Ansible


Migrate Gitlab PostgreSQL Database to Custom Location Using Ansible

Saugat Tiwari

Oct 11, 2023

Mastering DevOps: Your Ultimate Guide to DevOps Managed Services

Mastering DevOps: Your Ultimate Guide to DevOps Managed Services


Mastering DevOps: Your Ultimate Guide to DevOps Managed Services

Biswash Giri

Oct 11, 2023

Discover the Benefits of Security as a Service (SECaaS) for your Business

Discover the Benefits of Security as a Service (SECaaS) for your Business


Discover the Benefits of Security as a Service (SECaaS) for your Business

Saugat Tiwari

Oct 11, 2023

Port Forwarding Using AWS System Manager Session Manager

Port Forwarding Using AWS System Manager Session Manager


Port Forwarding Using AWS System Manager Session Manager

Saugat Tiwari

Oct 11, 2023

Maximizing Directory Services with LDAP: Creating OUs, Groups, and Users for Improved Authentication and Access Control

Maximizing Directory Services with LDAP: Creating OUs, Groups, and Users for Improved Authentication and Access Control


Maximizing Directory Services with LDAP: Creating OUs, Groups, and Users for Improved Authentication and Access Control

Biswash Giri

Oct 11, 2023

AWS Migration Tools: A Comprehensive Guide

AWS Migration Tools: A Comprehensive Guide

IntroductionAWS migration tools are a comprehensive set of services and utilities provided by Amazon...


AWS Migration Tools: A Comprehensive Guide

Binaya Puri

Oct 11, 2023

Difference Between AWS Cloudwatch and Cloudtrail

Difference Between AWS Cloudwatch and Cloudtrail

AWS CloudWatch and AWS CloudTrails are sometimes difficult to distinguish. This article seeks to d...


Difference Between AWS Cloudwatch and Cloudtrail

Sabin Joshi

Oct 11, 2023

New AWS Announcements for June 2023 - Adex

New AWS Announcements for June 2023 - Adex


New AWS Announcements for June 2023 - Adex

Ravi Gupta

Oct 11, 2023

Top 7 Applications Of Cloud Computing In Various Field

Top 7 Applications Of Cloud Computing In Various Field


Top 7 Applications Of Cloud Computing In Various Field

Susmita Karki Chhetri

Oct 11, 2023

Ingesting and Monitoring Custom Metrics in CloudWatch With AWS Lambda

Ingesting and Monitoring Custom Metrics in CloudWatch With AWS Lambda


Ingesting and Monitoring Custom Metrics in CloudWatch With AWS Lambda

Tej pandey

Oct 11, 2023

7 Types of Security in Cloud Computing?

7 Types of Security in Cloud Computing?


7 Types of Security in Cloud Computing?

Mukesh Awasthi

Oct 11, 2023

Cost-effective Use cases & Benefits of Amazon S3

Cost-effective Use cases & Benefits of Amazon S3


Cost-effective Use cases & Benefits of Amazon S3

Nischal Gautam

Oct 11, 2023

IT Outsourcing: Everything You Need To Know

IT Outsourcing: Everything You Need To Know

The world has changed, and as technology advances, so does the world of work. Gone are the day...


IT Outsourcing: Everything You Need To Know

Roshan Raman Giri

Oct 11, 2023

Getting Started with Amazon Redshift in 6 Simple Steps

Getting Started with Amazon Redshift in 6 Simple Steps


Getting Started with Amazon Redshift in 6 Simple Steps

Tej pandey

Oct 11, 2023

How to Host Static Websites on AWS S3?

How to Host Static Websites on AWS S3?

How to Host Static Websites on AWS S3? Hosting a Static Website on AWS S3 has a lot of benefits....


How to Host Static Websites on AWS S3?

Ravi Gupta

Oct 11, 2023

The Importance of Managed Cloud Security for Businesses

The Importance of Managed Cloud Security for Businesses


The Importance of Managed Cloud Security for Businesses

Roshan Raman Giri

Oct 11, 2023

How To Use Amazon S3 For Personal Backup?

How To Use Amazon S3 For Personal Backup?


How To Use Amazon S3 For Personal Backup?

Tej pandey

Oct 11, 2023

Major AWS Updates &Announcements of 2023 - March

Major AWS Updates &Announcements of 2023 - March


Major AWS Updates &Announcements of 2023 - March

Roshan Raman Giri

Oct 11, 2023

How To Insert Data Into a DynamoDB Table with Boto3

How To Insert Data Into a DynamoDB Table with Boto3

DynamoDB is used for many use cases, including web and mobile applications, gaming, ad tech,...


How To Insert Data Into a DynamoDB Table with Boto3

Binaya Puri

Oct 11, 2023

How to Install and Upgrade the AWS CDK CLI

How to Install and Upgrade the AWS CDK CLI


How to Install and Upgrade the AWS CDK CLI

Nischal Gautam

Oct 11, 2023

Ultimate Guide on Creating Terraform Modules

Ultimate Guide on Creating Terraform Modules


Ultimate Guide on Creating Terraform Modules

Tej pandey

Oct 11, 2023

What is serverless computing?

What is serverless computing?


What is serverless computing?

Tej pandey

Oct 11, 2023

AWS Well-Architected Framework Security Pillar

AWS Well-Architected Framework Security Pillar

The Amazon Well-Architected Framework is a set of recommendations and practice guidelines for develo...


AWS Well-Architected Framework Security Pillar

Binaya Puri

Oct 11, 2023

Amazon FSx for Lustre, Windows, and NetApp ONTAP

Amazon FSx for Lustre, Windows, and NetApp ONTAP

Amazon FSx for Lustre, Windows, and NetApp ONTAPAmazon FSx is known for its fully managed, hig...


Amazon FSx for Lustre, Windows, and NetApp ONTAP

Ravi Gupta

Oct 11, 2023

How to Choose the Right Cloud Service Provider?

How to Choose the Right Cloud Service Provider?


How to Choose the Right Cloud Service Provider?

Tej pandey

Oct 11, 2023

25 New AWS Services Updates from AWS Re:Invent 2022

25 New AWS Services Updates from AWS Re:Invent 2022


25 New AWS Services Updates from AWS Re:Invent 2022

Susmita Karki Chhetri

Oct 11, 2023

AWS Managed Hosting Services And Dedicated Hosting Benefits

AWS Managed Hosting Services And Dedicated Hosting Benefits


AWS Managed Hosting Services And Dedicated Hosting Benefits

Tej pandey

Oct 11, 2023

What is Serverless Security? Risk & Best Practices

What is Serverless Security? Risk & Best Practices

Serverless computing  is a rising topic right now in the cloud tech industry. As per a Datad...


What is Serverless Security? Risk & Best Practices

Anup Giri

Oct 11, 2023

Difference Between Cloud Computing and Cybersecurity

Difference Between Cloud Computing and Cybersecurity


Difference Between Cloud Computing and Cybersecurity

Mukesh Awasthi

Oct 11, 2023

DevOps for Developers: How It Helps Streamline the Development Process

DevOps for Developers: How It Helps Streamline the Development Process

As per a survey done by Puppet, firms with DevOps practice have increased recovery speeds by 24 ti...


DevOps for Developers: How It Helps Streamline the Development Process

Roshan Raman Giri

Oct 11, 2023

New AWS Announcements for August 2023

New AWS Announcements for August 2023


New AWS Announcements for August 2023

Rohan Jha

Oct 11, 2023

The FinOps Chronicles

The FinOps Chronicles


The FinOps Chronicles

Anup Giri

Oct 11, 2023

AWS Auto scale Instance-Based on RabbitMQ Custom Metrics

AWS Auto scale Instance-Based on RabbitMQ Custom Metrics


AWS Auto scale Instance-Based on RabbitMQ Custom Metrics

Anup Giri

Oct 11, 2023

Overcome Merge Hell with Trunk based development and Continuous Integration

Overcome Merge Hell with Trunk based development and Continuous Integration


Overcome Merge Hell with Trunk based development and Continuous Integration

Rohan Jha

Oct 11, 2023

What's the difference between CapEX Vs OpEX in Cloud Computing?

What's the difference between CapEX Vs OpEX in Cloud Computing?


What's the difference between CapEX Vs OpEX in Cloud Computing?

Tej pandey

Oct 11, 2023

How Does Your Organization Keep Cloud Costs Under Control?

How Does Your Organization Keep Cloud Costs Under Control?


How Does Your Organization Keep Cloud Costs Under Control?

Susmita Karki Chhetri

Oct 11, 2023

Microsoft Azure vs AWS vs Google Cloud Comparison

Microsoft Azure vs AWS vs Google Cloud Comparison


Microsoft Azure vs AWS vs Google Cloud Comparison

Mukesh Awasthi

Oct 11, 2023

What are the Benefits of Amazon S3 Glacier?

What are the Benefits of Amazon S3 Glacier?


What are the Benefits of Amazon S3 Glacier?

Anup Giri

Oct 11, 2023

Leverage Azure Migrate to Discover and Assess Your AWS Instances for Smooth Migration to Azure

Leverage Azure Migrate to Discover and Assess Your AWS Instances for Smooth Migration to Azure


Leverage Azure Migrate to Discover and Assess Your AWS Instances for Smooth Migration to Azure

Rohan Jha

Oct 11, 2023