Lesson 6 of 6 Amazon ECS, EKS and Fargate

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.

NeedUse
Scratch space within a taskEphemeral task storage (20 GB by default on Fargate, up to 200 GB)
Persistent storage shared across tasks, including FargateAmazon EFS
Per-task block storageEBS volumes attached to ECS tasks
ObjectsAmazon S3 through the SDK
Kubernetes persistent volumesEBS and EFS CSI drivers
Match the pairs

Container storage

Exam skills 2.1.4 · 3.1.1

Match each storage need with the right option for containers.

Log in to try this activity Interactive activities are free with an account.

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.

Worked example

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.

OptionFit
Task ephemeral storageLost when a task stops; not shared
EBS volumeAttached to one task at a time
Amazon EFS mounted in the task definitionShared across tasks and AZs; persistent
Amazon S3 through the SDKAlso 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.

Check yourself

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.