Launching an EC2 instance and attaching an EBS volume to the instance programmatically using API(boto3).

Rishabh Manhas
3 min readJun 29, 2023

--

Introduction

Launching an EC2 (Elastic Compute Cloud) instance and attaching an EBS (Elastic Block Store) volume to it programmatically using the AWS API with the Boto3 library opens up possibilities for automating infrastructure provisioning and scaling in your AWS environment. In this guide, we will explore the process of launching an EC2 instance and programmatically attaching an EBS volume to the instance using Boto3.

AWS EC2 provides scalable virtual servers in the cloud, while EBS offers persistent block-level storage volumes. By integrating these services programmatically, you can automate the creation of instances and attach storage volumes, allowing you to dynamically scale your infrastructure and manage your storage requirements efficiently.

To get started, we will walk through the steps required to achieve this programmatically using the Boto3 library, which is the AWS SDK for Python.

Per-requisites for the practical :

  1. AWS CLI should be installed in the OS.
  2. AWS CLI should be configured in the OS with Admin privileges.
  3. Create and download a keypair in your local machine.
  4. Import Boto3 and Establish a Connection:
  • Import the Boto3 library into your Python script or interactive Python environment.
  • Establish a connection to the AWS EC2 service using your AWS credentials.
import boto3
ec2 = boto3.client('ec2', region_name = "ap-south-1")

2. Launch an EC2 Instance:

  • Specify the desired parameters for the instance, such as the instance type, AMI (Amazon Machine Image), security groups, and key pair.
  • Use the Boto3 run_instances() method to launch the EC2 instance with the provided parameters.
inst = ec2.run_instances(ImageId="ami-0b9ecf71fe947bbdd",
MinCount=1,
MaxCount=1,
InstanceType="t2.micro",
KeyName="redhat",)
print(inst)

3. Retrieve the Instance ID:

  • Capture the instance ID of the launched EC2 instance for subsequent operations.
inst['Instances'][0]['InstanceId']

4. Create an EBS Volume:

  • Specify the desired parameters for the EBS volume, such as the size, availability zone, and volume type.
  • Use the Boto3 create_volume() method to create the EBS volume with the provided parameters.
vol = ec2.create_volume(AvailabilityZone=inst['Instances'][0]['Placement']['AvailabilityZone'],
Size=2,
VolumeType='gp2')
print(vol)

5. Retrieve the Volume ID:

  • Capture the volume ID of the created EBS volume for subsequent operations.
vol['VolumeId']

6. Attach the EBS Volume to the EC2 Instance:

  • Use the Boto3 attach_volume() method to attach the EBS volume to the EC2 instance, specifying the instance ID and volume ID.
inst_with_vol = ec2.attach_volume(Device='/dev/xvdf',
InstanceId=inst['Instances'][0]['InstanceId'],
VolumeId=vol['VolumeId'],
)
print(inst_with_vol)

By following these steps and executing the appropriate Boto3 API calls, you can programmatically launch an EC2 instance and attach an EBS volume to it. This automation enables you to create and configure instances on the fly, tailor your storage needs, and streamline your infrastructure management.

Here is the entire code with output.

--

--

Rishabh Manhas
Rishabh Manhas

Written by Rishabh Manhas

CSE Student || Curiosity is the most underrated skill.

No responses yet