# Deploying EC2 Instance with Shared EFS Storage.

As cloud infrastructure continuous to evolve, the nee for scalable, high-availability solutions becomes increasingly important. At **CloudNative Solutions,** we recently faced the challenge of deploying a distributed application that required two Amazon EC2 instances to share files in real time. To achieve this, we leveraged **Amazon Elastic File System \[EFS\],** a fully managed, scalable file storage service thar allows multiple EC2 instances to access shared data seamlessly.

In this blog post, I’ll walk us through the steps we took go deploy EC2 instance with shared EFS storage, configure security groups, and ensure high availability in our cloud environment. it will help us understand the process and apply it to our project.

**Step by step Implementation**

1\] **create an Amazon EFS File System**

login to the AWS Management console and Navigate to the EFS service and click create file system. give the file system a name and leave the VPC{ virtual private cloud }at default, click on the customize button.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740525019952/4441631c-f131-445c-8407-9b12b671d307.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740525095498/b4265027-7979-4458-bfd2-74da22983d6c.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740525222831/6b8feed8-15e6-4cc5-b2de-bd2893d21dbc.png align="center")

we have four steps:

File system setting

Network access

File system policy

Review and create.

a\] File system setting: configure the file system, the Lifecycle management, kms key should be left in the default state

b\] Performance settings: Set the performance mode to general purpose \[default\], and enable encryption at rest for security.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740561455972/cec1e6b1-8141-4d1b-ad69-79a51dcfc876.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740561531225/c00dd639-264a-4e9e-a34f-206fab3639b2.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740561575294/da040e5f-df65-4f56-8322-c104936186b6.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740561803170/5d8a236e-4388-4982-bba0-36f14637fabf.png align="center")

c\] Mount target should be left at default static of the VPC .

d\] Select **enforce in-transit encryption for all client** on the Policy option and move to review and create, the review page contains all the command we have done so far like a summary of the whole command and click on create, EFS is ready

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740563367121/8c0d51d5-8689-4511-8299-446a83747d9f.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740563416236/eb75bfb6-5c51-40ce-92cd-ab1508c545b6.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740563439900/004029b6-bbc3-42da-ab39-3bbe688e147c.png align="center")

The EFS has been successfully created

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740563927787/92a179e2-8558-49f9-8071-07c1cef76afb.png align="center")

**2\] CREATING OUR EC2 INSTANCES**

**a\]** On the search bar type EC2 and click on it

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740564412994/45c4966a-aceb-4799-a29c-5f008986e5d9.png align="center")

b\] Select the launch instance and give it a unique name, on the Amazon Machine Image we select **Amazon Linux.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740564916098/880160d5-48c5-4a0a-aedd-d39f3861c52f.png align="center")

c\] Description should be left at default Instance type: **t3.micro** was selected and the Key Pair was left at processed without a key pair

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740565729322/b400e55e-baea-46b7-b774-31b70a629923.png align="center")

d\] At the Network setting, click Edit. The VPC should be left at default note that the VPC was automatically generated by the AWS, leave others at their default state and move to firewall.

e\] Firewall \[security group\] select create a security group and give it a name and leave others in default state and launch your instance

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740567352528/bc814382-764d-4f5a-a0f4-c277fbf17602.png align="center")

Once the instance is successfully launched, use same process to launch a second instance.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740568386099/2b93d236-be2b-467d-a09a-dcd4810a104b.png align="center")

3\] ADDING RULES TO THE SECURITY GROUP

a\] We need to add certain rules to the security group to enable it function smoothly with the EFS, and to achieve these go to the **Network & security** and click on **Security Group**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740654069596/d149a2de-8cd8-4a5f-b060-122ea1c9d15c.png align="center")

**NOTE** that a security was created by default when we were creating our EFS

**b\] During** the creation of our two instances we created two security group **Petusco sunset and Browngroup** respectively.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740657676244/cce47ce5-daf7-4fd5-b892-5f99ccb11deb.png align="center")

c\] select the default security group, and click on Edit inbound Rule

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740658500945/3087504f-dae7-4696-9c5f-cdd279c26a70.png align="center")

d\] click on the Add Rule and under type, click the drop-down and select **NFS.** Also select the security group for that exact instanc, when you are done click on SAVE RULES

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740658901408/785392b5-38ee-4693-bade-7fea126e2ae1.png align="center")

**4\] MOUNTING OUR EFS AND CONNECTING OUR TWO INSTANCES**

a\] Locate the first instance created, highlight it and click on connect a new page opens scroll down and also click on Connect.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740660234308/8b24a49c-d5c0-492a-af83-e83a73817ae6.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740660267259/b45c6085-88e0-41a5-8abb-f840ad644130.png align="center")

**NOTE** we did not enable connect using keypair so a new page opens automaticall

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740660494827/ce5d32a2-e525-4ef3-933e-901ab9f8678f.png align="center")

b\] At this stage we need to install the **Amazon EFS UTILITIES** using the command code **sudo dnf install -y amazon-efs-utils** and enter, when installed successfully it will like that below. Next step is to create a directory by using the command code **sudo mkdir \[directory name\].** we used **efs.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740660809772/54f0f1d1-0a05-45a7-9eca-9a8a265cc390.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740660854259/5b2a0ff8-c5a2-458f-ad90-564ba3e2abb8.png align="center")

c\] Next step is to attach our EFS so to do this move back to your EFS console and click on attach

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740661182489/df60cbf9-c75c-481a-abd1-e92112ed6343.png align="center")

d\] Copy the mount command code under USING THE EFS MOUNT HELPER.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740661418988/a4189992-88c5-4556-9d7a-0f9b3a0dc0ab.png align="center")

e\] Return to your terminal paste and click enter to run the command.

f\] Run this command code **df -T** to check if you have successfully mounted the EFS

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740661723070/ea10a43f-bb8e-4e78-9dbe-0c06d8c28152.png align="center")

g\] The diagram below shows that our EFS was mounted successfully.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740663824055/771a4a83-6b1a-406b-8ea1-866cc1e2ff22.png align="center")

h\] At this point we need to change ownership of the directory since the default owner is the root user which does not give us the permission to execute any command, To achieve that use this command code **sudo chown ec2-user efs/** and click enter. This command automatically makes you the full owner of the directory.

**NOTE: sync enter** helps ensures that everything is fine.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740664389811/5efeef4f-12dc-4ffc-907f-bcce0729532d.png align="center")

i\] Now to get into your created directory type **cd \[your directory name\]** we used efs.

At this point you can create files and also download any type of files into your directory, To get that done use the command **sudo touch \[file name\].** we created a file named ales.we also installed WordPress document into it, To get that done use this command **wget** [https://wordpress.org/latest.tar.gz](https://wordpress.org/latest.tar.gz)**.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740669466793/0d96f035-88f7-4544-af31-04a7c40572f5.png align="center")

J\] At this point we can comfortably run **ls -l** command to see the permissions on our file and also run **ls** command to confirm the files in our directory

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740669816040/d3646655-86f3-4d9c-9c55-b0f000be1b4b.png align="center")

k\] At this point we need to connect the second instance

**Note** when the second instance is connected successfully, we will be able to view the same files in the directory in the second instance that has been connected to our EFS.

Follow same step we used in connecting the first instance once the EFS is mounted it will look like the diagram below

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740701199520/f9576112-842e-4d21-a0d7-4a3a7b15429e.png align="center")

l\] we can then enter our directory by using the command **cd\[name of directory like we did in first instance\]** type **ls** command to see the list of the files in the director, because we were able to mount our EFS on the second instance we will be able to see files saved in it with the help of the first instance

**NOTE** we did not create any file in the second instance but we were able to see files created in the first instance in the second instance which shows it was connected successfully.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1740702063767/ebbdce6d-b965-4155-9016-b3f7433bca62.png align="center")

By following these steps, we successfully deployed two EC2 instances with shared EFS Storage, this set up provides a scalable and highly available solution for future growth. stay safe and l hope you visit again for more enlightenment, Feel free to drop a comment share and like Thank you as you do so…
