A Simple Step-by-Step Guide for Deploying EC2 Instances with Shared EFS Storage

shafiu usmanshafiu usman
7 min read

Deploying EC2 Instances with Shared EFS Storage

Today, I would be explaining how you can attach an EFS (Elastic File System) to your EC2 instance but before I delve into giving you the steps, let me briefly give an explanation of what EFS is.

The above picture is an architectural design to explain the process for your understanding.

Amazon Elastic File System (Amazon EFS) is a file storage service that can be used with Amazon compute (Amazon EC2, Amazon ECS, Amazon EKS, AWS Lambda, and AWS Fargate, containers, serverless) and on-premises servers. It provides a file system interface, file system access and concurrently accessible storage for up to thousands of EC2 instances.

It is built to scale on demand without disrupting applications, growing and shrinking automatically as you add and remove files which makes it highly scalable, highly available, and highly durable. You can share data without provisioning or managing storage capacity and performance and you can also create and configure file systems quickly and easily.

Amazon EFS offers the different file system types, performance and throughput modes to meet your availability and durability needs. Amazon EFS supports the Network File System version 4 (NFSv4.1 and NFSv4.0) protocol.

Amazon EFS supports authentication, authorization, and encryption capabilities to help you meet your security and compliance requirements. Amazon EFS supports two forms of encryption for file systems: encryption in transit and encryption at rest. You can enable encryption at rest when creating an Amazon EFS file system. If you do, all of your data and metadata is encrypted. You can enable encryption in transit when you mount the file system.

Now that I have given a brief summary of what an Amazon EFS is and its attributes, let’s get started.

You need an AWS account to get started. This link would help you create an AWS account. https://signin.aws.amazon.com/signup?request_type=register. After creating an account, that account becomes the Root user. You can go ahead and create a user under the IAM.

If you already have an existing account, go to https://aws.amazon.com, and sign into the console.

Example: Let’s say your Company, Cloud Native Solutions, is expanding its cloud infrastructure to support a distributed application. The application runs on two Amazon EC2 instances that need to share files in real-time. To achieve this, you must configure Amazon Elastic File System (EFS) to allow both instances to read and write to the same file system.

This project will test your ability to configure AWS storage solutions, manage security groups, and ensure high availability in a cloud environment

This guide will walk you through setting up two Amazon EC2 instances that share a common Amazon Elastic File System (EFS) for real-time file sharing.

Prerequisites

  • We would need to create the following resources on AWS to help us achieve what we want:

  • An AWS account

  • Amazon EFS

  • Multiple instances (I would be creating 2 instances)

  • Security Group

  • AWS account with necessary permissions

  • Basic knowledge of AWS EC2, EFS, and Security Groups

  • AWS CLI or AWS Management Console access

Reminder….READ the steps first then Images below….

Step 1: Create an Amazon Elastic File System (EFS)

  1. Navigate to the EFS Console

    • Open the AWS Management Console.

    • Go to Amazon Elastic File System (EFS).

    • Click Create file system.

    • Give your file a name

    • Click on customize

  1. Under customize, there are 4 steps:
  • Step 1 File system settings: Give your file a name move on with the default settings and click Next

  • Step 2 Network Access: Move on with the default mode and click Next

  • Step 3 File system Policy: Select Enforce in-transit encryption for all clients

  • Step 4 Click Next and then Create.

Step 2: Deploy Two EC2 Instances Using the following steps.

  1. Launch EC2 Instances

    • Navigate to the EC2 Dashboard or search for Ec2 at the search bar and click on it.

    • Click Launch Instance.

    • Give your instance(server) a name of your choice (e.g Esf-server1 for the 1st Ec2 instance & Efs-server2 for the 2nd Ec2 instance.

    • Choose an Amazon Linux image

    • Select an appropriate instance type (t3.micro).

    • On the key pair drop-down, Select proceed without a key pair.

    • Change the security group name to your desired recognized name(e.g., Efs 1 for the first Ec2 instance and Efs 2 for the second Ec2 instance).

    • Launch Instance.

    • Remember to follow the same process and create the Second Ec2 instance.

Step 3: Go to security groups below Network & security and Edit the Security Inbound Rules for both the Two EC2 Instances Using the following steps.

Click on the eclipses on the top left of your screen.

Navigate to the security group and click on it.

Click the Default security group ID.

Navigate to Edit inbound rules & click on it.

Click on Add rule.

Click the drop-down for custom Tcp search for NSF and click on it.

Click the search bar right to the custom box and scroll down to find the security group you created for your Ec2 instance.(e.g search the security group for the 1st Ec2 instance while adding a rule for the 1st instance and same for the 2nd Ec2 instance)

Click on Save rule

Note that this step applies to the Second EC2 instance as well.

After successfully adding the inbound security group rule to your EC2 instances

The next step is to connect your instances and mount them to the EFS …. Follow the steps for both Ec2 instances to connect and mount your instances using the cloud shell terminal.

Navigate and click on instances on the left side of your screen.

Check the box of the instance you wish to connect.

Click on Connect at the top of your screen, it will take you to the next page …leave everything in default mode and click on Connect.

You will be redirected to the cloud shell terminal where you will run some commands to connect and mount your EFS….but before that.

Minimize and navigate back to your console search for EFS and click on it

Click on your file name and open

Click on Attach

Copy the first sudo mount link below Using the Efs mount helper and save it somewhere.

Navigate back to your Cloud shell Terminal run the following commands shown in the image or Steps below and mount your Ec2 instances to Efs….

In order to be able to mount an Amazon EFS file system on an EC2 instance, you would first install the amazon-efs-utils package using this command typed into the terminal and press the enter key on the keyboard:

i) sudo dnf install -y amazon-efs-utils. Once installed, we can use the utilities to mount and manage Amazon EFS file systems on our Linux virtual machine.

ii) Then we need to make a directory called efs using the command in our Linux machine: sudo mkdir efs and click ENTER.

iii) Run the command, sudo mount -t efs -o tls fs-0652bf7312bcdd893:/ efs. The command is used to mount an Amazon Elastic File System (EFS) to a directory on our Linux system. Remember, This command is the EFS command which you copy when you click on Attach

iv) change ownership sudo chown ec2-user efs/

v)Run the command, df -T, to display information about the file, including its type.

vi) Run the command, cd efs/. The command cd efs/ is used in Linux to change the current working directory to a directory named efs.

vii) xi) We have created a directory. We now need to add a file to that directory using the command, sudo touch efs.txt.

viii) Run the command ls. The ls command in Linux and Unix systems is used to list files and directories in the current working directory. efs.txt is supposed to show.

ix) Run the command sudo chown ec2-user efs/. This command changes the ownership of the efs/ directory to the ec2-user, allowing that user to access and manage the directory.

x) Run the command ls -l to see if ownership has really changed.

xi) Run ls to display all files in a directory.

xii)Run wget https://wordpress.org/lastest.tar.gz. wget is used to get a file from the internet.

xiii) Run ls -l

NOTE that you must follow the same steps to mount the Second EC2 instance.

Join me on this cloud adventure and elevate your tech skills! Sign in to the Azure portal, follow my easy instructions, and unleash the power of Being in the cloud.

Subscribe to my blog for more tech tips and tricks that will keep you ahead in the digital game. Your journey to mastering Cloud computing starts here!

🌟 Thank you for being a part of this incredible journey! Together, let's unlock new opportunities and make the most out of our digital experiences. Happy computing! 🌟

1
Subscribe to my newsletter

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

Written by

shafiu usman
shafiu usman