Persisting Your Data with Elastic Block Storage
We all know EBS is a storage for EC2 instances by default, and also suitable for storing data that requires frequent access, such as databases and application files
EBS volumes can be used for a variety of use cases, such as storing application data, running databases, and hosting websites. They can be configured to support different performance requirements, from low-cost and low-performance storage to high-performance storage with low latency and high IOPS (input/output operations per second).
EBS volumes can also be used with features such as snapshots, which allow you to take point-in-time backups of your volumes, and encryption, which provides an additional layer of security for your data. EBS volumes are a key building block for many AWS workloads and are widely used by businesses of all sizes.
However, one of the major challenge when using amazon ebs storage instance is persistency of data, this means termination of instance will result immediate data loss.
Well, we can tackle that by using ebs volume created separately for our instance, this will preserve our data, and can also be re-usable on another instance, though ebs storage one to one, or many to one relationship with the instance, this mean only one instance at a time can connected to an ebs volume, and also an instance can have more than one volume connected to it.
lets demonstrate this, by launching an ec2 instance
Choose an os : ubuntu
Instance type: t2.micro
create a security group that allows inbound connection on ports 22 and 80 from anywhere.
copy the code below and paste to the user data box
#!/bin/bash
sudo apt update
sudo apt install apache2 -y
we are good to go let launch our instance.
this is what our instance dashboard look like, copy the public ipv4 address to your browser first to see what our page look like.
now ssh into the instance and lets make more changes to the web page
now lets create another ebs volume and attach it to our running ec2 instance.
note : to be able to attach an ebs volume to an instance, it must be created in the same region and availability zone as the ec2 instance.
To achieve our goal, we will perform some certain operation on the volume which is, preserving our data even after the instance is dead or terminated. We will Partition, format and mount our volume to the instance, so lets see what these terms all are.
Partitioning, mounting, and formatting are all important steps in managing storage devices such as hard drives, solid-state drives (SSDs), and USB drives, and making them accessible to the operating system and applications.
- Partitioning: Partitioning is the process of dividing a storage device into multiple sections or partitions. Each partition can be thought of as a separate “virtual” drive within the physical device. Partitioning is useful for several reasons, including organizing data, separating the operating system from user data, and creating multiple bootable partitions.
- Mounting: Mounting is the process of making a partition or storage device accessible to the operating system. When a device is mounted, the operating system can read and write data to it. Mounting can be done manually or automatically when the system boots up.
- Formatting: Formatting is the process of preparing a storage device for use by creating a file system on it. A file system determines how data is organized and accessed on the device. Formatting is usually done when a new device is first used, but it can also be done to erase all data on a device and start fresh.
Partitioning
lets run the code below to see all our existing volumes
sudo fdisk -l
to partition our volume, run the code below and lets partition this volume!
#sudo fdisk <volumename>
sudo fdisk /dev/xvdf
now follow the command prompt to complete our partitioning of the volume
and finally we confirm the partitioning of the volume
Formatting
sudo mkfs.ext4 /dev/xvdf
we are formatting the volume to ext4 format, there many of format available, we are choosing this format
Mounting
finally, let mount our volume and get it to work
sudo mount /dev/xvdf <filepath>
although, there is a little problem with our mount, we have only done a temporary mounting which means, if we logout of the os, mount is loss. now, lets take a look into how we can mount permanently
sudo vim /etc/fstab
and the code below to the file
#devicename path 0 0
/dev/xvdf /var/www/html 0 0
let verify, which volume is mount to which directory
and voila! we have a permanent mount, if this instance dies we can re use it to keep our newly instance run our website
Thank you
Barakat Usman is a passionate Cloud and DevOps Engineer with a deep understanding of cloud technologies and a strong commitment to optimizing and automating software development and deployment processes, staying up-to-date with the latest trends and advancements in cloud computing and DevOps, and most importantly sharing her skills and knowledge in the field.