Choosing Storage for Containers
This lesson explains how data that must survive a task, or be shared between tasks, needs external storage, since container storage is ephemeral by default.
Learning outcomes
After completing this lesson, you will be able to:
- Choose a storage option for containers.
- Explain why containers should be stateless.
Storage options
Containers are short-lived, so storage is chosen by whether data must outlive the task and be shared. Ephemeral task storage is scratch space within a task, 20 GB by default on Fargate and expandable to 200 GB, which is lost when the task stops. Amazon EFS provides persistent storage that many tasks, including Fargate tasks, can mount at the same time, for files that must be shared or must survive task replacement.
EBS volumes can be attached to individual ECS tasks for block storage that one task uses at a time. Amazon S3, accessed through the SDK, stores objects, such as uploads and reports, independently of any task. On EKS, the EBS and EFS CSI drivers provide Kubernetes persistent volumes. The following table summarises the options.
| Need | Use |
|---|---|
| Scratch space within a task | Ephemeral task storage (20 GB by default on Fargate, up to 200 GB) |
| Persistent storage shared across tasks, including Fargate | Amazon EFS |
| Per-task block storage | EBS volumes attached to ECS tasks |
| Objects | Amazon S3 through the SDK |
| Kubernetes persistent volumes | EBS and EFS CSI drivers |
Container storage
Design guidance
Containers are replaced frequently, so their storage should be chosen deliberately. As a rule, containers should be stateless, keeping state in databases, caches or Amazon S3, so that any task can be replaced without losing data.
When tasks need a shared file system, Amazon EFS suits content such as user uploads or machine learning models that many tasks across Availability Zones read and write. Amazon EBS volumes can also be attached to tasks, but each volume is tied to one Availability Zone, so a task using one must run in that zone, which limits scheduling and availability.
Sharing files between container tasks
An ECS service on Fargate runs 10 tasks that must all read and write the same uploaded files, and the files must survive tasks being replaced.
| Option | Fit |
|---|---|
| Task ephemeral storage | Lost when a task stops; not shared |
| EBS volume | Attached to one task at a time |
| Amazon EFS mounted in the task definition | Shared across tasks and AZs; persistent |
| Amazon S3 through the SDK | Also works; suits files served to users; needs code |
EFS is the shared file system answer for containers. The service also scales on ALB request count per target, so each task handles a similar share of traffic.
What happens to a Fargate task's ephemeral storage when the task stops?
Show answer
It is deleted. Data that must outlive the task belongs in Amazon EFS, S3 or a database.
Exam Tips
Remember for the exam
- For persistent shared storage for Fargate tasks, use EFS.
- Container storage is ephemeral; persistent data belongs in EFS, EBS, S3 or a database.
- For block storage for a single ECS task, attach an EBS volume to the task.