Sachin helps you set up
dynamic port mapping
for Amazon ECS


I want to set up dynamic port mapping for my Amazon EC2 Container Service (ECS). How can I do this?

Dynamic port mapping with an Application Load Balancer makes it easier to run multiple tasks from the same ECS service on an ECS cluster.

The Classic Load Balancer requires that you statically map port numbers on a container instance. You cannot run multiple copies of a task on the same instance, because the ports would conflict. An Application Load Balancer allows dynamic port mapping. You can have multiple tasks from a single service on the same container instance.

Here's how to set up dynamic port mapping:

  1. In the Amazon EC2 console, create an Application Load Balancer and target group. For more information, see Creating an Application Load Balancer.
  2. During the creation of a service, map the load balancer to the service. For more information, see Configuring Your Service to Use a Load Balancer.
  3. For Port, use the default traffic port. Be sure you don't choose Override, because that causes health check traffic to be routed incorrectly.
    Note: The task definition must be set to use host port 0. Because a random port from the instance's ephemeral port range is used, make sure that the security group and NACL settings of the load balancer and the backend instances allow traffic from the load balancer to the instances over the ephemeral port range. For more information about ephemeral port ranges, see PortMapping. For more information about configuring security groups, see Create a Security Group Rule for your Container Instances.
  4. Bring up your tasks from the service and then verify the results from the Amazon EC2 console.

You can add a load balancer only during creation of the service. After you create a service, the target group ARN or load balancer name, container name, and container port specified in the service definition cannot be changed. You cannot add, remove, or change the load balancer configuration of an existing service. If you update the task definition for the service, the container name and container port that was specified when the service was created must remain in the task definition. For more information, see Load Balancing Concepts.

You should now have dynamic port mapping set up on your ECS service.

Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2016-05-26

Updated: 2017-12-19