Ho to allocate CPU and Mem for ECS cluster ec2 launch type


I'm unable to decide where I should specify the cpu and memory while creating the task definition. Should it be done at the infrastructure requirements tab OR should it be specified at containers tab?

Scenario - 2 t3.micro machines registered to the ecs cluster. I want to run 2 java microservices containers What should I specify under infrastructure requirements - Should i give the combined value of 2 micro machines with CPU- 2vcpu and mem as 2 gb? Or Should it be done at container tab by setting the cpu and memory limits - For each container specifying cpu as 1vcpu and mem as 1gb

I had created a task definition where I had specified the cpu as 1vcpu and 0.8gb as memory under infra requirements. This was able to create and run the containers, but it had launched both the containers in the same machine, which was too much for a micro machine and the machine crashed.I even tried adding a placement strategy where I specified for it to spread across instance id's, but it still launched the containers in a single machine. Please provide guidance on this.

1 Answer
Accepted Answer


Specifying CPU and memory at the "Task size" section for Ec2 launch type is optional. If you do not specify here in the task size then the containers' individual requests and limits will be used. In your case, with 2 t3.micro machines, do not combine them in the "Task size" section. Set appropriate CPU and memory requests and limits for each container in the "Containers" tab. Start with conservative values like cpu = 0.5 and memory = 0.5 GiB per container. Monitor resource usage through CloudWatch and adjust these values based on actual needs. You were on the right track with the placement strategy to spread containers across instances. Ensure your strategy uses the "spread" constraint with the specific instance IDs of your t3.micro machines. Alternatively, use the "memberOf" constraint with a Launch Template that specifies multiple machines for placement.

Hope this helps.

answered 22 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions