Deploying EC2 Instance with Shared EFS Storage.

Nweke HenryNweke Henry
6 min read

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.

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.

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

The EFS has been successfully created

2] CREATING OUR EC2 INSTANCES

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

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

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

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

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

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

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.

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

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

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.

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

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.

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

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

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

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

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.

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.

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

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

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.

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…

0
Subscribe to my newsletter

Read articles from Nweke Henry directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nweke Henry
Nweke Henry