Elastic File System (EFS) Multi-Instance Deployment: Streamlining File Sharing Across AWS Instances
Tags: aws, storage, awscommunitybuilder, awsefs
Published: 2024-01-19 19:00:11

In this article, we'll dive into AWS Elastic File System (EFS), a robust storage service that sets itself apart by enabling simultaneous mounting on multiple instances, offering a heightened level of flexibility. If you missed our exploration of Elastic Block Store (EBS), you can find it [here](https://dev.to/fowokzz/exploring-awss-elastic-block-store-a-step-by-step-beginners-guide-52f2).

Now, let's proceed with a comprehensive overview of the steps involved:


####1. Create EC2 Instances
Similar to the process for Elastic Block Store (EBS), I provisioned servers with no special configurations, running on **eu-north-1b**. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f7zb0e735jyqhmeilkuk.png). 


####2. Create File System
In the AWS console, search for Elastic File System service and click on **"Create file system"**.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3kt3chtjhgqrpborbop.png)

Choose between quick creation with default settings or customize for specific configurations
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r00azmqi2j1lrdu1u41o.png)

For customization, follow the detailed steps below:

#####Step 1: File System Settings

- File Name: **efs-demo**
- File System Type: Regional (redundant across multiple Availability Zones)
- Automatic Backups: Disable (for now)
- Lifecycle Management: Default
- Performance Settings: Default
Click on **Next**.

#####Step 2: Network Access 

- Specify Mount Targets for each Availability Zone where instances are running.
- Select the VPC and subnet ID of your instances. (EFS is a VPC specific service)
- Specify a security group which will act as a firewall for Mount Targets.
Click on  **Next**

#####Step 3: File System Policy 
This is very optional setting but you can configure different file system policies if you want but for this demo, we leave as default and proceed to review and create the file system 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plv8zmf9fnzegcdz58qs.png)


####3. Mount File System
Access the terminal with connections to both Instances A & B.
Starting with Instance-A, _when working with EFS, Amazon has made it really easy if you're using an Amazon Linux Image, you can install a package they created called **Amazon-EFS-utils**_
To install this package, Run `sudo yum install -y amazon-efs-utils`
Repeat this command on Instance-B.

Now that we installed the EFS package, we want to mount that file system we created in the console. 
To see a list of installed efs packages; Run `sudo mount. + tab `

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ncer5epu4gp0c798guq.png)
We can see a .efs after running that command because we installed the amazon efs utils package. 

To mount a file system, you need 2 things 
- efs instance id
- a folder/directory to mount on 
To get the efs instance id, go in to the console, click on the efs instance and copy the id as indicated in the image below 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qifvef7johfycyocpnw5.png)

Now that we have all we need, to mount file system, Run `sudo mount.efs efs-instance-id /efsdemo/`

####SIDE NOTE: I went through this so you don't have to

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/60784ooo8l5x6dx8jw73.png)
If encountering mounting errors, ensure that the mount target security group has an inbound rule allowing NFS access from the EC2 security group. Edit the inbound rule accordingly.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tr3ot4d2s3u2cgta3oly.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/74i25jb771w0lvep1vca.png)
Once the error is resolved, running `df -h` should confirm the successful mounting of the file system at **/efsdemo**.

At this point, we technically can use our EFS file systems but the changes made would not persist through a reboot


####4. Persist Data
To ensure data persistence through reboots, open the fstab file using the command `sudo nano etc/fstab`. 
Provide essential details such as the file system ID, the mount point, the file system type, and additional configurations sourced from the [AWS EFS Mount Configuration](https://docs.aws.amazon.com/efs/latest/ug/mounting-fs-nfs-mount-settings.html)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdrw0v35nvbwle0hc596.png)

After completing these configurations, a system reboot should automatically mount the file system, ensuring seamless data persistence.

To verify the persistence, create a file within the mounted directory using the command: 
`sudo nano efsfile.txt` and input the desired content.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9nycxf0mwy58qumkgmmt.png)

####5.Simultaneous Mount on Instance-B
Unlike EBS, EFS supports simultaneous mounting on multiple instances. Proceed to the second instance and replicate the process performed on Instance-A. Execute the following commands:

```
sudo mkdir /efsdemo
sudo mount.efs fs-0b058364663762fc9 /efsdemo
df -h
cd /efsdemo/
ls 
cat efsfile.txt
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7w5c3dvr9xfqvqs48le9.png)
The successful execution of these commands on Instance-B demonstrates that the file created on Instance-A is accessible on Instance-B.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7nmjgl7bbmql0iefh37b.png)
Additionally, creating a new file **efsfile2.txt** on Instance-B is reflected when inspecting the contents on Instance-A.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eo3syo3gwcraahcdq0vs.png)
This capability underscores the flexibility and convenience offered by AWS Elastic File System for simultaneous data access across multiple instances.