Objectives
- Create an Amazon EBS volume
- Attach and mount your volume to an EC2 instance
- Create a snapshot of your volume
- Create a new volume from your snapshot
- Attach and mount the new volume to your EC2 instance
AWS EBS, which stands for Amazon Elastic Block Store, is a block-level storage service used for Amazon EC2 instances, providing high-performance and durable block device volumes for cloud servers. With Amazon EBS, you can create and attach storage volumes to instances, which persist even after the instances are stopped. These volumes can be utilized for operating systems, applications, or data storage. EBS also includes features such as snapshots for backups and encryption for enhanced security.
Features and Functions
Persistent storage:
- EBS volumes are persistent-that is, data remains even when an EC2 instance is stopped or terminated. You may detach a volume using Connect and attach it to other instances.
High performance:
- The different volume types serve various workloads, from transactional databases to large-scale data processing. Performance is measured in IOPS or throughput.
High availability and durability:
- Guaranteed by the automatic replication of EBS volumes within a single Availability Zone to prevent component failure.
Snapshots:
- You can take point-in-time snapshots of your EBS volumes, which are stored in Amazon S3 and can be used to create new volumes for backup, disaster recovery, or test purposes.
Encryption:
- EBS provides built-in encryption for data at rest and in transit, using AWS Key Management Service (KMS).
Elasticity:
- The "Elastic Volumes" feature allows you to dynamically modify the size, performance, and type of a volume without downtime to adapt to changing needs.
How it works
- Create a volume:
- You create an EBS volume in a specific Availability Zone.
- Attach volume:
- Attach the volume to an EC2 instance in the same Availability Zone.
- Use the volume:
- The volume appears to the instance as a raw block device. You can format it with a file system and mount it to store data.
- Snapshot:
- Create a snapshot for backup and store it independently of the volume.
- Detach and delete:
- You can detach a volume when you're finished with it, and delete it when you no longer need it.
Architectural Diagram:
+-------------------------+
| AWS EC2 Instance |
| "Lab" |
+-----------+-------------+
| |
| |
+-----v------+ +---v---------------+
| Root Vol | | My Volume |
| (8 GiB, | | (2 GiB,/dev/xvdf) |
| /dev/xvda1)| | |
+------------+ +-------------------+
|
+------v------+
| My Snapshot |
+-------------+
|
+------v-----------+
| Restored Vol |
|(2 GiB, /dev/xvdg)|
+------------------+
Step 1: Launch an EC2 Instance
-
Go to EC2 > Instances > Launch Instances.
-
Configure:
- Name:
EBSLab - AMI: Amazon Linux 2 (or latest Amazon Linux)
- Instance type:
t2.micro - Key pair: Create (Private key pair will be automatically downloaded)
- Network: Select default VPC and respective public subnet (or Create custom)
- Auto-assign public IP: Enable
- Security Group:
- Create a Security Group: Security group name:
EBSLab- Allow SSH (port 22) from anywhere
0.0.0.0/0 - Allow HTTP (port 80) from anywhere
0.0.0.0/0
- Allow SSH (port 22) from anywhere
- Create a Security Group: Security group name:
- Storage: Keep default (8 GiB EBS root volume)
- Name:
-
Launch the instance and wait until the state is
running. -
Note the Availability Zone (e.g.,
us-east-1a).
Step 2: Create a New EBS Volume
- Go to EC2 > Elastic Block Store > Volumes > Create Volume.
- Set:
- Volume type: General Purpose SSD (gp2)
- Size: 2 GiB
- Availability Zone: must be the same as your EC2 instance e.g.,
us-east-1a - Tag: Key =
Name, Value =My Volume
- Click Create Volume.
Step 3: Attach the Volume to Your EC2 Instance
- In EC2 > Elastic Block Store > Volumes, select My Volume.
- Click Actions > Attach volume.
- Select your
EBSLabinstance. - Device:
/dev/sdf - Click Attach volume.
Step 4: Connect to Your EC2 Instance
- EC2 > Dashborad > Instances (running)
- Select the
EBSLabinstance. - Click Connect > EC2 Instance Connect or use SSH Client from your local terminal/PowerShell
- Using EC2 Instance Connect
- Click on connect bottom of the right side
- Using SSH
- Open terminal
- chmod 400 "KeyPairname.pem"
e.g. chmod 400 EBSLab.pem - Login using an SSH client
e.g., ssh -i "AWSLabCSIT2087.pem" ec2-user@44.204.136.74
Step 5: Create and Mount a File System
- Check existing disks:
lsblk #lists information about block devices (like hard drives, SSDs, USBs, partitions).df -h #shows disk space usage in human-readable format (GB/MB) with the mounted location.
-
Format the new EBS volume:
sudo mkfs.ext3 /dev/xvdf # /dev/sdf appears as /dev/xvdf in Linux -
Create mount point:
sudo mkdir /mnt/data-store
-
Mount the volume:
sudo mount /dev/xvdf /mnt/data-store
-
Verify:
lsblk
-
Make mount persistent:
echo "/dev/xvdf /mnt/data-store ext3 defaults,noatime 1 2" | sudo tee -a /etc/fstab
-
Create a test file:
sudo sh -c "echo Full stack developer >> /mnt/data-store/file.txt"ls /mnt/data-store/ cat /mnt/data-store/file.txt
Step 6: Create an EBS Snapshot
- In EC2 > Elastic Block Store > Volumes, select My Volume.
- Actions > Create Snapshot.
- Tag: Key =
Name, Value =My Snapshot. - Click Create Snapshot.
- Wait for snapshot state to become Completed.
Step 7: Delete the File (to Test Recovery)
- On the EC2 instance:
sudo rm /mnt/data-store/file.txt ls /mnt/data-store/
Step 8: Restore Data from Snapshot
-
Create New Volume from Snapshot
- In EC2 > Elastic Block Store > Snapshots, select My Snapshot.
- Actions > Create Volume from Snapshot.
- Availability Zone: same as your EC2 instance
- Tag: Key =
Name, Value =Restored Volume - Click Create Volume.
-
Attach the Restored Volume
- In EC2 > Elastic Block Store > Volumes, select
Restored Volume. - Actions > Attach Volume.
- Select your instance. (EBSLab)
- Device:
/dev/sdg - Click Attach Volume.
- In EC2 > Elastic Block Store > Volumes, select
Mount the Restored Volume
- On the EC2 instance terminal
sudo mkdir /mnt/data-store2 sudo mount /dev/xvdg /mnt/data-store2 ls /mnt/data-store2/ cat /mnt/data-store2/file.txt
- You should see your previously created file restored!
Troubleshooting
| Symptom | Possible Cause | Fix |
|---|---|---|
| Volume not visible | Wrong AZ | Volume and instance must be in same AZ |
| Can't mount | Wrong device name | Use /dev/xvdf or /dev/xvdg for Linux |
| File missing | Wrong volume/snapshot | Ensure correct snapshot/volume is used |