AWS Autoscaling group configured with ELB and Alarms in Boto (Python)

23 / Jun / 2016 by Mayur Rastogi 0 comments


Autoscaling is a service in AWS, which is used to launch or terminate an instance based on user-defined policies, health checks, and schedules.

There are several ways to configure an auto-scaling group in AWS, here we are focusing on implementing it in python using AWS python module boto.

Before Creating  an Autoscaling Group  we have to keep in mind the following steps:

  1. Autoscaling Group: It is a group used for maintaining or scaling a set of EC2 instances in one or more availability zones. It is specific to a single region.
  2. Launch Configuration: It is the information needed by the Autoscaling group to launch EC2 instances.
  3. Scaling Policies and Alarms: It is the set of rules for determining when to scale an instance up or down in an autoscaling group.
  4. Elastic Load Balancing (optional): Add a load balancer to the autoscaling group to distribute traffic and scale instances based on scaling policies.


Here we are taking a use case to create an Autoscaling group with ELB and configure scaling policies in which an instance is scaled up when CPU Utilization is greater than 70% and scaled down when CPU utilization is less than 40% over 2 cycles of 60 seconds each.

Steps to follow:

  1. Connecting to ELB
    The first step for accessing ELB is to create connections to the service:

    [js]import boto
    conn = boto.connect_elb()[/js]

  2. Getting all Load Balancers
    Retrieve existing load balancer information


  3. Creating Health Check:
    To create a load balancer we need to have the following information:
    -Specific Port Listeners where ELB will ping to instances.
    -HealthCheck or the ELB concept of a heartbeat. ELB uses this health check to see whether your instances are up or down.
    -A list of Availability Zones.

    import botofrom boto.ec2.elb import HealthCheck

  4. Creating Load Balancer
    Creating load balancer with the health check defined above.

    lb=conn.create_load_balancer(‘my_elb’,[‘us-east-1a’,’us-east-1b’],[(80, 80, ‘http’), (443, 8443, ‘tcp’)])

  5. Creating Launch Configuration
    Creating a launch configuration for the Autoscaling group.

    import boto.ec2.autoscale
    from boto.ec2.autoscale import LaunchConfiguration

  6. Getting all Autoscaling Groups
    Retrieving existing Autoscaling groups.


  7. Creating Autoscaling Group
    Creating an autoscaling group with the launch configuration defined above.

    from boto.ec2.autoscale import AutoScalingGroup

  8. To view Activities on Autoscaling Group
    To view activity on an Autoscaling group


  9. Scaling a Group Up or Down
    Creating scale up and scale down policies with cooldown period of 180

    scale_down_policy=ScalingPolicy(name=’scale_down’, adjustment_type=’ChangeInCapacity’,as_name=’boto_group’,scaling_adjustment=-1, cooldown=180)

  10. Fetching ARN(Amazon Resource Name)


  11. Next, we’ll create CloudWatch alarms that will determine when to run the Auto Scaling Policies.

    from boto.ec2.cloudwatch import MetricAlarm
    import boto.ec2.cloudwatch
    scale_down_alarm=MetricAlarm(name=’scale_down_cpu’,namespace=’AWS/EC2′,metric=’CPUUtilization’,statistic=’Average’,comparison='<‘,threshold=’40’,period=’60’, evaluation_periods=2,alarm_actions=[scale_down_policy_arn.policy_arn],dimensions=alarm_dimensions)
    scale_up_alarm=MetricAlarm(name=’scale_up_cpu’, namespace=’AWS/EC2′,metric=’CPUUtilization’, statistic=’Average’,comparison=’>’, threshold=’70’,period=’60’, evaluation_periods=2,alarm_actions=[scale_up_policy_arn.policy_arn],dimensions=alarm_dimensions)

Hope this was useful to you for implementing AWS Autoscaling. The complete Boto script can be downloaded from here for a ready reference.


Leave a Reply

Your email address will not be published. Required fields are marked *