AWS Lambda 节点 16 容器映像错误(缺少运行时 API 服务器配置)
AWS Lambda Node 16 container image error ( Missing Runtime API Server configuration )
我想为 aws lambda 创建 node16
容器映像。对于 lambda 函数,我有以下 Dockerfile
和 index.js
。构建图像 (docker build -t lambda-hello-world .
) 工作正常但是当我调用 (docker run --rm -p 8080:8080 lambda-hello-world
) lambda 函数时它 returns 错误。有人可以建议吗?
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}`;
}
根据此 issue,每当调用您的 Docker 图像时,无论是在 Lambda 中还是在您的本地计算机上,您都需要一个入口脚本来帮助您在必要时使用 RIE 代理(例如,在我们的本地机器上)。
entry.sh
在根项目中:
#!/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
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
fi
您的Docker文件:
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"]
我想为 aws lambda 创建 node16
容器映像。对于 lambda 函数,我有以下 Dockerfile
和 index.js
。构建图像 (docker build -t lambda-hello-world .
) 工作正常但是当我调用 (docker run --rm -p 8080:8080 lambda-hello-world
) lambda 函数时它 returns 错误。有人可以建议吗?
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}`;
}
根据此 issue,每当调用您的 Docker 图像时,无论是在 Lambda 中还是在您的本地计算机上,您都需要一个入口脚本来帮助您在必要时使用 RIE 代理(例如,在我们的本地机器上)。
entry.sh
在根项目中:
#!/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
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
fi
您的Docker文件:
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"]