Attaching EBS volume to an Instance

Garvit Garg
4 min readMar 27, 2021

Hello everyone
In this blog we are gonna learn how to attach an EBS volume to an instance through CLI.

For this we have to follow certain steps which are-
STEP-1 : We need to create a IAM user with the help of which we can login to our AWS console through CLI.
STEP-2 : After login through CLI we will create a key pair and a security group
STEP-3 : With the help of these key pairs and Security Group we will launch an instance.
STEP-4 : Create an EBS volume.
STEP-5 : Attach the created EBS volume with the Instance created.

Let’s follow these above steps:

STEP-1 : Creating an IAM user.
Go to IAM dashboard
Select Users
Click on add User to add a user.

Provide name to the User
Select the Access type to be given to the user.

Set the permission boundary for the user according to your need.
For this task we are gonna use administratoraccess as our policy.

Provide the tag names(Optional)
Review the details and proceed to create the user.
Once the user is created you will get an access key and a security access key to configure the AWS using CLI.

STEP-2 : Creating a key pair and a security group
Firstly login to your AWS account by use of access key and security access key created with the user.

Create a key pair by use of command : aws ec2 create-key-pair — key-name (Key_Name)

Now after creating a key pair we need to create a security group by using command : aws ec2 create-security-group — group-name (Group_Name) — description (“provide description”)

STEP-3 : Creating an instance using above created key pairs and security groups.
To create an instance we need following credentials :
1) Image id : Which we can get from AMI list
2) Region_name
3) Instance_type : For free tier we always use t2.micro
4) Number of Instance : 1
5) Key_name : key generated in step2
6) Security_group_id : We can see Security group id using cmd- describe-security-group

Now we have can launch an instance by using command :
aws ec2 run-instances — (Image of instance) — region (region) — instance-type t2.micro — count 1 — key-name (Key name) — security-group-id (Id of security group)

This will create an instance with an inbuilt EBS volume of 8GB

STEP-4 : Create an EBS volume
We need to provide
1)Availability Zone
2)Size of Volume
To create an EBS volume we use command : aws ec2 create-volume — availability-zone (Region) — size (Size of voume)

This will create an EBS Volume of size 1Gb in ap-south-1a region.

STEP-5 : Attach the volume to instance
To do so we need
1) Instance_id : Which we can see through cmd- aws ec2 describe-instance
2) Volume_ID : Which we can see through cmd- aws ec2 describe-volume
3) Device_Name : Which we can see in our AWS Portal

As we know we have a default EBS volume attached with our instance.
In presence of which we can not attach another volume.
So in this case we have to change our device name to attach another volume.
After attaching our EBS volume we can remove the default attached volume.

To attach volume we will use command :
aws ec2 attach-volume — volume-id (Volume_id) — instance-id (Instance-id) — device (Device_name)

An EBS Volume of size 1 Gb is attached to our instance created using key pairs and security groups created by a user using CLI.

THANK YOU

--

--