无法在 aws lambda 中为 node-alpine 创建 docker
failed to create a docker for node-alpine in aws lambda
我在尝试为 aws lambda 创建 docker 图像时遇到以下错误
基于节点 js 打字稿图像 (NestJs)
当我将示例 app.js 文件与处理函数
一起使用时,也会发生此错误
internal/modules/cjs/loader.js:905
throw err;
^
Error: Cannot find module '/function/main.handler'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
at Function.Module._load (internal/modules/cjs/loader.js:746:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
我的 运行 命令:
docker run --rm -p 9001:8080 lambda-test:latest
ARG FUNCTION_DIR="/function"
#*****************************************************************************
# Builder Stage
#****************************************************************************/
FROM node:14-alpine AS builder
# Include global arg in this stage of the build
ARG FUNCTION_DIR
# The working directory is where "npm install" created the node_modules folder.
WORKDIR ${FUNCTION_DIR}
# Install aws-lambda-cpp build dependencies. aws-lambda-cpp is used by aws-lambda-ric which is a
# node-gyp compiled dependency. Find it in package.json.
# See the Node.js example at https://github.com/aws/aws-lambda-nodejs-runtime-interface-client
RUN apk add --no-cache \
libstdc++ \
build-base \
libtool \
autoconf \
automake \
libexecinfo-dev \
make \
cmake \
libcurl \
python3
RUN mkdir -p ${FUNCTION_DIR}
COPY . ${FUNCTION_DIR}
# Install the AWS Lambda Runtime Interface Client (RIC) that is only required within this Docker container (not in package.json on development machine).
# It helps AWS to run the Lambda function code that autoiXpert provides.
RUN npm install
RUN npm install aws-lambda-ric
RUN npx tsc
RUN npm prune --production
#*****************************************************************************
# Production Stage
#****************************************************************************/
FROM node:14-alpine
# Include global arg in this stage of the build
ARG FUNCTION_DIR
# The working directory is where "npm install" created the node_modules folder.
WORKDIR ${FUNCTION_DIR}
# If this directory does not exist, lambda shows an annoying warning.
RUN mkdir -p /opt/extensions
COPY --from=builder ${FUNCTION_DIR}/node_modules ${FUNCTION_DIR}/node_modules
COPY --from=builder ${FUNCTION_DIR}/package*.json ${FUNCTION_DIR}
COPY --from=builder ${FUNCTION_DIR}/dist/src* ${FUNCTION_DIR}
CMD [ "main.handler" ]
所有文件都正确定位
当我使用 aws 基本图像时,它的效果很好,但图像大小约为 800mb
高山是 300mb
您在 Dockerfile 的最后阶段错过了 ENTRYPOINT ["/usr/local/bin/npx", "aws-lambda-ric"]
。
图片node:14-alpine
的默认ENTRY_POINT是
#!/bin/sh
set -e
if [ "${1#-}" != "" ] || [ -z "$(command -v "")" ]; then
set -- node "$@"
fi
exec "$@"
所以CMD [ "main.handler" ]
等同于node main.handler
,这导致Error: Cannot find module '/function/main.handler'
。
要解决此问题,您应该使用
将入口点更改为 aws-lambda-ric
ENTRYPOINT ["/usr/local/bin/npx", "aws-lambda-ric"]
我在尝试为 aws lambda 创建 docker 图像时遇到以下错误 基于节点 js 打字稿图像 (NestJs) 当我将示例 app.js 文件与处理函数
一起使用时,也会发生此错误internal/modules/cjs/loader.js:905
throw err;
^
Error: Cannot find module '/function/main.handler'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
at Function.Module._load (internal/modules/cjs/loader.js:746:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
我的 运行 命令:
docker run --rm -p 9001:8080 lambda-test:latest
ARG FUNCTION_DIR="/function"
#*****************************************************************************
# Builder Stage
#****************************************************************************/
FROM node:14-alpine AS builder
# Include global arg in this stage of the build
ARG FUNCTION_DIR
# The working directory is where "npm install" created the node_modules folder.
WORKDIR ${FUNCTION_DIR}
# Install aws-lambda-cpp build dependencies. aws-lambda-cpp is used by aws-lambda-ric which is a
# node-gyp compiled dependency. Find it in package.json.
# See the Node.js example at https://github.com/aws/aws-lambda-nodejs-runtime-interface-client
RUN apk add --no-cache \
libstdc++ \
build-base \
libtool \
autoconf \
automake \
libexecinfo-dev \
make \
cmake \
libcurl \
python3
RUN mkdir -p ${FUNCTION_DIR}
COPY . ${FUNCTION_DIR}
# Install the AWS Lambda Runtime Interface Client (RIC) that is only required within this Docker container (not in package.json on development machine).
# It helps AWS to run the Lambda function code that autoiXpert provides.
RUN npm install
RUN npm install aws-lambda-ric
RUN npx tsc
RUN npm prune --production
#*****************************************************************************
# Production Stage
#****************************************************************************/
FROM node:14-alpine
# Include global arg in this stage of the build
ARG FUNCTION_DIR
# The working directory is where "npm install" created the node_modules folder.
WORKDIR ${FUNCTION_DIR}
# If this directory does not exist, lambda shows an annoying warning.
RUN mkdir -p /opt/extensions
COPY --from=builder ${FUNCTION_DIR}/node_modules ${FUNCTION_DIR}/node_modules
COPY --from=builder ${FUNCTION_DIR}/package*.json ${FUNCTION_DIR}
COPY --from=builder ${FUNCTION_DIR}/dist/src* ${FUNCTION_DIR}
CMD [ "main.handler" ]
所有文件都正确定位
当我使用 aws 基本图像时,它的效果很好,但图像大小约为 800mb 高山是 300mb
您在 Dockerfile 的最后阶段错过了 ENTRYPOINT ["/usr/local/bin/npx", "aws-lambda-ric"]
。
图片node:14-alpine
的默认ENTRY_POINT是
#!/bin/sh
set -e
if [ "${1#-}" != "" ] || [ -z "$(command -v "")" ]; then
set -- node "$@"
fi
exec "$@"
所以CMD [ "main.handler" ]
等同于node main.handler
,这导致Error: Cannot find module '/function/main.handler'
。
要解决此问题,您应该使用
将入口点更改为aws-lambda-ric
ENTRYPOINT ["/usr/local/bin/npx", "aws-lambda-ric"]