HOW TO ATTACH AND MOUNT A NEW EBS VOLUME TO A RUNNING AWS EC2 INSTANCE
A recent encounter with service disruptions on my EC2 instance revealed a critical issue: insufficient disk space. To address this bottleneck, two potential solutions were evaluated.
Expanding the Default EBS Root Volume: A Temporary Relief
While increasing the size of the root EBS volume can offer a reprieve from storage constraints, it’s essential to consider the broader implications. This approach may:
- Impact Performance: Larger volumes can introduce latency, potentially hindering application responsiveness.
- Increase Costs: EBS volumes are billed based on provisioned size, regardless of utilization. Expanding unnecessarily can incur additional expenses.
- Raise Operational Complexity: Resizing the root volume necessitates careful planning and execution, ensuring instance type compatibility and sufficient regional capacity.
- Delete on Termination of the ec2 instance
Attaching New EBS Volumes: A Strategic Approach
A more sustainable solution lies in attaching separate EBS volumes dedicated to data storage. This method offers several advantages:
- Enhanced Persistence: Data remains secure and accessible even after instance restarts or termination.
- Improved Scalability: Additional EBS volumes can be attached as needed, enabling independent scaling of storage capacity to meet evolving requirements.
- Flexibility: EBS volumes cater to diverse application needs with various performance options (provisioned IOPS or throughput).
- Optimized Cost Management: Separate volumes allow for more granular cost control, as storage resources are provisioned and optimized based on usage patterns.
In today’s cloud-driven environment, ensuring adequate storage capacity for your EC2 instances is critical for optimal performance and application support. This article delves into the process of attaching and mounting new Elastic Block Store (EBS) volumes to existing running EC2 instances. By effectively leveraging EBS volumes, you can seamlessly expand your storage capabilities without disrupting ongoing operations.
Prerequisites:
- An existing AWS account with access to the EC2 service.
- A running EC2 instance.
- Knowledge of the instance ID or instance name.
STEPS:
- Create a New EBS Volume:
- Open the AWS Management Console and go to the EC2 service.
- In the left navigation pane, click on “Volumes”.
- Click on the “Create volume” button.
- Choose your preferred volume type (e.g., General Purpose SSD for performance, Magnetic for cost-effectiveness).
- Specify the size of the volume in GiB (Gigabytes).
- Select the Availability Zone where your EC2 instance is located (ensure it’s the same zone as your instance for optimal performance).
- Click on “Create volume”.
2. Attach the Volume to your EC2 Instance:
- In the EC2 service, navigate to the “Volumes” section.
- Select the newly created EBS volume by clicking the checkbox next to it.
- Click “Actions” and then choose “Attach volume”.
- In the “Instance” field, enter the ID of your running EC2 instance or select it from the list.
- Choose the device name where the volume will be mounted on the instance (e.g., /dev/sdf for Linux instances). You can find a list of available device names in the AWS documentation.
- Click “Attach volume”
3. Mounting the EBS Volume: Once the volume is attached, you need to connect to your EC2 instance to format and mount the volume for use.
- Identify the Device Name: Use the
lsblk
command to identify the device name assigned to the newly attached EBS volume.
- Create a Filesystem: Run this command on your terminal;
mkfs.ext4 /dev/xvdf
Replace /dev/xvdf
with the actual device name to create a filesystem on the volume.
- Create a Mount Point: Create a directory to use as the mount point for the volume.
sudo mkdir /app
- Mount the Volume: Use the
mount
command to mount the formatted volume to the created mount point.
sudo mount /dev/xvdf /app
N.B - Replace with your device name and mount point.
- Configure Automatic Mounts: Edit the
/etc/fstab
file to configure the volume to be mounted automatically on boot. Each line in/etc/fstab
defines a mount point. Add a new line specifying the following details for your EBS volume, the device name (/dev/xvdf), mount point (/app) and filesystem (ext4).
/dev/xvdf /app ext4 defaults 0 0
$ mount -a
#command to test if the fstab entry is valid and attempt to mount all filesystems listed in the file, including your new EBS volume.
- Reboot your ec2 instance (optional).
N.B
Be cautious when editing this /etc/fstab file, as errors can lead to boot issues. It’s recommended to back up the file before making changes. Confirm the configuration with the command mount -a before rebooting
By completing these steps, you have successfully set up automated mounts for your EBS volume. This ensures that the volume is readily accessible with each reboot of your EC2 instance, improving ease and eliminating any difficulties with applications or data access.
Happy Mounting !!!