ECS EFS Storage Configuration - Nginx Example

Configure ECS tasks to use Amazon EFS for persistent storage with this Nginx example. Learn how to mount EFS volumes for your containers.

ECS EFS Storage Configuration

Nginx with Amazon EFS Integration

This document provides an example of an AWS Elastic Container Service (ECS) task definition that integrates with Amazon Elastic File System (EFS) to provide persistent storage for a Nginx web server. This configuration is particularly useful for scenarios where you need to serve static content that can be updated or managed independently of the container lifecycle.

Understanding the Configuration

The provided JSON defines an ECS task. Key components include:

  • family: Identifies the task definition family.
  • executionRoleArn and taskRoleArn: Specify IAM roles for ECS agent and task execution.
  • requiresCompatibilities: Indicates the launch type, here set to EC2.
  • containerDefinitions: Defines the containers within the task.

Nginx Container Setup

The nginx-json container uses the ruanbekker/nginx-demo:json image. It maps port 80 for incoming traffic and crucially, mounts an EFS volume to /usr/share/nginx/html. This ensures that any files placed in the EFS volume's root directory will be served by Nginx.

EFS Volume Configuration

The volumes section defines the EFS volume named efs-html. It specifies the fileSystemId of your EFS file system and the rootDirectory within that file system to be mounted. This allows ECS to connect to your EFS and make its contents available to the container.

For more information on configuring ECS with EFS, refer to the AWS ECS Developer Guide on EFS Volumes.

You can also explore mounting EFS file systems for detailed instructions on EFS integration.

Consider reviewing Nginx Wiki for advanced Nginx configurations.

Task Definition JSON

Below is the complete task definition JSON:

{
    "family": "nginx-with-efs",
    "executionRoleArn": "arn:aws:iam::xxxxxxxxxxxx:role/ecs-exec-role",
    "taskRoleArn": "arn:aws:iam::xxxxxxxxxxxx:role/ecs-task-role",
    "requiresCompatibilities":[
        "EC2"
    ],
    "containerDefinitions": [
        {
            "name": "nginx-json",
            "image": "ruanbekker/nginx-demo:json",
            "memory": 128,
            "essential": true,
            "portMappings": [
                {
                    "hostPort": 0,
                    "containerPort": 80,
                    "protocol": "tcp"
                }
            ],
            "mountPoints": [
                {
                    "containerPath": "/usr/share/nginx/html",
                    "sourceVolume": "efs-html"
                }
            ],
            "logConfiguration": {
                "logDriver": "awslogs",
                "options": {
                    "awslogs-group": "/ecs/tools/efs-nginx-json",
                    "awslogs-region": "eu-west-1",
                    "awslogs-stream-prefix": "logs",
                    "awslogs-create-group": "true"
                }
            }
        }
    ],
    "volumes": [
        {
            "name": "efs-html",
            "efsVolumeConfiguration": {
                "fileSystemId": "fs-xxxxxxxx",
                "rootDirectory": "/efs-html"
            }
        }
    ]
}