Saturday, April 1, 2023

AWS Auto Scaling using Python Boto3

Auto Scaling Group in AWS configure using Python Boto3 

What is Auto Scaling means? 

This is key capability or power of Cloud Computing Engineers believe in their skills on scaling abilities. 

Amazon EC2 Auto Scaling helps to maintain application availability and lets automatically add or remove EC2 instances using scaling policies that we define.  There are 2 types of scaling policies : Dynamic or predictive. These scaling policies let us add or remove EC2 instances capacity to service established or real-time demand patterns.

It contains various steps involved in Auto Scaling process using Python Boto3 we will explore every step that accumulate to form a complete automation solution for a DevOps project.

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 Scaling
In 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 list
import 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 

 object client referring to 'autoscaling' method: create_launch_configuration() method is used to 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 this 
Check the output and compare it withe AWS console
 

4. Updating ASG 

You can try scale up by increase MinSize, MaxSize, DesiredCapacity values up similarly down. When you do update remember that is should not pass LoadBalancerNames.
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.

In Auto Scaling group configuration, Number of instances always remain up and running is the meaning of parameter 'DesiredCapacity'. Auto Scaling Group defining we need to define the Load Balancer in Auto Scaling setup.

Manage ELB using Python Boto3

 Managing AWS ELB automations using Boto3 Python scripts. 

What is ELB?

ELB automatically distributes incoming network traffic (may be HTTP/HTTPS requests) across multiple EC2 instances running in different Availability Zones.

ELB to EC2 instances



 In this post I will experimenting on the following: 
  • Create ELB 
  • Apply security group
  • Create Health check
  • Register instances with ELB  
  • Delete ELB services

1. How to Create ELB using Python Boto3 automations?

The create_load_blancer method is used to create a Load Balancer.
object: client refers to elb 
method: create_load_blancer() which requires LoadBalancerName, Listeners that is again a dictionary of Protocol,LoadBalancerPort,InstanceProtocol, InstancePort, And another parameter 'AvailabilityZone' is important that refer to region on which you ar working on.
import boto3
elb_client=boto3.client('elb')

response = elb_client.create_load_blancer(
	LoadBalancerName='vybhava_lb',
	Listeners=[{'Protocol': 'HTTP', 'LoadBalancerPort':80, 'InstanceProtocol':'HTTP', 'InstancePort': 80},
	AvailabilityZone=['ap-south-1a']
	)
print(response)
print(response['DNSName'])

2. Apply security group on ELB using Boto3

object: client refers to elb method: apply_security_groups_to_load_balancer() here we can use existing security group(that is created during EC2 instance creation) can be associated with the ELB as well.
import boto3
elb_client=boto3.client('elb')
response=elb_client.apply_security_groups_to_load_balancer(
	LoadBalancerName='vybhava_lb',
	SecurityGroups='sg-vybhava_lb']
	)
print(response)

3. How to create Health check?

object: client refers to elb method: configure_health_check() without healthcheck there is not strong bonding between ELB and EC2 instances. This will validate to check weather the incoming request should be send to the instance or not based on the parameters here we provide.
import boto3
elb_client=boto3.client('elb')
health_check_resp=elb_client.configure_health_check(
	LoadBalancerName =  'vybhava_lb',
	HealthCheck={
		'Target':'TCP:22',
		'Interval':10,
		'Timeout':5,
		'UnhealthyThreshold':5,
		'HealthyThreshould': 5
	}
	
print(health_check_resp)	
The health_check_resp object output c should have 200. Http code for success.

4. How to register instance with ELB services?

EC2 Instance(s) will be attached to ELB object: client method: register_instances_with_load_balancer
import boto3
elb_client=boto3.client('elb')

instanceid=input('Please enter instance id to attach to ELB:')
attachinstance=elb_client.register_instances_with_load_balancer(
	LoadBalancerName='vybhava_lb',
	Instances=[	{
		'InstanceId': instanceid,
	}	]
)
print(attachinstance)
Observe that 'Instance Count' value changes according to attached number of instances. In the bottom tabs look for 'Instances'.

5. How to deregister instance from ELB using Boto3?

De-register EC2 instances from ELB, its reverse process what we did for registration.The object: client method: register_instances_with_load_balancer
import boto3
elb_client=boto3.client('elb')

deattachinstance=elb_client.deregister_instances_with_load_balancer(
LoadBalancerName='vybhava_lb',
	Instances=[	{
		'InstanceId': instanceid,
	}	]
)
print(deattachinstance)

6. How to delete ELB service using Boto3?

The removal of elb can be performed on elb_client, to delete ELB we need to use param for delete_loadbalancer is existing elb services name.

object: client refer to elb service
method: delete_loadbalancer()
import boto3
elb_client=boto3.client('elb')

response=elb_client.delete_loadbalancer(LoadBalancerName='vybhava_lb')
print(response)
Goto the aws console and confirm that no ELB in that region where we did all the above. 

 Refrences: https://unbiased-coder.com/boto3-load-balancer-guide/

DevOps Foundation course

DevOps Foundation course
Join us to learn DevOps from the Beginning