Skip to content

service basic-ml-microservice-service port 5000 is unhealthy in target-group ecs-mlewp2-basic-ml-microservice due to (reason Health checks failed with these codes: [404]).

0

I am trying to deploy a simple flask application using ECR, ECS and load balancing. This is my first time and I am following a tutorial. However it is failing with the message of the title. The service event log are at the end of this post

Since this is the first time I am at lost at what could be wrong. I asked chatGPT and it told me that flask applications deployed in AWS should have some "health" checkpoints like

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello, ECS!', 200

@app.route('/health')
def health():
    return 'Healthy', 200

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

because otherwise the health check will fail with 404. Is this true? Are these endpoints necessary?

What other thing could be making this fail?

Events

March 30, 2025 at 19:07 (UTC+9:00) service basic-ml-microservice-service deployment ecs-svc/3068410287412697402 deployment failed: tasks failed to start. 4b482842-9487-49a1-9460-22ee9510d509 March 30, 2025 at 19:07 (UTC+9:00)

(service basic-ml-microservice-service, taskSet ecs-svc/3068410287412697402) has begun draining connections on 1 tasks.

ab1becaf-71a7-4813-bf7d-3fa96095a0a3 March 30, 2025 at 19:07 (UTC+9:00)

service basic-ml-microservice-service deregistered 1 targets in target-group ecs-mlewp2-basic-ml-microservice 0a580912-dc94-446b-b4d0-551a0e494d4d March 30, 2025 at 19:07 (UTC+9:00)

service basic-ml-microservice-service has stopped 1 running tasks: task 1654a3fbad004666a461c62296b2f093.

7a08c482-e06a-4cce-a646-aecc571ee06f March 30, 2025 at 19:07 (UTC+9:00)

service basic-ml-microservice-service port 5000 is unhealthy in target-group ecs-mlewp2-basic-ml-microservice due to (reason Health checks failed with these codes: [404]).

1953cf2f-ac1e-44c5-8bee-ab1a3cbf14fa March 30, 2025 at 19:06 (UTC+9:00)

service basic-ml-microservice-service registered 1 targets in target-group ecs-mlewp2-basic-ml-microservice 968186e4-cde7-492a-9da1-4b4ae70acbcb March 30, 2025 at 19:06 (UTC+9:00)

service basic-ml-microservice-service has started 1 tasks: task 69d0463ba51c4b19b8aaf9f6de0f18a6. Amazon ECS replaced 1 tasks due to an unhealthy status.

asked a year ago96 views
2 Answers
0

The information you received about adding health check endpoints to your Flask application is correct. When deploying a Flask application on AWS using ECS and load balancing, it's important to include health check endpoints. These endpoints are necessary for the load balancer to determine if your application is healthy and ready to receive traffic.

The 404 error you're seeing indicates that the health check endpoint the load balancer is trying to reach doesn't exist in your application. By default, load balancers often check the root path ('/') or a specific health check path (like '/health'). If these endpoints are not defined in your Flask application, the health checks will fail with a 404 error.

Adding the '/health' endpoint as suggested in the code snippet you provided is a good practice. It allows the load balancer to verify that your application is running and responsive. The root path ('/') can also serve as a health check endpoint if you prefer.

In addition to adding these endpoints, make sure that:

  1. Your Flask application is actually running on port 5000 inside the container.
  2. The container is exposing port 5000.
  3. The target group in your load balancer is configured to use the correct port and health check path.
  4. Your task definition in ECS is correctly specifying the container port.

Other potential issues that could cause this failure include:

  1. Incorrect Docker image or container configuration.
  2. Mismatched security group settings preventing traffic from reaching your container.
  3. Networking issues within your VPC or between the load balancer and your ECS tasks.

To troubleshoot further, you can:

  1. Check the application logs in CloudWatch for any error messages.
  2. Verify that your Dockerfile and ECS task definition are correctly configured.
  3. Ensure that your VPC and security group settings allow traffic between the load balancer and your ECS tasks.
  4. Double-check the health check settings in your target group configuration.

Remember, when deploying services in AWS, it's crucial to have proper health checks in place to ensure smooth operation and automatic recovery from failures.
Sources
Health checks for Network Load Balancer target groups - Elastic Load Balancing
Health checks for Gateway Load Balancer target groups - Elastic Load Balancing

answered a year ago
EXPERT
reviewed a year ago
0

Hello.

The health check endpoint will work even if it is not present, as long as there is content that can respond with an HTTP status code of 200 during the ELB health check.
I'm not sure what the health check path of the ALB you have set is, but if you get a 404 when you access the path you set, it means that you are accessing a route that does not exist.
What is your ALB health check path configuration?

EXPERT
answered a year ago
EXPERT
reviewed a year 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.