{"id":36155,"date":"2016-06-23T16:05:54","date_gmt":"2016-06-23T10:35:54","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=36155"},"modified":"2016-12-19T15:02:54","modified_gmt":"2016-12-19T09:32:54","slug":"aws-autoscaling-group-configured-with-elb-and-alarms-in-boto-python","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/aws-autoscaling-group-configured-with-elb-and-alarms-in-boto-python\/","title":{"rendered":"AWS Autoscaling group configured with ELB and Alarms in Boto (Python)"},"content":{"rendered":"<p style=\"text-align: center\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-36448\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/boto.png\" alt=\"boto\" width=\"699\" height=\"150\" \/><\/p>\n<p><a href=\"http:\/\/www.tothenew.com\/blog\/aws-auto-scaling-lifecycle-hooks-2\/\">Autoscaling is a service<\/a>\u00a0in AWS, which is used to launch or terminate an instance based on user-defined policies, health checks, and schedules.<\/p>\n<p>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.<\/p>\n<p>Before Creating \u00a0an Autoscaling Group\u00a0 we have to keep in mind the following steps:<\/p>\n<ol>\n<li><strong>Autoscaling Group<\/strong>: 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.<\/li>\n<li><strong>Launch Configuration<\/strong>: It is the information needed by the Autoscaling group to launch EC2 instances.<\/li>\n<li><strong>Scaling Policies and Alarms<\/strong>: It is the set of rules for determining when to scale an instance up or down in an autoscaling group.<\/li>\n<li><strong>Elastic Load Balancing (optional)<\/strong>: Add a load balancer to the autoscaling group to distribute traffic and scale instances based on scaling policies.<\/li>\n<\/ol>\n<p style=\"text-align: center\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-36439 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/20140105144132.png\" alt=\"20140105144132\" width=\"535\" height=\"543\" \/><\/p>\n<p>Here we are taking a use case to create an Autoscaling group with ELB and <a href=\"http:\/\/www.tothenew.com\/blog\/aws-step-auto-scaling-policies\/\">configure scaling policies<\/a> 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.<\/p>\n<h2>Steps to follow:<\/h2>\n<ol>\n<li><b>Connecting to ELB<br \/>\n<\/b>The first step for accessing ELB is to create connections to the service:<\/p>\n<p>[js]import boto<br \/>\nconn = boto.connect_elb()[\/js]<\/p>\n<\/li>\n<li><b>Getting all Load Balancers<br \/>\n<\/b>Retrieve existing load balancer information<\/p>\n<p>[js]conn.get_all_load_balancers()[\/js]<\/p>\n<\/li>\n<li><b>Creating Health Check:<br \/>\n<\/b>To create a load balancer we need to have the following information:<br \/>\n-Specific Port Listeners where ELB will ping to instances.<br \/>\n-HealthCheck or the ELB concept of a heartbeat. ELB uses this health check to see whether your instances are up or down.<br \/>\n-A list of Availability Zones.<\/p>\n<p>[js]<br \/>\nimport botofrom boto.ec2.elb import HealthCheck<br \/>\nconn=boto.connect_elb()<br \/>\nhc=HealthCheck(interval=20,healthy_threshold=2,unhealthy_threshold=4,target=&#8217;HTTP:80\/health&#8217;)<br \/>\n[\/js]<\/p>\n<\/li>\n<li><b>Creating Load Balancer<br \/>\n<\/b>Creating load balancer with the health check defined above.<\/p>\n<p>[js]<br \/>\nlb=conn.create_load_balancer(&#8216;my_elb&#8217;,[&#8216;us-east-1a&#8217;,&#8217;us-east-1b&#8217;],[(80, 80, &#8216;http&#8217;), (443, 8443, &#8216;tcp&#8217;)])<br \/>\nlb.configure_health_check(hc)<br \/>\n[\/js]<\/p>\n<\/li>\n<li><b>Creating Launch Configuration<br \/>\n<\/b>Creating a launch configuration for the Autoscaling group.<\/p>\n<p>[js]<br \/>\nimport boto.ec2.autoscale<br \/>\nfrom boto.ec2.autoscale import LaunchConfiguration<br \/>\nconn=boto.ec2.autoscale.connect_to_region(&quot;us-east-1&quot;,profile_name=&#8217;selectcloud&#8217;)<br \/>\nlc=LaunchConfiguration(name=&quot;boto&quot;,image_id=&#8217;ami-id&#8217;,key_name=&#8217;key&#8217;,security_groups=[security_grpid-1,security-grpid-2],instance_type=\u2019t2.small\u2019)<br \/>\nconn.create_launch_configuration(lc)<br \/>\n[\/js]<\/p>\n<\/li>\n<li><b>Getting all Autoscaling Groups<br \/>\n<\/b>Retrieving existing Autoscaling groups.<\/p>\n<p>[js]conn.get_all_groups()[\/js]<\/p>\n<\/li>\n<li><b>Creating Autoscaling Group<br \/>\n<\/b>Creating an autoscaling group with the launch configuration defined above.<\/p>\n<p>[js]<br \/>\nfrom boto.ec2.autoscale import AutoScalingGroup<br \/>\nag=AutoScalingGroup(group_name=&#8217;boto_group&#8217;,load_balancers=[&#8216;myelb1&#8217;],availability_zones=[&#8216;us-east-1c&#8217;,&#8217;us-east-1b&#8217;],launch_config=lc,min_size=1,max_size=3,connection=conn)<br \/>\nconn.create_auto_scaling_group(ag)<br \/>\n[\/js]<\/p>\n<\/li>\n<li><b>To view Activities on Autoscaling Group<br \/>\n<\/b>To view activity on an Autoscaling group<\/p>\n<p>[js]conn.get_all_activities(ag)[\/js]<\/p>\n<\/li>\n<li><b>Scaling a Group Up or Down<br \/>\n<\/b>Creating scale up and scale down policies with cooldown period of 180<\/p>\n<p>[js]<br \/>\nscale_up_policy=ScalingPolicy(name=&#8217;scale_up&#8217;,adjustment_type=&#8217;ChangeInCapacity&#8217;,as_name=&#8217;boto_group&#8217;,scaling_adjustment=1,cooldown=180)<br \/>\nscale_down_policy=ScalingPolicy(name=&#8217;scale_down&#8217;, adjustment_type=&#8217;ChangeInCapacity&#8217;,as_name=&#8217;boto_group&#8217;,scaling_adjustment=-1, cooldown=180)<br \/>\nconn.create_scaling_policy(scale_down_policy)<br \/>\nconn.create_scaling_policy(scale_up_policy)[\/js]<\/p>\n<\/li>\n<li><b>Fetching ARN(Amazon Resource Name)<\/b>\n<p>[js]<br \/>\nscale_down_policy_arn=conn.get_all_policies(as_group=&#8217;boto_group&#8217;,policy_names=[&#8216;scale_down&#8217;])[0]<br \/>\nscale_up_policy_arn=conn.get_all_policies(as_group=&#8217;boto_group&#8217;,policy_names=[&#8216;scale_up&#8217;])[0]<br \/>\n[\/js]<\/p>\n<\/li>\n<li><b>Next, we\u2019ll create CloudWatch alarms that will determine when to run the Auto Scaling Policies.<\/b>\n<p>[js]<br \/>\nfrom boto.ec2.cloudwatch import MetricAlarm<br \/>\nimport boto.ec2.cloudwatch<br \/>\ncloudwatch=boto.ec2.cloudwatch.connect_to_region(&#8216;us-east-1&#8242;,profile_name=&#8217;selectcloud&#8217;)<br \/>\nalarm_dimensions={&quot;AutoScalingGroupName&quot;:&quot;boto_group&quot;}<br \/>\nscale_down_alarm=MetricAlarm(name=&#8217;scale_down_cpu&#8217;,namespace=&#8217;AWS\/EC2&#8242;,metric=&#8217;CPUUtilization&#8217;,statistic=&#8217;Average&#8217;,comparison='&lt;&#8216;,threshold=&#8217;40&#8217;,period=&#8217;60&#8217;, evaluation_periods=2,alarm_actions=[scale_down_policy_arn.policy_arn],dimensions=alarm_dimensions)<br \/>\nscale_up_alarm=MetricAlarm(name=&#8217;scale_up_cpu&#8217;, namespace=&#8217;AWS\/EC2&#8242;,metric=&#8217;CPUUtilization&#8217;, statistic=&#8217;Average&#8217;,comparison=&#8217;&gt;&#8217;, threshold=&#8217;70&#8217;,period=&#8217;60&#8217;, evaluation_periods=2,alarm_actions=[scale_up_policy_arn.policy_arn],dimensions=alarm_dimensions)<br \/>\ncloudwatch.create_alarm(scale_up_alarm)<br \/>\ncloudwatch.create_alarm(scale_down_alarm)<br \/>\n[\/js]<\/p>\n<\/li>\n<\/ol>\n<p>Hope this was useful to you for implementing AWS Autoscaling. The complete Boto script can be downloaded from here\u00a0for a ready reference.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Autoscaling is a service\u00a0in 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 \u00a0an Autoscaling Group\u00a0 we have to [&hellip;]<\/p>\n","protected":false},"author":917,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":23},"categories":[1174,2348],"tags":[2098,248,2091,1262,1929,1746,2092,1611,1892,3236,1358,1790],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/36155"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/917"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=36155"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/36155\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=36155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=36155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=36155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}