Skip to content

The ECS Fargate frontend cannot connect to the backend using ALB dns_name

0

Hi, I have a three-tier application. The application client and server components are deployed in ECS Fargate, and the database is deployed in RDS. Frontend and backend require a separate Application Load Balancer. To get the final result, I follow these steps: 1.Create 2 separate security groups (server-lb-sg and client-lb-sg) with the following inbound rule: Type - Custom TCP, Protocol - TCP, Port range - 80 (the port number of the LB listener), Source - Custom 0.0.0.0./0. 2.Create 2 separate security groups for the client and the server with the following inbound rule: Type - All TCP, Source - Custom <lb-sg-id of corresponding app-component>. 3.Target group port - corresponding app backend(5000) and frontend(3000) port numbers, 4.ALB listener port - 80.

After listed and other configurations, I can access the client/server separately using the LB dns_name of the client and server (without port number), as well as using the LB DNS names of the client and server. However, the client cannot connect to the server.

This is connection string from client source-code: http://${process.env.REACT_APP_API_URL}/api/values/all

And when i try sending a request from the client to the server, the following error appears: POST http://custom-lb-0-448125871.eu-north-1.elb.amazonaws.com/api/values Error saving number: Error: Request failed with status code 500

Please tell me if I took the wrong steps. Thank You in advance

1 Answer
1

Based on the information provided, it seems you've taken many of the correct steps, but there are a few areas that may need adjustment:

  1. Security Groups: Your setup for the security groups looks correct. However, ensure that the backend security group also allows inbound traffic from the frontend security group on the specific port the backend is listening on (5000 in this case).

  2. ALB Configuration: Make sure your Application Load Balancers are configured correctly:

    • The frontend ALB should route traffic to your frontend ECS tasks.
    • The backend ALB should route traffic to your backend ECS tasks.
  3. Target Groups: Confirm that your target groups are healthy and the health checks are passing.

  4. Environment Variables: Double-check that the REACT_APP_API_URL environment variable in your frontend task is set correctly to the DNS name of your backend ALB.

  5. Backend Error: The 500 status code suggests an internal server error on your backend. This could be due to:

    • Incorrect routing in your backend application
    • The backend not being able to connect to the database
    • An error in your backend code

To troubleshoot further:

  • Check the logs of your backend ECS tasks for any error messages.
  • Verify that your backend can connect to the database.
  • Try to access the backend API directly using the ALB DNS name to see if you get the same error.
  • Ensure that your backend application is correctly configured to handle requests coming through the ALB (it may need to be aware of any path prefixes added by the ALB).

If you're still having issues, you might want to try:

  • Using AWS ECS Service Connect instead of ALBs for service-to-service communication within your ECS cluster.
  • Implementing proper error handling in your backend to provide more detailed error messages.
  • Using AWS X-Ray or a similar tracing tool to get more insight into where the request is failing.

Remember to check your ECS task definitions, ensure all necessary environment variables are set, and that your containers are exposing the correct ports.
Sources
ECS EC2 and Postgres container connection by Service Connect | AWS re:Post
Connecting ecs fargate backend service to frontend nginx using service connect | AWS re:Post
Connect ECS Fargate Service/Task from public | AWS re:Post
Resolve HTTP connection errors with Application Load Balancer | AWS re:Post

answered 2 years ago

  • Thanks Sorry, I have one more question: how can I check the server -> rds-db connection? I think that's the problem.

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.