Search code examples
node.jsamazon-web-servicesdockeramazon-ecsaws-application-load-balancer

ECS containers securely routed via API Gateway not returning but health check on containers is good


Okay so this is going to be a long one, but I want to make sure I explain. I am trying to do a proof of concept that we can deploy micro services via ECS that are exposed using API Gateway so we can leverage the lambda authorisers and cognito.

To achieve this I have been following this guide on Medium

However once I deploy my gateway I am faced with the following error:

Cannot GET /node-demo

Let me go through the basics, I have a basic express application like so:

const express = require('express')

const PORT = process.env.PORT || 3000

const app = express()

app.get('/', (request, response) => {
  return response.json({
    data: {
      message: `API is functional`,
    },
  })
})

app.listen(PORT, () => console.log(`App running on port ${PORT}`))

Dockerfile looks like this:

FROM mhart/alpine-node:10.16.3

WORKDIR /app

COPY package*.json ./

RUN npm ci

COPY index.js .

CMD ["npm", "start"]

This docker image has been pushed to ECR, I have created a Task Definition with an environment variable PORT => 80

Created a service on my cluster which is working fine. Then created an ALB with a target group that points to my containers, these come back healthy!

Healthy ALB targets

And I route to these targets with the following:

Routing

My NLB then points to the ALB and I have allowed the correct IPs on the ALB so we can get to it through the security group. Both IPs in the NLB target group are healthy

Created a VPC link in API Gateway and then created a resource like the below:

API

I then deploy hit the URL with /node-demo at the end and get the initial error above. Either the guide has a mistake or I've done something fundamentally wrong.


Solution

  • Good and bad news,

    Good news, I've solved the problem.

    Bad news, it's super simple

    Just needed to add a route to the express application,

    /node-demo