ECS: Unable to launch nodejs application

23 Views Asked by At

I am trying to launch a NodeJS application (api-gateway) using AWS ECS Fargate. The docker image was built by running the command docker build --target production -t api-gateway . and is pushed to ECR. My Dockerfile looks like below:

###################
# BUILD FOR LOCAL DEVELOPMENT
###################

FROM node:18-alpine AS development

# Set the working directory inside the container
WORKDIR /usr/src/app

COPY package.json ./
COPY yarn.lock ./

RUN yarn install --immutable --immutable-cache

# Bundle app source
COPY . .

###################
# BUILD FOR PRODUCTION
###################
FROM development AS build

RUN yarn build

# This ensures that the node_modules directory is as optimized as possible
RUN yarn install --production --immutable --immutable-cache && yarn cache clean

###################
# RUN PRODUCTION
###################
FROM node:18-alpine AS production

WORKDIR /usr/src/app

# Copy the bundled code from the build stage to the production image
COPY --from=build /usr/src/app/package.json ./package.json
COPY --from=build /usr/src/app/node_modules ./node_modules
COPY --from=build /usr/src/app/dist ./dist

If I run a container locally using the built image, everything looks fine. However, when I launch a task on ECS it fails with the error:

Error: Cannot find module '/usr/src/app/node dist/apps/api-gateway/main'

My task definition looks like this:

{
  "containerDefinitions": [
    {
      "name": "api-gateway",
      "image": "998947330855.dkr.ecr.eu-west-1.amazonaws.com/api-gateway:latest-testing",
      "cpu": 512,
      "memory": 1024,
      "portMappings": [
        {
          "name": "api-gateway-3600-tcp",
          "containerPort": 3600,
          "hostPort": 3600,
          "protocol": "tcp"
        }
      ],
      "essential": true,
      "command": [
        "node",
        "dist/apps/api-gateway/main"
      ],
      "workingDirectory": "/usr/src/app",
      "environmentFiles": [
        {
          "value": "arn:aws:s3:::env-variables/testing/api.env",
          "type": "s3"
        }
      ],
      "mountPoints": [
        {
          "sourceVolume": "app-volume",
          "containerPath": "/usr/src/app"
        }
      ],
      "healthCheck": {
        "command": [
          "CMD-SHELL",
          "curl -f http://localhost:3600/health || exit 1"
        ],
        "interval": 30,
        "timeout": 5,
        "retries": 3,
        "startPeriod": 60
      }
    },
  ],
  "family": "api-gateway-testing",
  "taskRoleArn": "arn:aws:iam::998947330855:role/ecsTaskExecutionRole",
  "executionRoleArn": "arn:aws:iam::998947330855:role/ecsTaskExecutionRole",
  "networkMode": "awsvpc",
  "volumes": [
    {
      "name": "app-volume",
      "host": {}
    }
  ],
  ],
  "compatibilities": [
    "EC2",
    "FARGATE"
  ],
  "requiresCompatibilities": [
    "FARGATE"
  ],
  "cpu": "4096",
  "memory": "8192",
  "ephemeralStorage": {
    "sizeInGiB": 50
  },
  "runtimePlatform": {
    "cpuArchitecture": "X86_64",
    "operatingSystemFamily": "LINUX"
  },
}

What am I missing in my config, to make the task run successfully?

0

There are 0 best solutions below