Easy Step by Step Guide to Connect Amazon EFS to EC2

Easy Step by Step Guide to Connect Amazon EFS to EC2

Imagine a world where your cloud infrastructure scales effortlessly, and your EC2 instances communicate in perfect harmony—seamlessly sharing files in real time. with my guide helping you attach an (efs) to your EC2 you would be able to achieve that. Let's take a step back so I can help you understand what an EFS is.

EFS (Elastic File System) is a scalable, fully managed cloud storage service by AWS(Amazon Web Services) that allows multiple EC2 instances to access and share files simultaneously over a network. It provides elastic storage that grows and shrinks as you add and remove files, with automatic scaling and encryption for secure data storage . EFS offers performance and throughput mode to ensure high availability and durability which means your data is automatically replicated across multiple Availability Zones (AZs) for redundancy. It also supports two forms of encryptions (encryption at rest and encryption in transit) which ensures end-to-end data protection, safeguarding files whether they are stored or being accessed over the network.

EC2 (Elastic Compute Cloud) instance is a scalable virtual machine/server in AWS that provides computing power to run applications, host websites, process data, e.t.c, with flexible configurations for CPU, memory, storage, and networking.

Now to get started, the first step is to sign in to your AWS IAM user account and if you don’t have one yet, you can easily set up an account on https://signin.aws.amazon.com/signup?request_type=register

  1. OPEN UP THE SEARCH BAR AND SEACH FOR EFS

    • Navigate to the search bar and type EFS click on it

    • Click on create file system

    • Click on customize and give the efs a unique name.

    • You can decide to adjust the settings to your liking but I left everything in step 1 and step 2 in its default settings and clicked on next.

    • In step 3(File system policy) check the box which says “encryption in-transit” which protects data by encrypting it while it is being transmitted between systems, safeguarding it from interception and unauthorized access and then click on next.

    • This page allows you to review all your settings up until now, then click on create.

    • Our EFS has now been created.

  2. SETTING UP THE EC2 INSTANCES

    • Navigate to the search bar, type ec2 and click on it.

    • Select launch instance.

    • Give it a name and select Amazon Linux for the Application and OS Images

    • Leave the Description in its default setting and move to Instance type

      Here I selected t2.micro, you can choose any instance type of your choice depending on your workload.

    • Under Key pair I chose to proceed without key pair, you can choose to create a key pair .

    • Under Network settings, click on the edit button. Leave the VPC and other settings here in default and scroll down to the Firewall (Security groups).

      Create a security group, give it a name and description so you can easily identify it and click on launch instance.

    • The first EC2 instance has successfully launched. Now use the exact guide to launch the second EC2 instance.

    • This is what your dashboard should look like, you should see that it is running and the status check has been passed.

  3. ADDING RULES TO THE SECURITY GROUP

    After launching the instances, we must now edit the inbound rules which controls, secures incoming network traffic to the AWS resource such as your EC2 instance e.t.c by allowing only authorized connections, and enhances security within a network.

    • On the left side of your screen you should see a panel, go to Network & Security and click on Security Groups.

    • In this tab you should see all the security groups you’ve created on display.

      The security group named default was automatically created for the EFS while it was being configured and because it is the security group for the EFS, that is why we would be editing and adding the rules to it, to ensure the efs is successfully mounted.

      While SG_Demo1 and SG_Demo2 were created simultaneously as my first and second EC2 instances were being launched.

    • Select the default security group and click Edit Inbound Rule.

    • Click Add Rule. Click on the drop down arrow Under Type and click on NFS (Network File System), select the security group created for the first instance as shown below.

    • Repeat the exact process for the second instance but this time select the second security group and then click save rules.

    • After editing the Inbound rules your dashboard should look like this.

  4. MOUNT AND CONNECT THE EFS TO THE EC2

    • On the left panel click on instance and your two instances would be displayed, click on the checkbox of the first instance and connect.

    • For the connection Type select “connect using EC2 Instance Connect” and click on Connect.

    • A terminal/command prompt will you open automatically once the instance is connected.

    • For you to be able to mount the EFS on our EC2 instance, we need to first install the amazon EFS utilities, type in the command sudo dnf install -y amazon-efs-utils and click on the enter key.

    • Once it has been installed the next step is to create a directory so that the file share can be mounted and saved on its file path. Type the command sudo mkdir “name your directory“. I named mine efs.

    • Navigate back to the search bar, and search for EFS

      Click on the File system ID, then click on Attach and copy the EFS mount helper.

    • Return to your terminal/command prompt, paste the EFS mount command and click on enter.

    • Use the command df -T to confirm that your EFS has been mounted and shows you the file path where it is mounted on the Linux server

    • Next we need to change ownership of the directory[efs] from the root to the user so we can get the permission to execute commands as the user. Using the command sudo chown ec2-user (name of your directory)/, then run ls -l to confirm the permission change of the directory.

    • To access/get into the directory you created remember I named mine "efs”, you need to run this command cd efs/ and click enter.

    • After getting into your directory, create a file using the command sudo touch (any name of your choice).txt and click on enter, I’m using the name “cheesy” so my command should look like sudo touch cheesy.txt. I created a file and also added a compressed wordpress file to my directory. (Note: when creating a file adding .txt isn’t compulsory you can choose to add .ext or .html or even go completely without any.)

    • Run the command ls which basically means list to see the list of files on the server. You should see the file you just created after running the command.

    • Next we need to connect the second ec2 instance to check if we can view the same files in the directory that the first ec2 mounted on the EFS.

      -After connecting the second instance, install the amazon efs utilities package.

      -Next create a directory sudo mkdir (give it the same name as the previous one ) to avoid confusion and for easy auto scaling.

      - Mount the EFS on this instance using the same command we got from the efs mount helper

      - Change ownership of the directory from root to the user using sudo chown ec2-user (your directory)/

      - Use df -T to confirm it has been mounted

      - Enter your directory using cd (your directory name)/

      - Type the command ls and you would be able to view and access the exact files created by the first instance.

NB: Remember no file was created on the second instance.

In conclusion, connecting Amazon EFS to your EC2 instances is a game-changer for boosting your cloud setup's scalability and efficiency. By following this guide, I hope I was able to easily put you through the process to achieve your desired result.