Reading Time: 3 minutes
AWS EC2 boto3 complete guide tutorial will guide you on How to create EC2 instances using boto3 and how to list EC2 instances. We consider you already have an AWS account however if you do not have please follow the below tutorial to create an AWS account. Once you have an AWS account, You may create EC2 using Python. It would help if you also created/grant AWS CLI access. We suggest you go through ” AWS EC2 boto3 complete guide ” and follow all mentioned steps.
How to Create AWS Account: Please click here to create AWS free account and follow and follow instructions and “AWS EC2 boto3 complete guide”
Table of Contents
We suggest you follow all the steps “AWS EC2 boto3 complete guide”
AWS EC2 boto3 complete guide
Prerequisites
- You can Create EC2 Instance using Python
- You need to have an AWS account
- You need to have CLI Access
- You can create AWS account if you do not have one yet
- Install AWS CLI
- Configure credential
- AWS EC2 boto3 complete guide
What is Amazon EC2
Using Amazon EC2 you can get on-demand, scalable computing capacity. AWS EC2 will reduce cost compared to an on-prem environment where you need to purchase hardware etc hence the cost is less if you use EC2 you can also scale UP your resource quickly. You can launch multiple servers (EC2) together, configure security and you can also scale down whenever required.
How to create EC2 instance using boto3
Python Code – Create EC2 instance
import boto3
ec2 = boto3.resource('ec2')
Ubuntu = 'Ubuntu'
instances = ec2.create_instances(
ImageId="ami-0283a57753b18025b",
MinCount=1,
MaxCount=1,
InstanceType="t2.micro",
KeyName="tf-kp-name",
TagSpecifications=[
{
'ResourceType': 'instance',
'Tags': [
{
'Key': 'Name',
'Value': Ubuntu
}
]}])
Code Explanation (Create EC2)
It would be best if you imported the boto3 module
initialize ec2 resource
Create a variable “Ubuntu”
Finally, create an instance using boto3
What is BOTO3
The AWS SDK for Python (Boto3) is a Python package/module which provides a Python API for AWS infrastructure services. Using the SDK for Python, you can build applications on top of Amazon S3, Amazon EC2, Amazon DynamoDB, and many other AWS services.
With Boto3, developers can write Python scripts to create, configure, and manage AWS resources such as EC2 instances, S3 buckets, DynamoDB tables, Lambda functions, and more. It provides a comprehensive set of methods and classes for interacting with AWS services, allowing you to perform operations such as creating, deleting, updating, and retrieving information about resources.
Boto3 handles the low-level details of making requests to AWS services over HTTP, handling authentication, and parsing the responses. It simplifies the process of working with AWS APIs and provides an intuitive programming interface, allowing developers to focus on building their applications rather than dealing with the underlying infrastructure.
Boto3 is a powerful tool for Python developers who want to leverage the capabilities of AWS services in their applications. It is widely used for tasks such as infrastructure provisioning, data processing, automation, and building serverless applications on AWS.
How to get AWS EC2 instances using Python boto3
Steps to follow
- import boto3
- connect to EC2 resource
- Filter Running instances
- Get instance_id
- Get instance_type
- Get private_ip
- Get , {image_id},
- Get public_ip
Python Code – Describe/list EC2 instance
import boto3
ec2 = boto3.client('ec2')
reservations = ec2.describe_instances(Filters=[
{
"Name": "instance-state-name",
"Values": ["running"],
}
]).get("Reservations")
for reservation in reservations:
for instance in reservation["Instances"]: ## Each reservation has an embedded Instances array that contains all EC2 instances in the reservation
instance_id = instance["InstanceId"]
instance_type = instance["InstanceType"]
private_ip = instance["PrivateIpAddress"]
image_id = instance["ImageId"]
public_ip = instance["PublicIpAddress"]
print(f" {instance_id}, {instance_type}, {private_ip}, {image_id}, {public_ip} ")
We hope AWS EC2 boto3 complete guide helps you to create EC2 instance and lists EC2 instance using python programme.