DEV Community

Cover image for ECS Fargate with mounted EFS
anil augustine chalissery
anil augustine chalissery

Posted on • Originally published at anilaugustinechalissery.Medium

ECS Fargate with mounted EFS

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:

Create and configure an Amazon EFS file system

  1. Create an Amazon EFS file system, 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.

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

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

  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.

  1. Choose Add.

volume section in task definition

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

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

  3. For Container path, choose your container path.

Setting mount point in task definition

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

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

  1. Choose Add.

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

  1. To verify that your Amazon EFS file system is successfully mounted to your Fargate container, check your task logs.

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

output screenshot from cloudwatch

Reference :

Top comments (0)