Dockerized Node API with Serverless Framework doesn't work

52 Views Asked by At

I wanted to setup a simple dockerized API with Serverless Framework. I do something wrong, because CloudWatch is throwing me Runtime.InvalidEntrypoint error once I access the Lambda function URL.

My commands I used are:

  • npm init -y
  • serverless deploy

Both successful.

Error:

INIT_REPORT Init Duration: 4.66 ms Phase: init Status: error Error Type: Runtime.InvalidEntrypoint

INIT_REPORT Init Duration: 0.58 ms Phase: invoke Status: error Error Type: Runtime.InvalidEntrypoint

START RequestId: 42c8d6e8-e848-461c-a6bd-5eebb839d86b Version: $LATEST

RequestId: 42c8d6e8-e848-461c-a6bd-5eebb839d86b Error: fork/exec /lambda-entrypoint.sh: exec format error Runtime.InvalidEntrypoint

./app.js

"use strict";

module.exports.handler = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: `Hello, world!`,
      },
      null,
      2
    ),
  };
};

./Dockerfile

FROM public.ecr.aws/lambda/nodejs:18

COPY package.json ./
RUN npm install

COPY app.js .
CMD ["app.handler"]

./serverless.yml

service: lambda-hello

provider:
  name: aws
  region: ap-southeast-1
  ecr:
    images:
      appimage:
        path: ./

functions:
  hello:
    image:
      name: appimage
      command:
        - app.handler
    events:
      - http:
          path: /{any+}
          method: any

Making the API runnable & accessable.

1

There are 1 best solutions below

0
helpinghand On

I’ve faced a similar issue in the past and in my case, the issue was with the way the Lambda entry point was configured in my Dockerfile, which is quite similar to what you’ve shown. You can read more about working with lambda container images here

Usually, AWS Lambda base images handle the command and entry point internally, and you shouldn’t need to specify them explicitly if your handler is written in the supported language and follows the expected naming conventions, which you’ve already done.

The handler you’ve specified in the function's definition within the Serverless Framework configuration (serverless.yml) file is good. So just modify your Dockerfile to take out the last line:

FROM public.ecr.aws/lambda/nodejs:18
COPY package*.json ./
RUN npm install
COPY app.js .
# The base image's CMD is used by default; no need to specify CMD ["app.handler"]

After making the change to your Dockerfile, rebuild your Docker image, and redeploy your service using Serverless Framework:

serverless deploy

With these changes, AWS Lambda will automatically use the entry point defined in the base image, and it will locate your handler based on the command specified in serverless.yml.