Last active
March 28, 2018 14:50
-
-
Save sudheerchamarthi/7600d81b004926f7d346597daf499ec2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import boto3 | |
| import time | |
| import datetime | |
| print "Function Start Time: "+ str(datetime.datetime.now()) | |
| Region='us-east-1' | |
| ASG_Name='awseb-e-3rreanw3r3-stack-AWSEBAutoScalingGroup-C20XEFG034MN' | |
| Draining_Time='20' | |
| ELB_Name='awseb-e-3-AWSEBLoa-PPG010DUGCPK' | |
| ASG_Instance_Count=3 | |
| asg_client = boto3.client('autoscaling',region_name=Region) | |
| elb_client = boto3.client('elb',region_name=Region) | |
| ec2_client = boto3.client('ec2',region_name=Region) | |
| waiter = ec2_client.get_waiter('instance_terminated') | |
| asg_response = asg_client.describe_auto_scaling_groups(AutoScalingGroupNames=[ASG_Name]) | |
| len(asg_response['AutoScalingGroups'][0]['Instances']) | |
| for Instance in range(len(asg_response['AutoScalingGroups'][0]['Instances'])): | |
| Instance_ID=asg_response['AutoScalingGroups'][0]['Instances'][Instance]['InstanceId'] | |
| print "Detaching this Instance from ASG" | |
| asg_client.detach_instances(InstanceIds=[Instance_ID],AutoScalingGroupName=ASG_Name,ShouldDecrementDesiredCapacity=False) | |
| print "Deregistering the Instance - "+ Instance_ID | |
| elb_client.deregister_instances_from_load_balancer(LoadBalancerName=ELB_Name,Instances=[{'InstanceId': Instance_ID}]) | |
| print "Wait till the Instance is Deregistered" | |
| time.sleep(int(Draining_Time)) | |
| print "Terminating the Instance - "+ Instance_ID | |
| ec2_terminate = ec2_client.terminate_instances(InstanceIds=[Instance_ID]) | |
| waiter.wait(InstanceIds=[Instance_ID]) | |
| print "Terminated Instance Successfully" | |
| if Instance < (ASG_Instance_Count-1): | |
| print "Wait for 2 minutes before terminating another instance" | |
| time.sleep(120) | |
| print "Function End Time: "+ str(datetime.datetime.now()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment