Setting Up Dynamic Port Mapping in Amazon ECS: A Step-by-Step Guide

In this article I will walk you through the process of setting up Dynamic Port mapping in Amazon Elastic Container Service (ECS). Dynamic Port mapping is a powerful feature that allows you to efficiently manage multiple tasks on the same container instance, boosting container efficiency and reducing manual configuration overhead. Let’s dive in and explore the steps to achieve this using an Application Load Balancer (ALB) in Amazon ECS.

What is Dynamic Port Mapping?

In traditional setups, running multiple copies of a task on the same instance could lead to conflicts when statically mapping port numbers. However, with Dynamic Port mapping, you can automatically assign spare ports to tasks, maximizing efficiency while maintaining consistency. This is achieved by utilizing an Application Load Balancer or a Network Load Balancer.

Hands-On: Setting Up Dynamic Port Mapping with an Application Load Balancer

  1. Task Definition Configuration:In the ECS console, create or update a task definition. Set the host port to 0, which indicates that the system should automatically assign an available port. Ensure to define the container port mappings specific to your application.Note: Host and AWS VPC Network modes do not support Dynamic Port mapping.
  2. Service Configuration:Configure your service to use the Application Load Balancer you’ve already created. During the creation of the service, on the “Configure Network” page of the Create Service console, add the load balancer. Remember that after creating the service, you cannot change the Target Group’s Amazon Resource Name (ARN), container name, or container port specified in the service definition.
    • Make sure to select the traffic port that will route health check traffic correctly when creating a Target Group.
  3. Monitor Dynamic Port Usage:In the Amazon EC2 console, navigate to “Target Groups” from the navigation pane. Choose the hyperlink name for your Target Group. On the “Targets” tab, you can observe which port is being used for the tasks in the service you’ve created.


By following these steps, you’ve successfully set up Dynamic Port mapping in Amazon ECS using an Application Load Balancer. This powerful feature enhances container efficiency by allowing multiple tasks from a single service to run on the same container instance. Dynamic Port mapping automates the assignment of ports, reducing manual intervention and the risk of port conflicts.

Now that you have this knowledge in your toolkit, you’re equipped to optimize your containerized applications and make the most of Amazon ECS. Thank you for joining me in this tutorial. Happy cloud computing from all of us here at Kodecamps!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top