How do I mount an EFS file system on an ECS container or task running on EC2?

3 minute read

I want use Amazon Elastic File System (Amazon EFS) with Amazon Elastic Container Service (Amazon ECS) container or tasks using an Amazon Elastic Compute Cloud (Amazon EC2) launch type. How can I do this?

Short description

You can mount an EFS file system on a task or container running on an EC2 instance. To do this, create a task definition that provides the file system ID in the volume task definition parameters. This allows the EFS file system to automatically mount to the tasks that you specify in your task definition.

Required resources:


Network requirements

  • The EFS file system and ECS cluster must be in the same VPC.
  • The security groups associated with the EFS file system must allow inbound connections on port 2049 (network file system, or NFS) from the ECS container instance and the ECS task.
  • Security groups of the ECS instance or tasks must allow outbound connections on port 2049 to the EFS file system's security group.

Create a task definition

1.    Open the Amazon ECS console and select Task Definitions, Create new Task Definition.

2.    Choose EC2 for the launch type compatibility, then select Next step.

3.    In Configure task and container definitions, enter a name for your task definition.

4.    In the Volume section, choose Add volume.

5.    Enter the name of the volume, and then select EFS from the Volume types drop down menu.

6.    For the File system ID, select the ID of the file system to use with the ECS tasks.

7.    (Optional) Specify the Root directory, Encryption in transit, and EFS IAM authorization if needed based on your requirements. If no options are modified, then the default root directory "/" is used.

8.    Select Add.

9.    While creating the container, under Container definitions, select Add container to use the previously created volume. Then, under Storage and Logging in the Mount points sub-section, select the volume that you created in step 4.

10.    For container path, choose the directory path within the container for your application, and then choose Add.

11.    Complete the remaining required fields in the task definition wizard and then choose Create.

In the following example, the task definition creates a data volume named efs-ec2-test. The nginx container mounts the host data volume at the /usr/share/nginx/html path.

  "containerDefinitions": [
      "memory": 128,
      "portMappings": [
          "hostPort": 80,
          "containerPort": 80,
          "protocol": "tcp"
      "essential": true,
      "mountPoints": [
          "containerPath": "/usr/share/nginx/html",
          "sourceVolume": "efs-ec2-test"
      "name": "nginx",
      "image": "nginx"
  "volumes": [
      "name": "efs-ec2-test",
      "efsVolumeConfiguration": {
        "fileSystemId": "fs-1324abcd",
        "transitEncryption": "ENABLED"
  "family": "efs-test"

Note: Replace the fileSystemid, containerPath, and other task definition parameters based on the values for your custom configuration.

In the preceding example, you can create a sample index.html file in the file system's root directory with the following content:

    <h1>You are using an Amazon EFS file system for persistent container storage.</h1>

Run an ECS task

1.    Run the ECS task using the task definition created earlier.

2.    Make sure that the EFS file system mounts successfully to the EC2 container by accessing the website using the ECS instance's public IP address.

AWS OFFICIALUpdated 2 years ago