Create and attach an aws EBS volume

Hello everyone, today we are going to create an aws EBS volume and attach it to a running aws  EC2 instance. This volume then can be used to store persistent data like we use a pen drive to store data.
As per AWS, 

Amazon Elastic Block Store (Amazon EBS) provides block level storage volumes for use with EC2 instances. EBS volumes behave like raw, unformatted block devices. You can mount these volumes as devices on your instances. EBS volumes that are attached to an instance are exposed as storage volumes that persist independently from the life of the instance. You can create a file system on top of these volumes, or use them in any way you would use a block device (such as a hard drive). You can dynamically change the configuration of a volume attached to an instance.

So let's login to your AWS account and open EC2 service. Scroll down and click on Elastic Block Store and then Volumes.


Now Click on Create Volume button to create a new EBS volume


In the next page Select the Availability Zone. Be sure to choose the same availability zone where your EC2 instance is created or else we won't be able to attach this EBS volume to our EC2 instance.

As per first snapshot, my EC2 instance is created in ap-south-1b. So during EBS volume creation I'll choose ap-south-1b as availability zone and Size as 1 GiB. We can also add tag to this volume.

So now our EBS volume is created. Now we need to attach this volume to our existing EC2 instance. 

We can only attach an EBS volume whose state is available. Select the new EBS volume then click on Attach Volume button. 

Select the Instance id where you want this volume to attach and click on Attach button.

Now select your EC2 instance and under Storage tab we can see two volumes. First volume is the Root volume where we have installed our OS while second volume is the newly attached EBS volume.


Now connect to this EC2 instance through SSH (if your are using Linux host machine) or Putty (if you are using windows host machine). Please follow this link if you are using putty tool to connect this EC2 instance.

Switch to root user using below command

sudo su - root


Check the available hard-disks attached to this machine using below commands

fdisk -l


To be able to store our data in this new EBS volume we need to perform below operations

  • Create partition in this volume. We can create up to 4 primary partitions. Here we will create only one partition of size 1 GiB.
  • Then we need to format this partition with desired format type. We will format and create an ext4 partition.
  • Lastly we will mount this partition to our system and then can save data in this volume.

To create a partition enter command as fdisk <hard-disk name>

fdisk /dev/xvdf

Press n to create a new partition, press p to create a primary partition, then press enter to create default partition number (partition 1). Press enter to choose default First sector and again press enter to choose  default Last sector. Finally press w to save this partition into the system.


To format this new partition enter below command

mkfs.ext4 /dev/xvdf1


We can create a new directory and mount our formatted partition to this directory as shown in the below snapshot

Now we can create files under this new directory /mydrive and save all our data.

Hope you have learnt something new today.


Happy Learning

Comments

Popular posts from this blog

Create custom VPC with private and public subnets

How to create an AWS EC2 instance