AWS Lambda Function (NodeJS) using a Docker image fails on invocation

Viewed 564

I have a lambda function packaged as Docker container. The app is pretty simple and structured as follows:

├── Dockerfile
├── app.js
├── package-lock.json
└── package.json

The package.json is:

{
  "name": "docker-lambda-test",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

The app.js is:

module.exports.handler = async (event, context) => {
  return "test";
};

Dockerfile is:

FROM public.ecr.aws/lambda/nodejs:14
COPY app.js package*.json ${LAMBDA_TASK_ROOT}
RUN npm install
CMD [ "app.handler" ]

After building the image and executing it using the AWS RIE, the invocation fails with the error:

docker run -d -v ~/.aws-lambda-rie:/aws-lambda -p 9000:8080 \
  --entrypoint /aws-lambda/aws-lambda-rie  lambda-docker-demo:latest
ced1fcef9cdf2e275d1680b889ac1e08afb63d80d6d04f4280ae2f9f836631c3



curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
{"errorType":"TypeError","errorMessage":"Cannot read property 'includes' of undefined","trace":["TypeError: Cannot read property 'includes' of undefined","    at _throwIfInvalidHandler (/var/runtime/UserFunction.js:210:25)","    at Object.module.exports.load (/var/runtime/UserFunction.js:237:3)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:1085:14)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)","    at Module.load (internal/modules/cjs/loader.js:950:32)","    at Function.Module._load (internal/modules/cjs/loader.js:790:12)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)","    at internal/main/run_main_module.js:17:47"]}

How can I get this simple Lambda function packaged as a Docker image to work?

1 Answers

You need to copy your code in the proper directory:

/var/task

So this should fix the issue:

COPY app.js package*.json /var/task
Related