How to Set Up AWS EFS for EC2 Instances


Introduction to AWS EFS
Amazon Elastic File System (EFS) is a scalable, fully managed file storage service that allows multiple EC2 instances to access and share data simultaneously. It supports the NFS (Network File System) protocol, making it easy to create a shared file system across multiple EC2 instances.
Why Use AWS EFS?
Shared Storage: Multiple EC2 instances can access the same file system concurrently.
Scalability: Automatically scales as files are added or removed.
Durability: Data is stored across multiple Availability Zones (AZs) for high availability.
Managed Service: AWS handles maintenance, backups, and scaling.
Step-by-Step Guide: Setting Up AWS EFS for Two EC2 Instances
Step 1: Create an EFS File System
Open the AWS Management Console.
Go to Elastic File System > Create File System.
Set the following:
Name: Enter a name for your file system.
Performance Mode: Choose "General Purpose."
Throughput Mode: Set to "Bursting" (default).
Under Network Access:
Choose the appropriate VPC and subnets.
Ensure the security group allows inbound NFS traffic on port 2049.
Click Create to launch the EFS.
Step 2: Launch Two EC2 Instances (Red Hat Linux)
Go to EC2 > Launch Instance.
Choose Red Hat Enterprise Linux (Free Tier).
Select the t2.micro instance type.
Configure the network to match the EFS VPC.
Set up security groups to allow traffic on port 2049 (NFS).
Launch both instances.
Step 3: Mount EFS to EC2 Instances
Install the NFS client on both instances:
yum install -y nfs-utils
Create a directory for the mount point:
mkdir /mnt/efs
Mount the EFS file system using the EFS DNS name:
mount -t nfs4 <EFS DNS NAME>:/ /mnt/efs
To automatically mount on reboot, add this line to
/etc/fstab
:<EFS DNS NAME>:/ /mnt/efs nfs4 defaults,_netdev 0 0
Step 4: Test File Sharing Between Instances
Create a test file from the first instance:
echo "Hello from Instance 1" > /mnt/efs/testfile.txt
Access the file from the second instance:
cat /mnt/efs/testfile.txt
If the output shows "Hello from Instance 1," the shared storage is working correctly.
Use Cases for AWS EFS
Web Applications: Store shared assets like media files.
Big Data and Analytics: Store and process large datasets.
CI/CD Pipelines: Share build artifacts between instances.
Machine Learning: Centralized storage for model training and data.
Cost of AWS EFS
Storage Costs: Charged based on the amount of data stored ($0.30/GB/month for Standard).
Access Costs: Charged for read and write operations ($0.30 per 1 million requests).
Throughput Costs: Additional costs for provisioned throughput if needed.
Free Tier: AWS offers 5 GB of EFS storage for free each month.
Conclusion
AWS EFS makes it easy to share files between multiple EC2 instances with high availability and scalability. By following these steps, you can quickly set up a shared file system using Red Hat Linux instances within the AWS Free Tier. EFS is ideal for use cases like web hosting, data analysis, and application development.
Subscribe to my newsletter
Read articles from Dipak Mali directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Dipak Mali
Dipak Mali
Hello, I'm Dipak, a Junior Software Engineer with a strong foundation in React JS, Python, and Java. I’m a working professional focused on developing efficient software solutions in the dynamic adtech industry. Beyond core coding, I specialize in RESTful API design, web development, and have hands-on experience with technologies like Spring Boot, MySQL, Docker, and AWS cloud services. I thrive on creating scalable, user-centric applications and enjoy tackling complex problem-solving challenges. Eager to collaborate, I’m passionate about innovation, continuous learning, and making a meaningful impact through technology.