How Launch an EC2 instance and an EBS volume, and then attach the volume to the instance using Boto3 Library?

Pappu Sanodiya
3 min readJul 4, 2023

--

In this blog I am going to explain you how to launch an ec2 instance, create an EBS volume and attach it to instance by boto3 library.

Boto3 is a Python library that makes easy to interact with Amazon Web Services (AWS) using code. It provides simple functions and methods to create, configure, and manage AWS resources, such as EC2 instances, S3 buckets, and Lambda function. With boto3, we can automate AWS tasks, access AWS services directly from your Python scripts, and build powerful cloud-based applications.

aws using boto3

Before using the boto3 library, there are a few prerequisites you need to have in place:

1. Python: Ensure you have Python installed on your system. `boto3` is a Python library, and it requires a compatible Python version (usually Python 3.x).

2. AWS Account: You need to have an AWS account to work with boto3 and obtain the necessary credentials (Access Key ID and Secret Access Key).

3. AWS CLI: In you local machine have the AWS Command Line Interface (CLI) installed on your machine. It allows you to configure your AWS credentials and region, which boto3 uses for authentication and resource operations.

4. boto3 Library: Install the boto3 library using Python’s package manager `pip`. You can do this by running the following command in your terminal.

pip3 install boto3

Once you have these prerequisites in place, you’ll be all set to start using the `boto3` library to interact with various AWS services programmatically from your Python scripts.

🔰let’s first launch an ec2 instance here we have to give the image id on which region you are launching instance pick from their and instance type, I am using t2.micro it’s come under the free tier category.

instance is launched successfully

here you can see our instance is launched successfully and I have store instance id in InstanceID variable so that we can use in future.

🔰 Now let’s create an EBS volume. make sure to create ebs volume in same availability zone where you launched instance.

you can see here our volume is created and I have store volume id in VolumeID variable so that we can use it while attaching to an instance.

🔰 for attaching the EBS volume we use client function.

here we have to give instance id, volume id and device information I have already stored Instance id and volume id in InstanceID and VolumeId variable so I have give that variable. and I have stored the output of this this function in attachResponse variable.

Now here you can see our EBS volume is attached to instance.

import boto3
myec2 = boto3.resource("ec2")
response= myec2.create_instances(
ImageId='ami-06b09bfacae1453cb',
InstanceType='t2.micro',
MaxCount=1,
MinCount=1
)
InstanceID = response[0].id


VolumeResponse = myec2.create_volume(
AvailabilityZone = 'us-east-1a',
Size = 2
)
volumeID = VolumeResponse.id


client = boto3.client('ec2')
attachResponse = client.attach_volume(
InstanceId = InstanceID,
VolumeId = volumeID,
Device = '/dev/sdf'

)
myec2
VolumeResponse
attachResponse

here is my LinkedIn profile if you are facing any issues you can contact to me.

https://www.linkedin.com/in/pappu-sanodiya-a85411202/

Thanks for reading …….

--

--

Pappu Sanodiya
Pappu Sanodiya

Written by Pappu Sanodiya

Technical Content Writer | DevOps and Cloud Aspired | Exploring Cutting Edge technology | Passionate about making complex tech concepts simple and accessible |

No responses yet