Working with AWS Cloud from CommandLine — Part3
In this article, we will understand how to create an EBS Volume and connect it an ec2 instance using aws cli.
In previous part, we learnt how to launch an ec2 instance. This instance comes with a Root EBS volume, which stores all your data including the OS as well. But one of the drawbacks of this instance is that, as soon as the instance is terminated or deleted, the root volume also gets deleted, and hence we lose all our data. This type of storage is called ephemeral storage.
In this article we will learn how to create and attach a permanent storage to an instance. So that even if in the case, the instance gets terminated, no data is lost.
Before we proceed further, lets take a look at what are the prerequisites, for this task. The Prerequisites for this task are:
- An AWS Account.
- AWS CLI pre-configured in the system.
To verify if you have an AWS CLI configured in your system, run this command in command prompt or Terminal:
Command: aws --version
If the output is command not found or this command is not recognized as internal or external command, that means aws cli is not configured in your system.
Correct Output:
If you do not have aws cli configured in your system, follow the Part-1 of this series.
- An ec2 instance.
To understand how to launch an ec2 instance, follow the Part-2 of this series.
EBS or Elastic Block storage.
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, because of this even if the instance gets deleted or is terminated, the data stored on it remains persistent. 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.
Features of Amazon EBS
- EBS volumes are created in a specific Availability Zone, and therefore can only be attached to instances in the same Availability Zone.
To make a volume available outside of the Availability Zone, you can create a snapshot and restore that snapshot to a new volume anywhere in that Region. You can copy snapshots to other Regions and then restore them to new volumes there.
- Amazon EBS provides the following volume types: General Purpose SSD (
gp2
), Provisioned IOPS SSD (io1
andio2
), Throughput Optimized HDD (st1
), and Cold HDD (sc1
).
Not to worry, with all the new terms, We will learn about them in detail in upcoming articles.
For now, lets get started.
How to Set Up an Amazon EBS?
Note: Since, EBS created in one availability zone is only available within that availabilty zone. So make sure the availability zone in which you launch your EC2 instance in, you select the same availability zone for EBS as well. For availability zone we had used subnet-id of the availability zone to specify.
But, in case, if the instance and EBS Volume are present in different regions or availability zones, We solve this issue using Snapshots, follow the next part of this series, to understand Snapshots and how to solve this issue using this.
Step 1: Creating an EBS Volume.
To create an EBS volume of size 1 GiB in the same availabilty zone that of instance(ap-south-1a).
Command: aws ec2 create-volume --size=1 --availability-zone=ap-south-1a
Step 2: Attaching the Volume to the instance.
Command: aws ec2 volume-attach --volume-id=<> --instance-id=<> --device=/dev/sdf
We can see the state of the volume has changed to in-use.
Step 3: Making the Volume available to use.
We cannot directly use the Block storage devices, such as EBS. To make the EBS usable, these are the steps to follow:
Step 3.a: Create Partitions.
To view the drive attached to the instance, and partitions, use
Command: sudo fdisk -l
There are two volumes connected, first is /dev/xvda which is the root volume which contains our OS. The second volume is the /dev/xvdf (device name: /dev/sdf that we gave, internally gets changes into such names) which we have attached.
The drive /dev/xvda conatins two parts: /dev/xvda128: this is the reserved part on the disk, and one partition named as /dev/xvda1.
As we can see /dev/xvdf doesnot contain any partitions yet.
So, we will create partitions. For this task, we will create only 1 partition of complete volume capacity. To create a new partition,
Command:sudo fdisk <volume_name>
Press n(for creating a partition), then p(to specify the type of partition-primary), and keep pressing enter till the time(to keep the other parameters default), the output comes “Created a new partition……..”. After this press w to confirm the partition.
We can see that one partition named /dev/xvdf1 of size 1023 MiB is created.
Step 3.b: Format the Partition created.
Command: sudo mkfs.ext4 <partition_name>
Step 3.c: Mount the Partition to a directory.
Command: sudo mount <partition_name> <directory>
This command doesnot return any output. To confirm that it is mounted to the directory.
Command: df
We can see that /dev/xvdf1 is mounted on a directory named as /volume-2.
Validation
Objective: To demonstrate that the data is stored permanently on the external EBS volume that we have attached.
For this I have created two files.
A file named volume-1-file.txt is present in the default root volume directory volume-1(epehemeral storage) and another file named volume-2-file.txt is stored in externally attached EBS volume(permanent storage) directory volume-2.
Now if we delete or terminate the instance,
We will observe that, the root volume will get deleted while the external volume that we created will still remain available.
Now if we launch another instance within the same availabilty zone(subnet-id of az: ap-south-1a) and reattach the created EBS volume.
- To launch an instance in ap-south-1a
Command: aws ec2 run-instances --image-id=ami-026669ec456129a70 --instance-type=t2.micro --count=1 --key-name=Task1 --subnet-id=subnet-b0bdb8d8 --security-group-ids=sg-0cc9340aa1efbce35
- To attach an external EBS volume.
Command: aws ec2 attach-volume — volume-id=vol-01e376047638a4f57 — instance-id=i-0c2e43142115991c4 — device=/dev/sdf
Note: When we attach a block storage again, which has already been partitioned, formatted and mounted earlier, we dont need to create partitions again, and format them.
It already comes with the previously created partitions.
We can see that /dev/xvdf1 partition is still present in the drive.
To use this volume, we just need to mount this to a directory. For this. we will create a directory say volume-2.
Then we will mount the partition to this directory.
We can see that the file named volume-2-file.txt that we created earlier, is present in this instance, while the file named volume-1-file.txt present in volume-1 directory, gets deleted permanently along with the directory.
And thats it. Done!
In next part, we will learn about Snapshots to understand how to work with EBS volume which is available in different availability zone or region as that of the instance.
📝 ️Few Last Words
→ I will be sharing many article’s on Integrating multiple tools and technologies Cloud Computing, DevOps, Big Data Hadoop, Machine Learning etc. in Upcoming days .
→ Follow me on Medium for more Article’s on Research based and integration of new new tools and technologies .
For Furthur Queries, Suggestion’s Feel Free to Connect with me On Linkedin
For any doubt or query, let me know in the Response section!
If you like it then Clap & Share ..
Thankyou EveryOne For reading .!! | arigatou gozaimasu. sayonara!