Running multiple ECS tasks based on same task definitions in one host, using different ports

11.2k views Asked by At

I have one ecs task definition. Can multiple tasks of the above task definition can run on one EC2 instance on several ports ?

I have already have running several tasks running on several ec2 instances. I want to reduce the consumption of resources. As one EC2 instance has more than enough resources to run several tasks. Is it possible to run same task several times on different ports on one EC2 instance ?

2

There are 2 answers

3
Bram On BEST ANSWER

Yes, ECS has very good support for this since 2016. You can leave the host port empty in the container definition, this will result in a random port to be chosen for your container. As a result, more instances of the same task definition can run on one ECS instance.

You can configure your ECS service in combination with an Application Load Balancer so that when it starts a new task, it will register the port number in the associated target group. This way you never have to deal with the random port.

If you setup your service via the AWS console, configuration is pretty straightforward.

0
Nikhil Prakash On

This can be configured by setting Host Port to be 0 in Port Mappings setting of Container Definitions while defining Task.

Following setting is available in Container Definition of Task.

enter image description here

It allows ECS to assign random ports to Tasks running in same EC2.

For more details please check - Setup Dynamic Port Mapping for ECS