How to set up an Elastic Block Store (EBS) on AWS
Published: 2024-04-06 20:23:03

In this article we will learn how to create and configure an Elastic Block Store volume and also attach it to an EC2 instance.

**Note:** The EC2 and EBS volume must be in the same availability zone.

- We need to first create an EC2 instance
- Log in to your AWS Console.
- Navigate to the EC2 dashboard
- Click on "Launch Instance" and enter a name for the EC2 E.g. Test Sever.
- Choose an Amazon Machine Image (AMI) of your choice. E.g. Amazon Linux
- Choose an instance type (Select one within the free tier)
- Create a key pair or you can continue without a key pair (Not recommended)
- Create a security group or choose the default one
- Configure the storage as seen in the image below
- Leave everything else as default and click on "Launch instance"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/timpffqsy44g0owmxajt.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mx9u6qq1fqbondqts4a8.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ackav153z6slvqs9sq70.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/crg3d4k3aks2ud1wycae.png)




We now need to launch our instance and connect to it and proceed to create an EBS volume and attach it to our EC2

**Let's create an EBS Volume**

In the EC2 dashboard scroll down and click on "volumes" under Elastic Block Store category.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sjnti978hkpewdp6bhec.png)



- Click on "create volume"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/awfpijg81sgqjqf30g5r.png)

- Specify the volume type
- Specify the size
- Specify the availability zone (Same as the EC2 instance we created earlier)
- Configure as seen in the image below.
- Click on "Create volume"


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jr47hgo08s8euco50osh.png)




Now Let's attach the EBS volume to the EC2

- Select the EBS volume we just created .
- Click on "Actions" and then click on "Attach volume."

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i4htq337aez6adxm29y5.png)



- Click on the instance and choose the instance you wish to attach the volume from the drop down (The EC2 created earlier will show)
- Specify the device name for the volume E.g. /dev/sdf
- Click on "Attach volume" to attach the volume to the EC2 instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2zqreootqmef00kyhgt.png)



It's time to connect to the EC2 Instance and mount the EBS

- Select the EC2 and click on "Connect"
- Connect using the EC2 instance connect
Viola! We are connected to the EC2 terminal


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5no59us64xg0c5h19jh.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6r611g680sdegelfkgf6.png)



We need to run some commands on the EC2 terminal

- 
Use the "lsblk" command to list the available disk drives

Once the attached EBS volume has been identified i.e the 100G volume (As seen in the image)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6q14931959o4aqj773ta.png)



Use the "sudo mkfs -t ext4 /dev/xvdf" to format the EBS volume.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkiie6yi79fhaqd4vy3l.png)



- Use the "mkdir" command to create a mount point directory for the volume.
- Use "sudo mount /dev/xvdf /data" to mount the volume at the mount point directory we just created.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1tqhcuboga9sl64zdk1z.png)



Yes! We have successfully mounted the EBS volume!

Now let us navigate to the mounted directory and create a test file to be sure we can write to the EBS volume

- Use "cd /data" to navigate.
- Use "sudo touch filetext.txt" to create a test.
- Use "ls" to list the content of the directory to be sure the file we just created is there

- You can unmount the EBS volume if needed by using the "sudo unmount /data" command

You can also delete the EBS volume by navigating to the dashboard

- Select the EBS volume
- Click on "Actions" and click "Detach volume"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7gk5hhankxd6748t3f9q.png)



Once you are done with the process, Please terminate the EC2 instance to avoid unnecessary charges


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zqa5ee37kz4y4d5p79x6.png)



- Navigate to the EC2 dashboard, select the instance
- Click on "Instance state" and click on "Terminate instance"

Viola! We have completed our project.

We have learnt how to configure an EBS volume and attach it to an EC2 instance.