AWS/Azure/OpenShift

How to add an EBS volume to AWS EC2 via the AWS Console and CLI

EBS-EC2

Amazon’s Elastic Block Store (EBS) is a block-storage service provided by the Amazon Elastic Compute Cloud (EC2) to store the data which can be kept on the EBS servers even if the EC2 instances are shut down or not working. An EBS data volume can be attached, detached dynamically, irrespective of the EC2 to which the volume is attached. EBS is an easy-to-use service that provides high scalability, availability (99.999%), and protection against failures. In this article, we will see how you can add an EBS volume to AWS EC2, using AWS CLI Commands. Kindly refer to some of these related guides: AWS ec2 and multi-cloud, How to Deploy MVC Application to AWS EC2 Using RDP,  How to Deploy Dynamic Website to AWS EC2, How to host Static Website and Versioning on AWS S3, How to deploy an Angular App to AWS S3

There are 2 ways for adding an EBS volume to an EC2 instance.

  1. Creating and attaching an EBS while creating an EC2 instance.
  2. Adding EBS volume to an already created EC2 instance.

Let us first install the AWS CLI. Remember to install the AWS CLI according to your operating system.

Installing AWS CLI in Linux

Open the command prompt, and execute the following command. Here is a guide on how to configure AWS CLI, and how to work with AWS CLI via the Command Line Interface.

curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "aws-ec2-ebs.zip"

Unzip the package that has been downloaded in the above step.

unzip aws-ec2-ebs.zip

Run the installation of the package using the command.

sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws

After installing the AWS CLI, configure it so that it will be able to connect to the AWS account. To configure the AWS CLI use the following command

aws configure

It will ask for the AWS credentials, Access Key ID, and Secret Access Key after entering your credentials it will get connected to the AWS account. Once the CLI is configured follow the steps below to either create an EC2 instance and then attach an EBS Volume to it.

Creating an EC2 instance

Create a key pair by writing the following command in the AWS CLI,

aws ec2 create-key-pair --key-name TempKey

Here “TempKey” is the key name you want to give to your key. This key pair will be used to create an EC2 instance.

In the console, create the security group by typing the following command.

aws ec2 create-security-group --group-name ec2-aws-allowall --description “Allow all traffic for ec2”
image-27

The security group is the instance-level firewall security provided by the AWS to control the incoming and outgoing traffic for the instance. Copy this security group id from the Type the following command for launching an Amazon Linux AMI2 instance with the key pair and security group that has been created before. Remember to replace AMI ID, subnet ID, and the security group ID in the command.

aws ec2 run-instances --image-id <REPLACE_AMI_ID_HERE> --instance-type t2.micro --count 1 --subnet-id <REPLACE_SUBNET_ID_HERE> --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=aws-ec2-instance}]' --security-group-ids <REPLACE_SECURITY_GROUP_ID_HERE> --key-name TempKey

You should see an output similar to the following. Please copy and save the instance id.

image-28

Creating an EBS volume

Let us now use a command to create an EBS volume of 30 GB. Use the same subnet name (for example us-east-1a or eu-west-1b) of which you have supplied the id in the above command.

aws ec2 create-volume --availability-zone <REPLACE_WITH_THE_SUBNET_NAME> --size 30 

You Should see an output similar to the following. Please copy the volume ID here.

image-29

Attaching the EBS to the EC2 instance

Add the EBS created to the instance that has been created before with the following command,

aws ec2 attach-volume --instance-id <REPLACE_WITH_INSTANCE_ID> --volume-id <REPLACE_WITH_THE_VOLUME_ID> --device /dev/xvdh
image-30

You can write your preferred device name instead of /dev/xvdh. After executing the above command an EBS volume will be added to the EC2 instance that we have created. Thus keeping the data protected, available, and persistent.

You can also do this step on the AWS Console. Find the volume created using the volume Id. It should be in the ‘Available State’. Select the volume, click on the action button and Choose the Attach Volume option.

image-32

Choose the EC2 instance from the list, type in the device name, and click the Attach Volume button.

image-31

We have now successfully created an EC2 instance and attached an EBS volume to it. I hope this article helps you. Let me know if you have any questions.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x