What is Auto Scaling means?
ASG Groups associated with ELB and EC2 instances |
Understanding AWS Auto scaling configuration steps
Check any running instances Create launch configuration Configure ASG for Auto scaling Verify the configuration Disable Auto ScalingIn order to setup Auto Scaling, we need Launch configuration to be created first followed by Auto Scaling group
1. Check any running instances
Get the running EC2 instance listimport boto3 ec2_resource=boto3.resource('ec2') instances = ec2_resource.instances.filter( Filters=[{'Name':'insance-state-name','Values':['running']} ]) for i in instances: print(instance.id, instance.state)Check the output
2. Create launch configuration
as_client = boto3.client('autoscaling') ami_id = input("Please enter AMI id to use in Auto Scaling:") keyname = input("Please enter Key Name to use for instances:") response = as_client.create_launch_configuration( LaunchConfigurationName = 'vybhava_lc', ImageId = ami_id, KeyName = kayname, SecurityGroup = ['vybhava_sg'] InstanceType = 't2.micro' ) print(response)When you observe the printed output have 'HTTPStatusCode as 200 then it is successful. 3. Creating Auto Scaling Group The create_auto_scaling_group() method is used to create Auto Scaling group.
asg_resp = as_client.create_auto_scaling_group( AutoScalingGroupName='vybhava_asg', LaunchConfigurationName='vybhava_lc', MinSize=1, MaxSize=2, DesiredCapacity=1, LoadBalancerNames=['vybhava_lb'] AvailabilityZones=['ap-south-1b','ap-south-1c'] ) print (asg_resp) print (asg_resp['ResponseMetadata']['HTTPStatusCode']) #Try thisCheck the output and compare it withe AWS console
4. Updating ASG
update_resp = as_client.update_auto_scaling_group( AutoScalingGroupName='vybhava_asg', LaunchConfigurationName='vybhava_lc', MinSize=1, MaxSize=2, DesiredCapacity=1, AvailabilityZones=['ap-south-1b','ap-south-1c'] ) print(update_resp)When you run down to 0 value then terminated ASG instances.