Seamless File Sharing in the Cloud: Deploying EC2 Instances with Shared EFS Storage

Introduction:

Imagine you and a friend are working on a shared document—like a group project or planning a surprise party. You both need to see updates in real-time, add your input, and ensure nothing gets lost in endless email threads. Now, scale that up to the cloud where two servers (EC2 instances) need to work together seamlessly, sharing data without hiccups. That’s where Amazon Elastic File System (EFS) comes in like a magical shared folder both servers can tap into at the same time.

In this guide, we’re going on a hands-on journey to set up shared storage for two EC2 instances using EFS. Whether you’re new to AWS or just looking to refresh your skills, we’ll break it down step-by-step, with real-world examples and simple explanations. By the end, you’ll have a setup that ensures smooth, real-time file sharing no cables, no confusion, just cloud magic!

So grab your favorite drink, fire up your AWS console, and let’s dive in! 🚀

Step 1: Create an Amazon EFS File System

a. Sign in to the AWS Management Console.

b. In the search bar, type "EFS" and select "Amazon EFS" from the services list.

c. Click on "Create file system."

d. Assign a name to your file system for easy identification and click on “Customize“. I named mine “EFS- new“

e. In the Customize section, you’ll find four steps. You can skip the first two and proceed directly to Step 3 without making any changes.

f. Step 3 focuses on policy options. Here, I enabled encryption in transit, which safeguards data as it moves between two points in a network, ensuring secure communication throughout the transfer process.

g. Now in Step 4, Click on "Create file system." Once created you’ll be able to see your new EFS

Step 2: Creating EC2 Instances

a. With our EFS successfully created, the next step is to launch an EC2 instance (a virtual machine). Begin by searching EC2 in the search bar.

b. Once there, select on “Launch Instance

c. For the Name: Assign a distinctive name to your EC2 instance.

Application and OS Images (AMI): Select Amazon Linux as your operating system.

d. Description: I kept the default setting.

Instance Type: I selected t3.micro, but you’re free to choose any other free-tier eligible instance type that suits your needs.

e. In the Key Pair section, click the dropdown menu and select Proceed without a key pair.

f. In the Network settings section, click Edit. AWS automatically assigns the VPC created for the EFS as the default, so you can leave all settings unchanged and proceed to the Firewall (security groups) section.

Select Create security group, assign it a unique name, and optionally add a description to specify its purpose.

g. Leave all remaining settings at their defaults and click Launch Instance. Your instance is now successfully launched!

Repeat the same steps to create the second instance.

h. Once both EC2 instances are created they should both be active on the dashboard.

Step 3: Setting Up Security Group Rules

a. Once the instances are launched, the next step is to modify the security group rules to allow communication between the EC2 instances and the EFS by enabling traffic on port 2049.

To do this, navigate to Network & Security and click on Security Groups.

b. The Security Groups tab displays a list of all the security groups created.

  • Default: Automatically generated during the EFS configuration.

  • SG-machine-1: Created while launching the first EC2 instance.

  • SG-machine-2: Created while launching the second EC2 instance.

c. Select the default security group and click Edit Inbound Rules.

d. Click Add Rule.

In the Type dropdown menu, select NFS, then choose the security group associated with the specific instance, as illustrated in the diagram.

e. Repeat the above process for the second instance, ensuring you select the correct security group.

Once done, click Save Rules to apply the changes.

f. After editing, the inbound rules on the default security group should appear as shown below.

Step 4: Mounting the EFS and Connecting It to the EC2 Instances

a. At this stage, I navigated back to the Instances section, selected one of my instances, and clicked Connect.

b. Once the connection has been established this is what it looks like.

c. 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:

d. Next, you’ll need to create a directory on your EC2 instance where the EFS will be mounted. Run the following command in your terminal:

sudo mkdir efs

Here, efs is the directory name, but you can choose any name you prefer. This folder will serve as the mount point for your EFS.

e. Head back to your AWS Management Console, Navigate to your EFS dashboard and Click on Attach to view the mount instructions.

👉 Tip: The console provides you with a pre-generated command that includes your file system ID—copy it for the next step!

f. Go back to your terminal and paste the EFS mount helper command you copied from the console.

Press Enter to execute the command and mount the EFS to your directory.

g. 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.

df -T

This command displays the file system type and mount points, allowing you to confirm that your EFS is properly attached.

h. 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.

i. To gain the necessary permissions, use the chown command:

sudo chown ec2-user efs/

✅ Once you run this command, the ownership of the efs directory is immediately updated, granting you the required access.

j. Navigate into your directory by running:

cd efs/

Inside the directory, you can create or add any type of file. To create a new file, use the touch command:

touch stingray

In this example, I created a file named stingray and also added a WordPress document to the directory.

To confirm the files within the directory, run:

ls

✅ The output will display the files you’ve added, as shown below.

Step 5: Checking the connected instances

Now, let’s connect the second EC2 instance and verify if we can access the same files stored in the EFS-mounted directory from the first instance.

a. Connect to the second EC2 instance using the same method as the first one.

b. Install the Amazon EFS utilities (if not already installed):

sudo dnf install -y amazon-efs-utils

c. Create a directory (use the same name as the one on the first EC2 instance for consistency):

sudo mkdir efs

d. Confirm the mount by running:

df -T

Result:
As shown in the diagram below, the EFS has been successfully mounted on this instance, enabling both EC2 instances to access the same shared storage in real time.

e. Navigate to your directory using the cd command:

cd efs

Once inside, run the ls command to view the list of files:

ls

💡 Note:
You don’t need to create any files on this instance. Since the EFS is successfully mounted, you’ll automatically see all files stored in the shared file system, including those created from the first EC2 instance.

🚀 Notice how Instance 2 seamlessly accesses the same files created and saved by Instance 1 on the EFS! This demonstrates the power of Amazon EFS in enabling real-time, shared file storage across multiple EC2 instances.

I hope this guide has been helpful and that you enjoyed following along to achieve your desired result. Setting up shared storage in the cloud doesn't have to be complicated—and you’ve just proven that by completing this step-by-step process!

Thank you for reading! I appreciate you stopping by and look forward to sharing more insightful content for your cloud journey.

👉 If you found this helpful, please like, comment, and share—your support means a lot!

0
Subscribe to my newsletter

Read articles from Di Nrei Alan Lodam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Di Nrei Alan Lodam
Di Nrei Alan Lodam