Showing posts with label ec2. Show all posts
Showing posts with label ec2. Show all posts

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.

Monday, March 27, 2023

Manage AWS EC2 Instances using Python Boto3 script

Hey Welcome! back to Automations with Python for AWS!! 
Now IT market says AWS is the top number one Cloud Computing platform. That is why I've selected this AWS automations using Python Boto3.

In this post we will be exploring the AWS EC2 Instance related operations, and manage them in a reusable form.
  • Create EC2 instance using Python Boto3
  • Launch AWS EC2 instance using Python Boto3 script
  • Stop AWS EC2 instance using Python Boto3 script
  • Start AWS EC2 Instance using Python Boto3 script
  • Terminate AWS EC2 instance using Python Boto3 script
  • Fetching Public IP of given instance-id



How do you Create EC2 instance using Python3 Boto3? 

 Creating EC2 instance using Boto3 Python code
#=============================================
# File : create_ec2.py
# Description: Create EC2 instance by Boto3

import boto3
ec2 = boto3.resource('ec2')

instances = ec2.create_instances(
        ImageId="ami-0dafa01c8100180f8",
        MinCount=1,
        MaxCount=1,
        InstanceType="t2.micro",
        KeyName="KeyPair1"
    )
    
Launch instance
import boto3
ec2_client = boto3.client('ec2')

# This function will requires 
# image_id [Required] based on the region this will be changing
# instance_type [optional] default t2.micro type otherwise you can provide
# max [optional] default as 1, you can provide maximum number of instances
def launch_instance(image_id, instance_type='t2.micro', max=1):
	resource = ec2_client.run_instances(ImageId=image_id,
					InstanceType=instance_type,
					MinCount=1, MaxCount=max)
									
	for instance in resource['Instances']:
		print(instance['InstanceId'])

# main program
launch_instance('yourami-id')
To control the ec2 instances individual AWS cli commsnd  test Start, Stop
ec2.start_instances 
ec2.stop_instances

and terminate the ec2 instance
List AWS EC2 Instances using python boto3 script
import boto3
ec2_client = boto3.client('ec2')
resp = ec2_client.describe_instances()
for reservation in resp['Reservations']:
	for instance in reservation['Instances']:
		print("Running Instance Image ID: {} Running instance Instance Type: {} Running Instance Keyname {}".format(instance['InstanceId'],instance['InstanceType'],instance['KeyName']))

Python code with Menu driven program to manage EC2 instances
import boto3
import time

ec2 = boto3.resource('ec2')
ec2_client = boto3.client('ec2')

## Display all instances
def display_instances():
    for instance in ec2.instances.all():
        print (instance.id , instance.state)

## Stop instance by given instance id
def stopinstance():    
    instanceid=input("Please enter instanceid:")
    response = ec2_client.stop_instances(InstanceIds=[instanceid])
    time.sleep(90)
    display_instances()

## Terminate instance by given instance id
def terminateinstance():    
    instanceid=input("Please enter instanceid:")
    response = ec2_client.terminate_instances(InstanceIds=[instanceid])
    time.sleep(90)
    display_instances()
    
## Start instance by given instance id
def startinstance():    
    instanceid=input("Please enter instanceid:")
    response = ec2_client.start_instances(InstanceIds=[instanceid])
    time.sleep(90)
    display_instances()

## Main Program
def main():
    while True:
        menu_list=["Display All Instance","Stop Instance","Start Instance","Terminate Instance","Exit"]
        i=1
        for item in menu_list:
            print (i,item)
            i=i+1
        choice=int(input("Please enter Menu Choice:"))
        if choice==1:
            display_instances()
        elif choice==2:
            stopinstances()
        elif choice==3:
            startinstances()
        elif choice==4:
            terminateinstance()            
        else:
            exit()

if __name__ == '__main__':
    main()        
 

Enjoy the automations with Python Boto3 for AWS...

DevOps Foundation course

DevOps Foundation course
Join us to learn DevOps from the Beginning