ALB and ASG

Auto scalling group

chevron-rightASGhashtag

is a collection of ec2 instance used for the pupose of automated scalling

asg helps us to scale out (add) or scales in (remove) ec2 instance to match load for your application

we need to define

min size - never goes below this side

max size - never goes to this size

desire size - current size

Scalling policy: 3 types

  1. dynamic scalling:

a] target tracking

  • ec2 sends predefined metrics to CloudWatch eg cpu, memory

  • suppose u want to maintain average 50% CPU utlization on each node

  • create cloudwatch alarm automatically for us

b] step scaling

in target tracking we defined metrics but not define how much ec2 to provision

here define how much ec2 instance to provision when threshold match

we need to cloudwatch alarm for scalling policy

support multiple scalling adjustments as shown in image

c] simple scaling

single scaling adjustment

2] predictive scaling:

3] schedule policy:

schedule scalling based on event eg scale during black friday

Let's Proceed

Create security group

for ALB

allow http for all

and for ASG

allow all tcp for alb security group

allow ssh

Create Auto Scaling Group

step1 - select or create launch template

launch template:

  • A Launch Template is a resource in AWS EC2 that defines the configuration for EC2 instances.

  • It includes:

    • AMI ID (operating system + application image)

    • Instance type (e.g., t3.medium)

    • Key pair (SSH access)

    • Security groups

    • Network settings (VPC, subnet) - select at time of configuring auto scaling group

    • Storage (EBS volumes)

    • User data script (bootstrap script) - will run as root user

Step 2 - Create an Auto Scaling Group

  1. Navigate to the Auto Scaling Groups section in the AWS Management Console.

  2. Click on Create Auto Scaling group.

  3. Provide a name for your Auto Scaling group.

  4. Select the launch template you created in Step 1.

  5. Under networking Choose the VPC and AZ, Subnet(s) where you want your group to launch instances.

  6. Select load balancer

a] no LB

b] Existing LN

c] new LB

schema - internet or internal facing

vpc - ame as we select in networking

keep az default one

add listener and target group

enable health check for elb - tick yes

Configure group size and scaling policy

group size

desire capacity

min cap

max cap

Review and Create

suppose we have any application running on port 80

when we hit alb dns we see our app

go to auto scaling group

then auto scaling tab

insde this tab we have multiple option for scalling policy

we will see all 3 policies

  1. dynamic scaling policy

when we select metric threshould it create 2 alarm in cloudwatch for scale in and scale out

  1. predictive policy

  1. schedule policy

Load testing

put load on ec2 instance by using tool called

sudo stress cpu

check cpu utilization in asg > monitoring > check graph

go to alarm in cloudwatch u will see in alarm in red on ok green for no alarm

go to activity

u will see new ec2 launch

for memory utilization

chevron-rightcustom cloudwatch alarmhashtag

step - install agent, create cloudwatch metrics alarm eg memory threshold for ec2

assign alarm to ASG and choose steps scalling or single scaling

Good one, Pavan πŸ‘ This is a common confusion in AWS.


πŸ”Ή Can we scale ASG on memory utilization with Target Tracking?

  • By default β†’ No.

    • AWS Auto Scaling Groups (ASG) only support scaling on metrics from CloudWatch.

    • Out-of-the-box, EC2 instances do not publish memory utilization to CloudWatch.

    • Only CPUUtilization, NetworkIn, NetworkOut, etc. are available.


πŸ”Ή How to Scale on Memory Utilization (Workaround)

  1. Install CloudWatch Agent

    • On each EC2, install and configure the CloudWatch Agent to publish mem_used_percent (or similar custom metric).

    • Example config (/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json):

    • Start the agent β†’ metrics flow into CloudWatch under CWAgent.

  2. Create a CloudWatch Custom Metric Alarm

    • Example: Trigger when memory > 75% for 2 data points.

  3. Attach Alarm to ASG Scaling Policy

    • Instead of Target Tracking, you’ll use a Step Scaling or Simple Scaling policy linked to the CloudWatch Alarm.


πŸ”Ή Why not Target Tracking directly?

  • Target Tracking only works with predefined metrics (like CPUUtilization) or some ALB metrics.

  • For custom metrics (like memory), AWS does not allow direct target tracking.

  • Instead, you simulate it with CloudWatch Alarms + Step Scaling.


βœ… Summary

  • CPUUtilization β†’ Target Tracking βœ…

  • MemoryUtilization β†’ Needs CloudWatch Agent β†’ Custom Metric β†’ CloudWatch Alarm β†’ Step Scaling ❌ (not target tracking)


πŸ‘‰ Do you want me to show you a Terraform example of scaling an ASG based on high memory utilization (using custom metric + alarm)?

Last updated