Create and mount an Amazon EFS file system (Amazon EFS) to an Amazon Elastic Compute Cloud (Amazon EC2) instance
Tags: efs, ec2, aws
Published: 2023-02-06 23:47:43

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](https://github.com/awsmine/EC2_projects)** 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](https://docs.aws.amazon.com/efs/latest/ug/whatisefs.html)**

**[Launch Instance Wizard documentation](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/launching-instance.html)**

# 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-sg`**security 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u51sfdi5e5xpa8nyobda.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqoztv2mkyt3r9j29wkt.png)


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/na1d3xrgy1bgsy9442tn.png)


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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xo69xbxfef2ncotm6ts.png)


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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ctohkpou3mdngc6ashvo.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2egnspb4uf5rgay022zs.png)


## 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
```

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ecqp7yb9neow17kyfhs3.png)

5. 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.