DEV Community

Revathi Joshi for AWS Community Builders

Posted on

Create and mount an Amazon EFS file system (Amazon EFS) to an Amazon Elastic Compute Cloud (Amazon EC2) instance

In this article, I am going to show you how to create a new Amazon Elastic File System (Amazon EFS) file system, then mount it to a new Amazon Elastic Compute Cloud (Amazon EC2) instance using the EC2 Launch Instance Wizard, verify that file system is mounted on the EC2 instance, and then delete the resources created.

Please visit my GitHub Repository for EC2/EFS articles on various topics being updated on constant basis.

Let’s get started!

Objectives:

1. Create the security groups - myec2-sg, myefs-sg

2. Create the EFS file system - myefs

3. Launch the EC2 instance myec2 and mount the file system

4. Verify EFS file system is mounted

5. Cleanup

Pre-requisites:

  • AWS user account with admin access, not a root account.
  • AWS CLI.

Resources Used:

Amazon EFS documentation

Launch Instance Wizard documentation

Steps for implementation to this project:

1. Create the security groups - myec2-sg, myefs-sg

  • Create two security groups in the default VPC, in us-east-1 region

myec2-sg

  • This myec2-sgsecurity group will be attached to the EC2 instance and it allows only SSH inbound connection to the EC2 instance and any outbound connectivity.

  • on the left navigation pane, choose Security Groups

  • Create security group

  • myec2-sg, default vpc, choose the Inbound Rules tab, Add Rule, Type SSH, Port 22, CIDR block 0.0.0.0/0

  • choose the Outbound Rules tab and verify that the Type is All traffic.

  • Create security group

Image description

myefs-sg

  • This myefs-sg security group will be attached to the EFS file system and allows only TCP connection on port 2409 from the EC2 instance and any outbound connectivity.

  • on the left navigation pane, choose Security Groups

  • Create security group

  • myefs-sg, default vpc, choose the Inbound Rules tab, Add Rule, Type NFS, Port 2049, CIDR block choose myec2-sg

  • choose the Outbound Rules tab and verify that the Type is All traffic.

  • Create security group

Image description

2. Create the EFS file system - myefs

  • Go to Amazon EFS in the AWS Management Console and choose Create file system.

  • myefs, choose your default VPC.

  • click Customize, you will see all the following default inputs

  • Next,

  • In the Security groups column, see the existing security groups, in different subnets and Availability zones

  • Next, Next,

  • Review and create

  • Create

Image description

  • Click the myefs file system , on network tab, in the Security groups column, you will see these following security groups.

Image description

  • Delete all the existing security groups and add the myefs-sg security group.

Image description

  • Next, Next,

  • Review and create

  • Create

3. Launch the EC2 instance myec2 and mount the file system

  • On the Amazon EC2 console and choose Launch Instance.

  • Amazon Linux 2 AMI, t2.micro, NVirKey, default vpc, default-subnet-1a, Select existing security group, choose the ec2-sg security group

  • Configure storage, File systems, Edit

  • Share file system and select the file system ID of myefs from Step 2.

  • See the mount path - this is the location at which the file system will be mounted on the EC2 instance.

  • You can customize this path, but I am keeping the default value.

  • Launch instance

Note: After you select the file system ID, the User data field is automatically populated with a configuration script that automates all manual tasks, such as installing the file system client, installing the EFS mount helper (for Amazon Linux only), and updating the EC2 instance properties so that the file system mounts automatically upon instance reboot.

Image description

4. Verify EFS file system is mounted

  • On the EC2 console, choose Instances, then select the EC2 instance myec2 you created in Step 3.

  • Choose Connect.

  • In the Connect to your instance dialog box, choose EC2 Instance Connect (browser-based SSH connection) and

  • then choose Connect.

  • In the EC2 instance CLI, type the following command:



df -T -h


Enter fullscreen mode Exit fullscreen mode
  • You can see that the EFS File System is mounted at mnt/efs/fs1 which is the same default mount path as seen in Step 3.

  • This verifies that your file system myefs is successfully mounted on the EC2 instance myec2.

Image description

  1. Cleanup
  • Terminate EC2 instance - myec2

  • Delete EFS file system - myefs

  • Delete Security groups - myec2-sg, myefs-sg

What we have done so far

We have successfully created a new Amazon EFS, then mounted it to an Amazon EC2 instance using the EC2 Launch Instance Wizard, verified that file system is mounted on the EC2 instance, and then deleted the resources created.

Top comments (0)