How to Attach Elastic File System (EFS) to EC2 Instance

You are welcome to my blog, and I am grateful for you taking out time to go through my work. I hope this post will be helpful to you as you progress in the world of cloud computing engineering.

Abstract.

The ability to attach an Amazon Elastic File System (EFS) to an Amazon EC2 instance enables shared, scalable file storage for applications running on EC2. The process involves creating an EFS file system, configuring network access, and mounting the EFS volume on the EC2 instance.

Table of Contents.

  1. Introduction.

  2. Prerequisites.

  3. Creating the Amazon EFS.

  4. Creating EC2 Instances.

  5. Configuration of the rules in the Security Groups.

  6. Mounting the EFS and connecting the EC2 Instances to the EFS.

  7. Conclusion.

  1. INTRODUCTION.

    Let us talk a little about Amazon Elastic File System (EFS). This is a scalable, fully managed cloud-based file storage service provided by Amazon Web Services (AWS). It allows multiple EC2 instances to access a shared file system, which is ideal for use cases like web serving, content management, and data sharing across different applications. EFS provides the flexibility and scalability required to handle dynamic workloads, and it offers high availability and durability by storing data redundantly across multiple Availability Zones (AZs) as captured below.

    Benefits of attaching EFS to the EC2 instance on AWS.

    1. Scalability and Shared Access: EFS can scale to 1,024 terabytes of data and can be accessed by multiple EC2 instances simultaneously, making it ideal for workloads requiring shared file access, such as web hosting, content management systems, and big data analytics.

    2. High Availability and Durability: EFS is designed for high availability, meaning your data is accessible at all times. It also provides strong data consistency, ensuring that changes made to files are immediately visible to all instances accessing the file system.

    3. Simplified Management: EFS is a fully managed service, meaning AWS handles the underlying infrastructure, patching, and maintenance, freeing up your team to focus on application development.

    4. Cost Optimization: EFS offers different storage classes, including EFS Standard and EFS Infrequent Access, allowing you to optimize costs based on your access patterns.

    5. Security: EFS supports encryption of data at rest and in transit, enhancing the security of your data.

    6. Performance: EFS offers two performance modes: General Purpose and Max I/O, allowing you to choose the best option for your workload.

    7. Integration with Other AWS Services: EFS seamlessly integrates with other AWS services, such as Amazon ECS for containerized applications and AWS DataSync for data migration.

  1. Prerequisites.

    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 in to the console. You should also have the following. a Virtual Private Cloud (VPC) where your EC2 instance and EFS file system will reside, and an EFS file system with mount targets. Additionally, the EC2 instance and the mount target must have appropriate security group configurations and be in the same region and VPC for each Availability Zone where your EC2 instances will be located.

  2. Creating the Amazon EFS.

    After logging in to your AWS account, in the search bar, type ‘EFS’ as shown below and click ‘create file system.’

    Thereafter, we will name our file system EFS Demo (VPC is created automatically with the EFS) and select the file system type as regional to offer the highest levels of availability zones, having the automatic backups enabled, as stated below.

    We will establish our lifecycle management to transition into infrequent access after 30 days, followed by a move to archive status at 90 days. This approach ensures that our data is efficiently managed while maintaining accessibility when needed. Having the encryption enabled, elastic recommended, and clicking on next as captured below.

    Now, we can see Mount targets with 5 Availability zones as we scroll down, leave it at the default, and click next.

    We can observe that file system policy is optional, but we must check 'Enforce in-transit encryption' for all clients so as to generate a specific JavaScript, as shown, and then click on 'Next'’

    Now, we can see a quick review before creating the EFS file, taking notes of the editable fields after EFS is created and its mount targets on all five availability zones, as we click on the Create button, as captured below.

    Now our Elastic File System (EFS) has been created, and as we click on the EFS Demo(file name), it will show its creation as shown below.

    Please note that this EFS is not useful unless it is mounted or connected to a server or EC2 instance.

  3. Creating EC2 Instances.

    The benefit of multiple Elastic Compute Cloud (EC2) instances is that they can concurrently access the same EFS file system, enabling collaborative environments where applications or users across different instances need to read from and write to the same dataset, eg, content management systems, development environments, or shared application data.

    Now, type EC2 in the search bar and click on it as shown below.

    Now we are in the EC2 creation stage, as we will click on the launch instance bar, as shown below.

    We will choose a unique name and tags and select Amazon Linux as the OS image, as stated below.

    Please note, Amazon machine image (AMI) and Instance Type (ts.micro) are selected already on the free tier based on the project.

    Next step, keypair production is not mandatory, so we can proceed without a keypair (default value) by dropping the taskbar.

    No, in the network settings, click on 'Edit' and scroll to the security group name and description. Rename both for easy identification as we proceed with the project.

    Thereafter, scroll down and click “Launch Instance” as shown below.

    Now that the first ES2 instance has been launched, as shown below, we will repeat the same process to create an EC2 instance and launch the second EC2 instance.

    The second EC2 instance process has been conducted, and both instances are now running as shown below.

  4. Configuration of the rules in the Security Groups.

    After launching the instances, we need to edit the security group rules to enable the EC2 instances to communicate with the EFS by allowing traffic from port 2049. Now, click on the first EC2 instance ID towards the left-hand side, locate the Network & Security, and click on Security Group.

    Please note that after clicking the Security Group, all the security groups created, such as the Default for the EFS automatically and the other EFS instances, are configured, showing EFSServerDemo and EFServerDemo2 when launched as 1st and 2nd instances.

    Now select the default security group, and click on Edit inbound Rule.

    Click "Add Rule." Under "Type," click the drop-down menu and select "NFS" two times to apply it to both instances. Next, choose the security group created for each instance (1 and 2). In the drop-down menu under "Source," select the custom option and choose the respective security group for each instance, labeled as EC2 EFS. This will allow our server to access the file share appropriately. Finally, click "Save Rules," as illustrated in the diagram.

    Now, from the EC2 created, the inbound rules on the default security group should be as shown below after editing, making sure our server can access the EFS.

  5. Mounting the EFS and connecting the EC2 Instances to the EFS.

    We will refer to our EC2 instance, select the 1st instance, and click on Connect as shown below.

  6. Once connected, the terminal will pop up. Next, install the Amazon EFS utilities using the command sudo dnf install -y amazon-efs-utils. Once installation is complete, it will show as below.

    Now, let’s create a directory by using the command sudo mkdir (directory name). which I named my directory efs.

    Next is to attach the EFS Demo file from the EFS file system console by clicking on the attach button.

    From the attached environment, copy the mount command using the EFS mount helper.

    Navigate back to your command terminal and paste the EFS mount helper command that was earlier copied and run. To confirm that we have successfully mounted the EFS, run the df -T command as shown below.

    Further checks can be performed by running the ls -l command to view the file permissions. From the capture, we can see that the ownership of the efs directory is set to root, which restricts permission to execute any commands as the user. To gain the necessary permissions, we can utilize the sudo chown command. By executing sudo chown ec2-user efs/, we will notice that the ownership change is applied immediately after running the command.

    Now let’s, cd into the directory efs and create or put any file inside the directory. To create a file, use the touch command with a file name ‘eddy’ as created, and include the WordPress document in the directory. Then, run the ls command to confirm the files created are in the directory, as shown below.

    Now let’s log in through Connect to the 2nd EC2 instance and see if we can view the same files in the directory that has been mounted on the EFS by the first EC2 instance by repeating the same command we ran on our 1st EC2 instance as follows: sudo dnf install -y amazon-efs-utils, make a directory (give it the same name as the one in the previous EC2 instance) mkdir efs, sudo mount (using the EFS mount finder as for the 1st EC2 instance), df -T to confirm if it was mounted successfully, as captured below, we will notice the EFS has been mounted.

    On the 2nd EC2 instance, we can now cd (directory name) (cd efs), When you are in the directory, type the ls command to give you a list of the files in the directory. Note, you don’t need to create any file in this instance again. Since you have successfully mounted the EFS on the 2nd EC2 instance, you would be able to see whatever files have been saved in the EFS and notice how the 2nd EC2 instance can now see the same files that the 1st EC2 instance created and saved on the EFS, including as we vim into the file for the readup below.

  7. Conclusion.

    The project simplifies how EFS is a robust and flexible solution for shared file storage needs in an EC2 environment, enabling team projects to work simultaneously with accurate handshakes. It simplifies the management of file systems, provides automatic scalability, ensures high availability and durability, and offers cost-effective storage options, making it ideal for a wide range of applications requiring shared access to file data across multiple EC2 instances..

0
Subscribe to my newsletter

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

Written by

Gabriel Aboiraor
Gabriel Aboiraor