[RECIPE]: AWS ECS deployment and Health Checks
Recently my regular deployment of NestJS Backend API to Amazon ECS service failed without any meaningful reasons. What I see is that Docker container is taken, everything inside is executed successfully, I see logs of service which has started and even (but why) some requests to my endpoints. Seems like ok, but every minute AWS would restart all ECS update process again and again.
Turned out that ECS considered my service failed to start, and tried to install it once again.
I've already seen such behavior recently, but I saw clear error in logs, which caused such behavior; no errors now.
After a while I opened a particular task in load balancing and found that it tried to do Health Checks. Official documentation about this topic tells us that after deployment and if no errors, ECS tries to do Health Check by:
- either request configured in CloudFormation HealthCheckPath
- or request base endpoint of API - `/`
In my case, we just added authentication to the whole API, including base path, default endpoint. And all this caused such behavior.
Particular sample from in abstract NestJS project. You should avoid using @UseGuards(AuthGuard)
where AuthGuard is CanActivate guard, docs here
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
// this is health check endpoint, it should be open and not auth guarded
// critical for ECS deployment
@Get()
healthCheck(): string {
return 'Service is Up and Running';
}
}