Search code examples
node.jsamazon-web-servicesamazon-elbhealth-monitoring

Code to enable healthcheck in node app for ALB


Im checking to enable Application Load Balancer in AWS using Node. In my EC2 instances Im using a nginx proxy to send to 3000 port and all I configure is a "/healthcheck" path to validate in nginx and node the health control. In nginx was quite easy

location /healthcheck {
return 200;

}

But for node I can't find a clean example to enable same function for 3002 port. For now Im having a timeout error.

Thanks

PD: I checked history and last answer is 3 years old creating a ping.html file How to configure AWS Elastic Load Balancer's heath check for Node.js


Solution

  • You basically need to expose a healtcheck endpoint. It will depend on what are you using to create a web server. options are express, http, hapijs etc. The simplest route using express could be

    const express = require('express')
    const app = express()
    const port = 3002
    
    app.get('/healthcheck', (req, res) => res.send('Hello World!'))
    
    app.listen(port, () => console.log(`Example app listening on port ${port}!`));
    

    In your ALB, you can simply use http://someIp:3002/healthcheck. If it returns 200 that means your app is up else not. You need to make sure the port is also open for communication on your ec2.