Creating and Mounting EFS on AWS EC2 with system manager runbook
Tags: aws, storage, cloud, ec2
Published: 2023-07-04 11:17:26

## Introduction

In this blog post, we will walk you through the steps to create and mount an Amazon Elastic File System (EFS) on an AWS EC2 instance using the **AWSSupport-CheckAndMountEFS** system manager runbook. EFS provides scalable and shared file storage that can be accessed from multiple EC2 instances simultaneously.

**Systems Manager Automation runbook**

AWS Systems Manager provides predefined runbooks. These runbooks are maintained by Amazon Web Services, AWS Support, and AWS Config. The runbook reference describes each of the predefined runbooks provided by Systems Manager, AWS Support, and AWS Config.

**AWSSupport-CheckAndMountEFS**

The AWSSupport-CheckAndMountEFS runbook verifies the prerequisites to mount your Amazon Elastic File System (Amazon EFS) file system and mounts the file system on the Amazon Elastic Compute Cloud (Amazon EC2) instance you specify. 
This runbook supports mounting your Amazon EFS file system with the DNS name, or using the mount target’s IP address.


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

## Step 1: Create EFS from AWS Console

1. Log in to the AWS Management Console and navigate to the EFS service.
2. Click on "Create file system" and provide a name for your EFS.
3. Choose the VPC where your EC2 instance resides.
4. Select the "Regional" EFS option for high availability.
5. (Optional) In the advanced options, you can choose the storage class. For example, you can select the "Standard" storage type instead of "Infrequent Access" (IA) if desired.

## Step 2: Configure Network in EFS

1. After the EFS creation, navigate to the EFS dashboard and go to the "Network" tab, then click on "Manage".
Copy the security groups listed under "Mount targets" and navigate to the EC2 service, then go to "Security Groups".
2. Add the EC2 instance's security group as a source under the EFS security group to allow access.

## Step 3: Install EFS Utils

1. Connect to your EC2 instance using SSH or AWS SSM.
2. Run the following command based on your Linux distribution:
For SUSE: zypper install aws-efs-utils
For Amazon Linux 2: sudo yum install -y amazon-efs-utils

## Step 4: Create EFS with AWS SSM Document

1. Navigate to the AWS Systems Manager (SSM) service and go to "Documents".
2. Search for "AWSSupport-CheckAndMountEFS" and select the corresponding document.
3. Click on "Execute automation" in the top right corner.
4. Choose "Simple execution".
5. Provide the necessary input parameters:
Choose your EC2 instance (only one instance allowed at a time).
EfsId: Enter the EFS ID obtained from the EFS console.
MountPoint: Specify the desired mount point (e.g., /usr/test).
Region: Select your desired region.
Action: Choose "CheckandMount".
6. add your tags.
7. Click on "Execute" to run the automation.

## Step 5: Keep Mounted File After Reboot

In your EC2 server, navigate to the /etc/fstab file.
Add the following line, replacing the EFS ID and directory (/usr/test) with your own:

fs-0c1cf955858757bc7.efs.us-east-1.amazonaws.com:/ /usr/test efs defaults,_netdev 0 0

## Step 6: Test EFS Mounting

To unmount the EFS, run the command: umount /usr/test.
Use the command "mount -a" to read the /etc/fstab file and mount all file systems listed in it.
Verify the mounted EFS by running the command: df -hT

## Conclusion:

By following the above steps, you can create and mount an Amazon EFS on your AWS EC2 instance using the AWSSupport-CheckAndMountEFS system manager runbook. This enables you to leverage scalable and shared file storage for your applications. 

**Links:**
https://docs.aws.amazon.com/systems-manager-automation-runbooks/latest/userguide/automation-awssupport-check-and-mount-efs.html
https://docs.aws.amazon.com/efs/latest/ug/automount-with-efs-mount-helper.html