Search code examples
amazon-web-servicesamazon-ec2amazon-ecsmernamazon-elb

Traget Group draining with 503 error and Unable to access port 3001 through domain name in AWS


I am new to AWS and I was trying to host a complete MERN application on EC2 using ECS and Docker. In my project, I have separate frontend and backend modules. In ECS, I have created 2 tasks(EC2), 1 each for frontend and backend. In frontend, I have added a container, which has port mapping to host mapping of 80 to 80. Also, added some environment variables. For backend, I have added 2 containers with port mappings to host mappings of 3001 to 3001 and 27017 to 27017. I have also added links and hostname. I have assigned 512 Task memory (MiB) and 512 CPU unit for frontend task and for backend task, these values are 128 MiB and 128 unit. Then, I created 2 load balancers and 2 target groups, with frontend LB have a listener on port 80 and 443 and backend LB have listeners on 3001 and 443. The frontend's target group has a protocol:port of http:80, while the backend one has of http:3001. I have also created 2 separate routes and linked to each of 2 LB. I then created 2 services with respective tasks and LBs. The issue I am having right now is with backend service. The frontend works fine. I am able to access through public IP as well as domain name(Route 53), but the backend only works with public IP. So, basically, http://x.x.x.x:3001/api works, but https://domain.name/api does not work. It shows 503 Temporary Service unavailable.

I did try to find the issue and the Target Group(backend) is trying to register the instance on port 3001, but, it keeps on failing. Also surprisongly, When the target registration starts & is in initial stage, the domain name works too, but, then it goes to unhealthy(Failed due to 404) stage, then domain shows me 502 bad gateway error. And then, target starts draining, after which it shows 503 Temporary Service unavailable. I have also opened all ports 80, 3001, 22, 443 in the security group.

Does anyone have any idea why this might be happening? Is this even possible? Do I need to post more details?

Thanks!!!


Solution

  • Ok, so, I was able to solve this problem. Did some digging and turns out, everyone was talking about Target Group's health checks urls, so, I changed the health check url from / to /backendhealth. Then, in Advanced Health Settings, I also changed the port setting from Traffic Port to Override to 80 port and it successfully registered the respective cluster's EC2 Instance on port 3001. So relieved!!!