Real-Time File Sharing: Connecting EC2 Instances via Amazon EFS


In today’s cloud-driven world, teams often need multiple servers to access and update the same files in real time. Amazon Elastic File System (EFS) makes this easy by providing a shared, scalable storage solution that multiple EC2 instances can use simultaneously. Instead of manually managing storage, EFS grows and shrinks automatically as needed, ensuring seamless access without complexity. It’s perfect for web hosting, content management, and data processing, where consistent and reliable file sharing is essential.
In this guide, I will walk you through setting up Amazon EFS and attaching it to EC2 instances step by step. Whether you're running a website, collaborating on files, or handling machine learning workloads, integrating EFS simplifies storage management and improves efficiency. By the end, you’ll have a fully functional shared file system that enhances your cloud infrastructure with minimal effort.
A. CREATE AN AMAZON EFS
Sign in to your AWS Management Console
In the search bar, type EFS and select EFS
Select Create file system
Give a unique name for your EFS and leave Virtual Private Cloud as default as it is created automatically. Then click Create
After creating, there are four steps under create which you can leave as default and move to step 3 which covers policy options
Check the box encryption in transit, which safeguards data as it moves between two points in a network, ensuring secure communication throughout the transfer process. Then select Next
In step 4, you click on Create under review and create to see your new EFS
B. SETTING UP EC2 INSTANCES
In the search bar, type EC2 and select EC2.
At the EC2 homepage, select Launch instance
Assign your EC2 instance a unique and recognizable name.
Select Amazon Linux as the preferred operating system for your EC2 instance.
Description: Keep the default setting.
I selected t2.micro as the instance type; however, you may choose any other free-tier eligible option that aligns with your needs.
In the Key Pair section, open the dropdown menu and select Proceed without a key pair.
In the Network settings section, click Edit. AWS automatically assigns the VPC created for the EFS as the default, so you may leave all settings unchanged and proceed to the Firewall (security groups) section.
Select Create security group, provide a unique name, and optionally add a description to define its purpose.
Leave all remaining settings at their default values and click Launch Instance to complete the process. Your instance is now successfully launched!
Repeat the same steps to create the second EC2 instance.
Once both EC2 instances have been created, they should appear as active on the AWS dashboard.
C. Configuring Security Group Rules
After launching the instances, the next step is to update the security group rules to allow communication between the EC2 instances and Amazon EFS by enabling traffic on port 2049 (NFS protocol).
To modify these settings, navigate to Network & Security in the AWS Management Console and select Security Groups.
The Security Groups tab will display a list of all created security groups, including:
Default: Automatically generated during the EFS configuration.
SGAK1: Created during the launch of the first EC2 instance.
SGAK2: Created during the launch of the second EC2 instance.
Select the default security group and click Edit Inbound Rules.
Click Add Rule.
In the Type dropdown menu, select NFS. Then, under Source, choose the security group associated with the specific EC2 instance, as shown in the diagram.
Repeat the above process for the second instance, ensuring you select the correct security group.
Once done, click Save Rules to apply the changes.
After making the necessary edits, the inbound rules for the default security group should reflect the updated settings, allowing NFS traffic on port 2049 for the specified EC2 instances.
D. Mounting the EFS and Connecting It to the EC2 Instances
On the First EC2 Instance
At this stage, navigate to the Instances section, select one of your EC2 instances, and click Connect.
Once the connection is successfully established, you should see the terminal interface of your EC2 instance, ready for command execution.
To begin, install the Amazon EFS utilities on your EC2 instance. These utilities are essential for mounting the EFS.
Run the following command in your terminal:
sudo dnf install -y amazon-efs-utils
This command uses the dnf package manager to install the EFS utilities with automatic confirmation (
-y
).✅ Once the installation is complete, you should see an output similar to the one below:
Next, you must create a directory on your EC2 instance to mount the EFS. Execute the following command in your terminal:
sudo mkdir efs
In this case, efs is the chosen directory name, but you are free to select any name you prefer. This directory will act as the mount point for your EFS.
Return to your AWS Management Console, navigate to your EFS dashboard by selecting your EFS name, then click Attach to access the mount instructions.
👉 Tip: The console will provide a pre-generated command with your file system ID—be sure to copy it for the next step!
Return to your terminal and paste the EFS mount helper command that you copied from the console.
Press Enter to execute the command and mount the EFS to your directory.
To verify that the EFS has been successfully mounted, run the following command in your terminal:
This command displays the file system type and mount points, allowing you to confirm that your EFS is properly attached.
The
df -T
command displays the file system type and mount points, enabling you to verify that your EFS is successfully attached.To check the file permissions, run the following command:
ls -l
🔎 In the output (as shown in the diagram), you’ll notice that the efs directory is owned by
root
. This ownership prevents you from executing certain commands as a regular user.To obtain the necessary permissions, use the
chown
command:sudo chown ec2-user efs/
✅ After executing this command, the ownership of the
efs
directory will be updated, granting you the required access. Then you can run the commandls - l
to confirm the change of ownership.Navigate into your directory by running:
cd efs/
Once inside the directory, you can create or add any type of file. To create a new file, use the
touch
command:touch ak1
In this example, I created a file named ak1
I also added a WordPress document to the directory by running the command:
wget
https://wordpress.org/latest.tar.gz
To confirm the files within the directory, run:
ls-l
✅ The output will list the files you've added, as shown below.
On the Second EC2 Instance
Now, let's connect to the second EC2 instance and verify if we can access the same files stored in the EFS-mounted directory from the first instance.
Connect to the second EC2 instance using the same method as the first one.
Install the Amazon EFS utilities (if not already installed):
sudo dnf install -y amazon-efs-utils
Create a directory (use the same name as the one on the first EC2 instance for consistency):
sudo mkdir efs
Mount your EFS by repeating steps 5&6 done under instance one
Confirm the mount by running
df -T
✅ Result: As shown below, the EFS has been successfully mounted on this instance, allowing both EC2 instances to access the same shared storage in real time.
Navigate to your directory using the command:
cd efs
Once inside, run the
ls
Command to view the list of files:As shown above both files, ak1 and the Wordpress are present on this instance
💡 Note:
There's no need to create any files on this instance. Since the EFS is successfully mounted, you'll automatically see all the files stored in the shared file system, including those created on the first EC2 instance.
Subscribe to my newsletter
Read articles from Jesudetan Akinyemi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
