ECS Fargate with mounted EFS
Tags: devops, aws, awscommunity, docker
Published: 2022-08-31 05:17:19

Amazon Elastic File System (Amazon EFS) provides simple, scalable file storage for use with your Amazon ECS tasks. With Amazon EFS, storage capacity is elastic, growing and shrinking automatically as you add and remove files. Your applications can have the storage they need, when they need it.

You can use Amazon EFS file systems with Amazon ECS to access file system data across your fleet of Amazon ECS tasks. That way, your tasks have access to the same persistent storage, no matter the infrastructure or container instance on which they land. When you reference your Amazon EFS file system and container mount point in your Amazon ECS task definition, Amazon ECS takes care of mounting the file system in your container. The following sections help you get started using Amazon EFS with Amazon ECS.

This feature is supported by tasks that use both the EC2 and Fargate launch types, however this tutorial will use an Amazon ECS task that uses the Fargate launch type. This tutorial is also meant to be followed step by step, however if you already have some of these resources created on your account then you may be able to skip some steps.

> The following resolution applies to the Fargate platform version 1.4.0 or later, which has persistent storage that you can define at the task and container level in Amazon ECS. Fargate platform versions 1.3.0 or earlier don’t support persistent storage using Amazon EFS.
> Amazon EFS may not be available in all Regions. For more information about which Regions support Amazon EFS, see Amazon Elastic File System Endpoints and Quotas in the AWS General Reference.

Before you complete the steps , you must have the following:

- [Amazon ECS cluster](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create_cluster.html)
- [Amazon Virtual Private Cloud (Amazon VPC)](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-getting-started.html)
- [Amazon EFS file system](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/tutorial-efs-volumes.html#efs-create-filesystem)

### Create and configure an Amazon EFS file system

1. [Create an Amazon EFS file system](https://docs.aws.amazon.com/efs/latest/ug/gs-step-two-create-efs-resources.html), and then note the EFS ID and security group ID.

> Note: Your Amazon EFS file system, Amazon ECS cluster, and Fargate tasks must all be in the same VPC.

2. To allow inbound connections on port 2049 (Network File System, or NFS) from the security group associated with your Fargate task or service, [edit the security group rules of your EFS file system](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html#AddRemoveRules).

3. Update the security group of your Amazon ECS service to allow outbound connections on port 2049 to your Amazon EFS file system’s security group.

###Create a task definition

1. Open the [Amazon ECS console](https://console.aws.amazon.com/ecs/).

2. From the navigation pane, choose Task Definitions, and then choose Create new Task Definition.

3. In the Select launch type compatibility section, choose FARGATE, and choose Next Step.

4. In the Configure task and container definitions section, for Task Definition Name, enter a name for your task definition.

5. In the Volumes section, choose Add volume.

6. For Name, enter a name for your volume.

7. For Volume type, enter EFS.

8. For File system ID, enter the ID for your Amazon EFS file system.

> Note: You can specify custom options for Root directory, Encryption in transit, and EFS IAM authorization. Or, you can accept the default, where “/” is the root directory.

9. Choose Add.

![volume section in task definition](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/be8czqsxmcl8uufal8zj.png)

10. In the Containers Definition section, choose Add container.

11. In the STORAGE AND LOGGING section, in the Mount points sub-section, select the volume that you created for Source volume in step 5.

12. For Container path, choose your container path.


![Setting mount point in task definition](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c7blnukzljmvx5jo7ku2.png)


13. (Optional) In the ENVIRONMENT section, for Entry point, enter your entry point.

14. For Command, enter the [df ,-h] command to display the mounted file system.

Note: You can use the entry point and command to test if your Amazon EFS file system is mounted successfully. By default, the container exits after the df -h command executes successfully.

15. Choose Add.

16. Fill out the remaining fields in the task definition wizard, and then choose Create.

### Run a Fargate task and check your task logs
1. Run a Fargate task using the task definition that you created earlier.

Important: Be sure to run your task on the [Fargate platform version 1.4.0](https://aws.amazon.com/blogs/containers/aws-fargate-launches-platform-version-1-4/).

2. To verify that your Amazon EFS file system is successfully mounted to your Fargate container, [check your task logs](https://docs.aws.amazon.com/AmazonECS/latest/userguide/using_awslogs.html#viewing_awslogs).

The output of df-h looks similar to the following:

![output screenshot from cloudwatch ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y9qdm9gzexv1h1aclfl4.png)


###Reference :

- [How do I mount an Amazon EFS file system on an Amazon ECS container or task running on Fargate?](https://aws.amazon.com/premiumsupport/knowledge-center/ecs-fargate-mount-efs-containers-tasks/)

- [Troubleshooting Mount Issues](https://docs.aws.amazon.com/efs/latest/ug/troubleshooting-efs-mounting.html)