Sub topic 16
Ephemeral storage and shared storage with EFS
This chapter assumes you can read a Fargate task definition and have a working service with awsvpc networking and security groups. Here you decide where a container writes data that must outlive a file write, a container restart, or a task.
Ephemeral storage
Every Fargate task gets 20 GiB of ephemeral storage by default. All containers in the task share it. AWS deletes it when the task stops, so use it for scratch files, caches, and temporary downloads. Do not use it for data you need to keep.
You can raise the size to a maximum of 200 GiB with ephemeralStorage at the task level. This needs platform version 1.4.0 or later, which is what LATEST resolves to today. Extra storage is billed per GiB-hour beyond the 20 GiB included.
Sharing files between containers in one task
A volume with no host path is a bind mount backed by the task's ephemeral storage. Mount it in several containers to share files, for example between an app and a sidecar that ships logs.
Persistent shared storage with EFS
Amazon EFS is a managed NFS file system. Fargate tasks can mount it, and many tasks can mount it at once, across Availability Zones. Data survives task replacement.
{
"ephemeralStorage": { "sizeInGiB": 50 },
"volumes": [
{ "name": "scratch" },
{
"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,
"mountPoints": [
{ "sourceVolume": "scratch", "containerPath": "/tmp/work" },
{ "sourceVolume": "uploads", "containerPath": "/data/uploads" }
]
}
]
}Add these keys to the task definition from earlier chapters. The scratch volume is ephemeral. The uploads volume is EFS.
What EFS needs to work
- A mount target in every subnet (AZ) where tasks run.
- A security group on the mount targets that allows inbound TCP 2049 from the task's security group.
- With
iamset toENABLED, the task role must allowelasticfilesystem:ClientMount, andelasticfilesystem:ClientWritefor writes. The task role is used here, not the execution role. - An access point is optional. It fixes the POSIX user and root directory, which avoids permission errors when the container runs as a non-root user.
Choosing between them
| Need | Use |
|---|---|
| Temporary files for one task | Ephemeral storage |
| Files shared by containers in one task | Bind mount volume |
| Data kept after the task stops, or shared across tasks | EFS |
EFS has higher latency than local disk. It is a poor fit for databases that need fast random I/O.
Exercise
Tasks in a service fail to start after you add an EFS volume with IAM authorization. The file system and mount targets exist. Name two things to check.
Show answer
Check that the mount target security group allows TCP 2049 from the task security group. Check that the task role, not the execution role, allows elasticfilesystem:ClientMount (and ClientWrite for writing). Also confirm a mount target exists in each subnet the tasks use.