Sub topic 17
Ephemeral storage and shared storage with EFS
This chapter assumes you can read a Fargate task definition, know how the task role differs from the execution role, and have a service running with awsvpc networking and security groups. It covers where a Fargate task can write data, how long that data lives, and how to mount shared storage with Amazon EFS.
Two kinds of storage
A Fargate task has two storage options.
- Ephemeral storage is disk attached to the task. Every task gets 20 GiB by default. The data is gone when the task stops, whether it exited, crashed, was replaced in a deployment, or was scaled in.
- Amazon EFS is a managed NFS file system. Its data outlives tasks and can be mounted by many tasks at once, across Availability Zones.
Fargate does not support EBS-style host paths, and you cannot mount the host's disk. You also have no host to bind to. All volumes are declared in the task definition.
| Need | Use |
|---|---|
| Scratch space, caches, temp files | Ephemeral storage |
| Sharing files between containers in one task | A task-scoped volume (ephemeral) |
| Data that must survive task replacement | EFS |
| Files shared by several tasks or services | EFS |
| A relational database | A managed database, not a container volume |
Ephemeral storage
Ephemeral storage requires platform version 1.4.0 or later for Linux tasks, which is what LATEST resolves to today. The 20 GiB default covers the container image layers and anything the containers write. The image is pulled onto this disk, so a large image uses part of the allowance before your code writes a byte.
To raise the size, set ephemeralStorage at the task level. The allowed range is 21 to 200 GiB.
{
"family": "web-app",
"requiresCompatibilities": ["FARGATE"],
"networkMode": "awsvpc",
"cpu": "256",
"memory": "512",
"ephemeralStorage": { "sizeInGiB": 50 }
}You pay for storage above the 20 GiB included, so size it to what you need. If a task runs out of disk, the process usually fails with "no space left on device". Check image size and log or cache growth before you raise the limit.
Sharing files between containers in a task
Containers in the same task can share a directory by declaring a volume with no host or EFS settings. Fargate backs it with the task's ephemeral storage. A common use is an init container that prepares files for the main container.
{
"volumes": [{ "name": "shared-data" }],
"containerDefinitions": [
{
"name": "init",
"image": "public.ecr.aws/docker/library/busybox:stable",
"essential": false,
"command": ["sh", "-c", "echo ready > /data/config.txt"],
"mountPoints": [
{ "sourceVolume": "shared-data", "containerPath": "/data" }
]
},
{
"name": "web",
"image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/web-app:1.0.0",
"essential": true,
"dependsOn": [{ "containerName": "init", "condition": "SUCCESS" }],
"readonlyRootFilesystem": true,
"mountPoints": [
{ "sourceVolume": "shared-data", "containerPath": "/data", "readOnly": true }
]
}
]
}The dependsOn condition SUCCESS makes web wait until init exits with code 0. Setting readonlyRootFilesystem to true limits where the application can write, so any path that must be writable needs its own volume mount.
Shared storage with EFS
Use EFS when data must survive a task or be seen by several tasks. The setup has four parts.
- A file system with a mount target in each subnet (Availability Zone) where tasks run. A task in a zone with no mount target cannot mount the file system.
- A security group on the mount targets that allows inbound TCP 2049 from the tasks' security group.
- A volume and mount point in the task definition.
- Permissions for the task role, if you turn on IAM authorization.
Access points
An access point is an application-specific entry into the file system. It fixes a root directory and a POSIX user and group for every request that uses it. This prevents a container running as root from reaching the whole file system, and it creates the directory with the right owner on first use.
aws efs create-access-point \
--file-system-id fs-0123456789abcdef0 \
--posix-user Uid=1000,Gid=1000 \
--root-directory "Path=/web,CreationInfo={OwnerUid=1000,OwnerGid=1000,Permissions=750}"Task definition with EFS
{
"family": "web-app",
"requiresCompatibilities": ["FARGATE"],
"networkMode": "awsvpc",
"cpu": "256",
"memory": "512",
"taskRoleArn": "arn:aws:iam::123456789012:role/webAppTaskRole",
"executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
"volumes": [
{
"name": "uploads",
"efsVolumeConfiguration": {
"fileSystemId": "fs-0123456789abcdef0",
"transitEncryption": "ENABLED",
"authorizationConfig": {
"accessPointId": "fsap-0123456789abcdef0",
"iam": "ENABLED"
}
}
}
],
"containerDefinitions": [
{
"name": "web",
"image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/web-app:1.0.0",
"essential": true,
"portMappings": [{ "containerPort": 8080, "protocol": "tcp" }],
"mountPoints": [
{ "sourceVolume": "uploads", "containerPath": "/var/uploads" }
]
}
]
}With iam set to ENABLED, the task role (not the execution role) signs the mount request. Its policy needs these actions on the file system, and the file system policy must allow them as well.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"elasticfilesystem:ClientMount",
"elasticfilesystem:ClientWrite"
],
"Resource": "arn:aws:elasticfilesystem:us-east-1:123456789012:file-system/fs-0123456789abcdef0"
}
]
}Leave out ClientWrite for read-only consumers, and also set "readOnly": true on the mount point. When an access point is used, the file system's root directory is the access point's path, so /var/uploads in the container maps to /web in EFS.
Edge cases and failure modes
- Task fails to start with a mount error. The usual causes are a missing mount target in the task's subnet, a security group that blocks port 2049, or a task role without
ClientMount. The stopped-task reason in the console ordescribe-tasksshows the error. - Permission denied inside the container. The access point's POSIX user does not match the file or directory owner. Create directories through the access point so ownership is correct.
- Slow small-file workloads. EFS has network latency on every operation. Do not run a build cache or a database on it. Copy hot data to ephemeral storage when you can.
- Concurrent writers. Several tasks writing the same file need application-level locking. NFS gives you close-to-open consistency, not transactions.
- Windows tasks. EFS volumes are supported for Linux tasks on Fargate only.
- Cost. EFS bills per GiB stored, plus throughput in some modes. Old files accumulate quietly, so use lifecycle management to move cold data to cheaper storage classes.
Exercise
A service runs four Fargate tasks that each save user uploads to /var/uploads. After a deployment, users report that uploaded files are missing, and files uploaded to one task do not appear when requests reach another. What is the cause, and which changes fix it?
Show answer
The uploads are written to each task's ephemeral storage, which is private to the task and deleted when the task is replaced. Create an EFS file system with mount targets in the tasks' subnets, allow TCP 2049 from the tasks' security group, and add an EFS volume (with an access point and transit encryption) and a mount point at /var/uploads in the task definition. Give the task role elasticfilesystem:ClientMount and ClientWrite if IAM authorization is enabled. Register the new task definition revision and update the service. All tasks then share the same persistent files. Storing uploads in S3 is an alternative.