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

23 / Jun / 2016 by Mayur Rastogi 0 comments

boto

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.

20140105144132

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

    [js]conn.get_all_load_balancers()[/js]

  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.

    [js]
    import botofrom boto.ec2.elb import HealthCheck
    conn=boto.connect_elb()
    hc=HealthCheck(interval=20,healthy_threshold=2,unhealthy_threshold=4,target=’HTTP:80/health’)
    [/js]

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

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

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

    [js]
    import boto.ec2.autoscale
    from boto.ec2.autoscale import LaunchConfiguration
    conn=boto.ec2.autoscale.connect_to_region("us-east-1",profile_name=’selectcloud’)
    lc=LaunchConfiguration(name="boto",image_id=’ami-id’,key_name=’key’,security_groups=[security_grpid-1,security-grpid-2],instance_type=’t2.small’)
    conn.create_launch_configuration(lc)
    [/js]

  6. Getting all Autoscaling Groups
    Retrieving existing Autoscaling groups.

    [js]conn.get_all_groups()[/js]

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

    [js]
    from boto.ec2.autoscale import AutoScalingGroup
    ag=AutoScalingGroup(group_name=’boto_group’,load_balancers=[‘myelb1’],availability_zones=[‘us-east-1c’,’us-east-1b’],launch_config=lc,min_size=1,max_size=3,connection=conn)
    conn.create_auto_scaling_group(ag)
    [/js]

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

    [js]conn.get_all_activities(ag)[/js]

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

    [js]
    scale_up_policy=ScalingPolicy(name=’scale_up’,adjustment_type=’ChangeInCapacity’,as_name=’boto_group’,scaling_adjustment=1,cooldown=180)
    scale_down_policy=ScalingPolicy(name=’scale_down’, adjustment_type=’ChangeInCapacity’,as_name=’boto_group’,scaling_adjustment=-1, cooldown=180)
    conn.create_scaling_policy(scale_down_policy)
    conn.create_scaling_policy(scale_up_policy)[/js]

  10. Fetching ARN(Amazon Resource Name)

    [js]
    scale_down_policy_arn=conn.get_all_policies(as_group=’boto_group’,policy_names=[‘scale_down’])[0]
    scale_up_policy_arn=conn.get_all_policies(as_group=’boto_group’,policy_names=[‘scale_up’])[0]
    [/js]

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

    [js]
    from boto.ec2.cloudwatch import MetricAlarm
    import boto.ec2.cloudwatch
    cloudwatch=boto.ec2.cloudwatch.connect_to_region(‘us-east-1′,profile_name=’selectcloud’)
    alarm_dimensions={"AutoScalingGroupName":"boto_group"}
    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)
    cloudwatch.create_alarm(scale_up_alarm)
    cloudwatch.create_alarm(scale_down_alarm)
    [/js]

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

FOUND THIS USEFUL? SHARE IT

Leave a Reply

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