How do I change the target port for an ECS service with load balancer?

0

I created an ECS service on Fargate that's associated with an Application Load Balancer and Target Group. I accidentally gave it the wrong target port in setup. How can i modify the port in the AWS console?

I tried updating the task definition to map the original port (80) to the correct port (8000) in the container like this:

"containerPort": 8000,
"hostPort": 80,

But the console gives the error "When networkMode=awsvpc, the host ports and container ports in port mappings must match."

I created a new revision with both hostPort and containerPort updated to 8000, but the console will not allow me to update the service to use the new revision: "The container did not have a container port 80 defined."

1 Answer
0
Accepted Answer

Changing the target port for an ECS service with a load balancer can be a bit tricky, especially when using the AWS console. Unfortunately, you cannot directly modify the port in the AWS console for an existing service. Here's what's happening and how you can address this:

  1. The error you're seeing about host ports and container ports needing to match is correct for Fargate tasks using awsvpc network mode. In this mode, the hostPort and containerPort must be the same.

  2. The second error you're encountering is because the load balancer configuration is tied to the original container port (80) that was specified when the service was created. This configuration can't be changed directly in the console.

To resolve this issue, you'll need to take the following steps:

  1. Create a new task definition revision with the correct port (8000 for both containerPort and hostPort).

  2. Instead of trying to update the existing service, you'll need to create a new service:

  • Use the new task definition revision
  • Set up a new target group with the correct port (8000)
  • Configure the load balancer to use this new target group
  1. Once the new service is up and running, you can gradually shift traffic to it and then delete the old service.

Alternatively, if you want to keep the same service:

  1. You'll need to use the AWS CLI or SDK to update the service. The console doesn't allow changing the load balancer configuration, but it can be done programmatically.

  2. Use the UpdateService API call, specifying the new task definition and updating the loadBalancers section to point to the new port.

  3. You'll also need to update or create a new target group for the load balancer that uses the correct port (8000).

Remember, making these changes will cause Amazon ECS to start a new deployment, which will register and deregister tasks from the load balancer. It's advisable to test this process in a non-production environment first.

Sources
Use a Gateway Load Balancer for Amazon ECS - Amazon Elastic Container Service
Troubleshooting service load balancers in Amazon ECS - Amazon Elastic Container Service
LoadBalancer - Amazon Elastic Container Service

profile picture
answered 3 months ago
profile picture
EXPERT
reviewed 3 months 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