Open In App

Launching AWS EC2 Instance using Python

In this article, we will learn how python can be used for creating and managing Amazon Web Services (AWS) such as Elastic Compute Cloud (EC2), Simple Storage Service (S3), Relational Database Service (RDS). For this purpose, we will use boto3 library of python, Boto is a Python package that provides interfaces to Amazon Web Services (AWS). 

Prerequisites:

For installing boto3 in python :



default version : pip install boto3
specific version : python3 -m pip install boto3 (as python3 in this case)

Setting Up the AWS EC2 Console:

For accessing the AWS services from python code we first need to create a user and give him programmatic access using Amazon console.



Now we are all set to launch our EC2 instance using python code. For making a connection with EC2 instance we use boto3’s client API. The client API takes in following arguments to make a connection with AWS the service.

Implementation:




#Python Program for creating a connection
import boto3
 
ec2 = boto3.client('ec2',
                   'ap-south-1',
                   aws_access_key_id='',
                   aws_secret_access_key='')
 
#This function will describe all the instances
#with their current state
response = ec2.describe_instances()
print(response)

OUTPUT

Creating an instance:

The ec2.run_instances launches the specified number of instances using an AMI for which you have permissions. It provides a variety of launch configurations, but we can launch instances with few of the following arguments.

Implementation:




#Python Program for creating a connection
import boto3
 
#Function for connecting to EC2
ec2 = boto3.client('ec2',
                   'ap-south-1',
                   aws_access_key_id='',
                   aws_secret_access_key='')
 
#Function for running instances
conn = ec2.run_instances(InstanceType="t2.micro",
                         MaxCount=1,
                         MinCount=1,
                         ImageId="ami-02d55cb47e83a99a0")
print(conn)

OUTPUT

How to terminate an EC2 instance using Python and the boto3 library:
 

To terminate an EC2 instance using Python and the boto3 library, you can use the terminate_instances() method of the ec2 client. This method requires you to provide the InstanceIds of the instances that you want to terminate as a list.

Here is an example of how you can terminate an EC2 instance using Python and boto3:




import boto3
 
# Connect to the EC2 service
ec2 = boto3.client('ec2',
                   'ap-south-1',
                   aws_access_key_id='',
                   aws_secret_access_key='')
 
# Terminate the instance with the specified ID
response = ec2.terminate_instances(InstanceIds=['instance-id-1'])
 
# Print the response
print(response)

Note that the terminate_instances() method returns a TerminateInstancesResponse object, which contains information about the request and the instances that were terminated. You can access the TerminatingInstances field of this object to get a list of InstanceStateChange objects, which contain information about the state of the terminated instances, such as their ID, previous state, and current state.


Article Tags :