Question: How can I allocate memory to tasks in Amazon ECS?

I want to use Amazon Elastic Container Service (Amazon ECS) to allocate memory to tasks.

Short descriptionIn Amazon ECS, memory can be defined at both the task level and at each container level. Memory defined at the task level is the hard limit of memory for the task. At the container level, there are two parameters for allocating memory to tasks: memoryReservation (a soft limit) and memory (a hard limit). For tasks that are hosted on Amazon EC2 instances, the task level memory field is optional, and any value can be used. If a task-level memory value is specified, then the container-level memory value is optional. The value for each parameter is subtracted from the available memory units of an Amazon ECS container instance when a task is running. The calculation is based on the soft limit, hard limit, or task-level memory of a task definition. For more information, see Cluster reservation.Note: The memory and memoryReservation parameters are set as the container definition parameters of an Amazon ECS task definition. If you specify a value for both container-level memory and memoryReservation, then memory must be greater than memoryReservation. If you specify memoryReservation, then that value is subtracted from the available memory resources for the container instance where the container is placed. Otherwise, the value of memory is used. For more information, see Memory.

ResolutionBefore you start, check to see that you have an Amazon ECS cluster that includes an Amazon Elastic Compute Cloud (Amazon EC2) instance. For more information on creating a cluster, see Creating a cluster using the classic console. For more information on configuring the cluster and container instance, see Container Instance Memory Management.View the memory allocations of a container instanceOpen the Amazon ECS console.In the navigation pane, choose Clusters.Choose the cluster that you created.Choose the ECS Instances view, and then choose the container instance included with the cluster that you created from the Container Instance column.Note: The Details pane shows that the memory in the Available column is equal to the memory in the Registered column.For statistics on the resource usage of the instance, connect to the instance using SSH, and then run the docker stats command.Create a task definition with a soft limitFrom the Amazon ECS console, in the navigation pane, choose Task Definitions.Choose Create new Task Definition.For the launch type, choose EC2, and then choose Next step.For Task Definition Name, enter a name.In the Container Definitions section, choose Add container.For Container name, enter a name.For Image, enter nginx or the appropriate Docker image for your environment.For Memory Limits (MiB), choose Soft limit, and then enter 700.Choose Add, and then choose Create.Run the task definition with a soft limitFrom the Amazon ECS console, in the navigation pane, choose Clusters, and then choose the cluster that you created.Choose the Tasks view, and then choose Run new Task.Choose Launch Type as EC2, choose the task definition that you created with soft limit, and then choose Run Task. Note: Task Definition and Cluster can be prepopulated with the name of the task definition and cluster that you created earlier if you are using ECS for the first time.When the Last status column of the task with a soft limit shows as RUNNING, move to next step.Note: To update the status of the task to RUNNING, refresh the page.Choose the ECS Instances view, and then choose the instance from the Container Instance column.Note: The Details pane shows that the memory in the Available column is less than the memory in the Registered column.For statistics on the resource usage of the instance, connect to the instance using SSH, and then run the docker stats command.Choose Clusters from the navigation pane, and then choose the cluster.Choose the Tasks view, select the task with a soft limit, and then choose Stop.Choose the ECS Instances view, and then choose the instance from the Container Instance column.Note: The Details pane shows that the memory in the Available column is equal to the memory in the Registered column.Create a new revision of a task definition with a hard limitFrom the Amazon ECS console, in the navigation pane, choose Task Definitions.Select the task definition that you created with a soft limit, and then choose Create new revision.In the Container Definitions section, in the Container Name column, choose the container that you added for the task definition with a soft limit.For Memory Limits (MiB), choose Hard limit, and then enter 1000.Choose Update, and then choose Create.Run the revised task definition with a hard limitFrom the Amazon ECS console, in the navigation pane, choose Clusters, and then choose the cluster that you created.Choose the Tasks view, and then choose Run new Task.Choose Launch Type as EC2For Task Definition, choose the task definition with a hard limit that you created, and then choose Run Task.When the Last status column of the revised task with a hard limit shows as RUNNING, move to next step.Note: To update the status of the task to RUNNING, refresh the page.Choose the ECS Instances view, and then choose the instance from the Container Instance column.Note: The Details pane shows that available memory in the Available column is less than the memory in the Registered column.For statistics on the resource usage of the instance, connect to the instance using SSH, and then run the docker stats command.Choose Clusters from the navigation pane, and then choose the cluster.Choose the Tasks view, select the task with a hard limit, and then choose Stop.Choose the ECS Instances view, and then choose the instance from the Container Instance column.Note: The Details pane shows that the memory in the Available column is equal to the memory in the Registered column.Create a new revision of a task definition with both a soft limit and a hard limitFrom the Amazon ECS console, in the navigation pane, choose Task Definitions.Select the task definition that you created with a hard limit, and then choose Create new revision.In the Container Definitions section, in the Container Name column, choose the container that you added for the task definition with a hard limit.For Memory Limits (MiB), choose Soft limit, and then enter 700.Choose Add Hard limit, and then enter 1200.Choose Update, and then choose Create.Run the revised task definition with both a soft limit and a hard limitFrom the Amazon ECS console, in the navigation pane, choose Clusters, and then choose the cluster that you created.Choose the Tasks view, and then choose Run new Task.Choose Launch Type as EC2For Task Definition, choose the task definition that you created with both a soft limit and a hard limit, and then choose Run Task.When the Last status column of the task shows as RUNNING, move to next step.Note: To update the status of the task to RUNNING, refresh the page.Choose the ECS Instances view, and then choose the instance from the Container Instance column.Note: The Details pane shows that the memory in the Available column is less than the memory in the Registered column.For statistics on the resource usage of the instance, connect to the instance using SSH, and then run the docker stats command.Choose Clusters from the navigation pane, and then choose the cluster.Choose the Tasks view, select the task with both a soft limit and a hard limit, and then choose Stop.Choose the ECS Instances view, and then choose the instance from the Container Instance column.Note: The Details pane shows that the memory in the Available column is equal to the memory in the Registered column.Create a new revision of a task definition with Task Level Memory limitFrom the Amazon ECS console, in the navigation pane, choose Task Definitions.Select the task definition that you created with a hard and soft limit, and then choose Create new revision.Under the Task Size section, for Task memory (MiB) enter 1000In the Container Definitions section, in the Container Name column, choose the container that you added for the task definition with a hard and soft limit.For Memory Limits (MiB), remove Soft limit, by choosing the x icon on the right sideNext for Hard limit, remove the value 1200 by selecting it and deleting it.Choose Update, and then choose Create.Run the revised task definition with Task Level Memory limitFrom the Amazon ECS console, in the navigation pane, choose Clusters, and then choose the cluster that you created.Choose the Tasks view, and then choose Run new Task.Choose Launch Type as EC2For Task Definition, choose the task definition that you created with Task Level memory, and then choose Run Task.When the Last status column of the task shows as RUNNING, move to next step.Note: To update the status of the task to RUNNING, refresh the page.Choose the ECS Instances view, and then choose the instance from the Container Instance column.Note: The Details pane shows that the memory in the Available column is less than the memory in the Registered column.For statistics on the resource usage of the instance, connect to the instance using SSH, and then run the docker stats command.Note: You might observe that the memory limit shown by the docker stats command might not be 1000 MiB for the container. This is because the Task Level memory is managed by ECS Agent and not by docker daemon.Choose Clusters from the navigation pane, and then choose the cluster.Choose the Tasks view, select the task with Task level memory, and then choose Stop.Choose the ECS Instances view, and then choose the instance from the Container Instance column.Note: The Details pane shows that the memory in the Available column is equal to the memory in the Registered column.

Related informationAmazon ECS CloudWatch metricsAmazon EC2 Instance typesLimit a container's access to memoryTask size