I wanted to create node16
container image for aws lambda. I have following Dockerfile
and index.js
for lambda function. Building image (docker build -t lambda-hello-world .
) works fine but when I invoke( docker run --rm -p 8080:8080 lambda-hello-world
) lambda function it returns error. Could someone please suggest?
2022-05-07T15:56:51.182Z undefined INFO Executing 'index.handler' in function directory '/'
/node_modules/aws-lambda-ric/lib/index.js:42
throw new Error("Missing Runtime API Server configuration.");
^
Error: Missing Runtime API Server configuration.
at Object.run (/node_modules/aws-lambda-ric/lib/index.js:42:15)
at Object.<anonymous> (/node_modules/aws-lambda-ric/bin/index.js:14:8)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47```
enter code here
Dockerfile
FROM node:16-alpine
# Bundle app source
COPY . .
RUN npm install
EXPOSE 8080
ENTRYPOINT ["npx", "aws-lambda-ric"]
CMD ["index.handler"]
index.js
exports.handler = async (event, context) => {
return `Hello World form node version ${process.version}`;
}
CodePudding user response:
According to this issue, whenever your Docker image is invoked, whether in Lambda or on your local machine, you need an entry script that will help your use the RIE proxy when necessary (e.g., on our local machine).
entry.sh
in root project:
#!/bin/sh
# Check if the AWS_LAMBDA_RUNTIME_API is not set. This environment
# variable is set when the image is running on Lambda.
if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
# We know the image is being run off of Lambda, so we need to use the RIE
# to start the function.
exec /usr/bin/aws-lambda-rie ./node_modules/.bin/aws-lambda-ric $1
else
# We know the image is being run on Lambda so we don't need to use the RIE.
exec ./node_modules/.bin/aws-lambda-ric $1
fi
Your Dockerfile:
FROM node:16-alpine
# Bundle app source
COPY . .
RUN npm install
EXPOSE 8080
# Downloads the Lambda Runtime Interface Emulator (RIE)
ADD https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie /usr/bin/aws-lambda-rie
RUN chmod x /usr/bin/aws-lambda-rie
RUN chmod x entry.sh
ENTRYPOINT ["entry.sh"]
CMD ["index.handler"]