AWS Lambda 容器映像出现 Datadog 错误。 datadog-lambda-js

问题描述 投票:0回答:1

社区,我需要您的建议!

简短说明:
我正在尝试为 NodeJS 应用程序配置 Datadog 与 AWS Lambda 容器映像的集成。

我从 Lambda 收到错误:

Error: Cannot find module '/function/node_modules/datadog-lambda-js/dist/handler.handler'

关于如何调试这个有什么想法吗?


扩展说明:
我部署了 Lambda 容器映像。我使用非 AWS 基础映像创建了一个 Dockerfile,如 AWS 文档 中所示。 我的 Dockerfile:

# Define custom function directory
ARG FUNCTION_DIR="/function"

FROM node:18-buster as build-image

# Include global arg in this stage of the build
ARG FUNCTION_DIR

# Install build dependencies
RUN apt-get update && \
    apt-get install -y \
    g++ \
    make \
    cmake \
    unzip \
    libcurl4-openssl-dev

# Copy function code
RUN mkdir -p ${FUNCTION_DIR}
COPY . ${FUNCTION_DIR}

WORKDIR ${FUNCTION_DIR}

# Install Node.js dependencies
RUN npm install

# Install the runtime interface client
RUN npm install aws-lambda-ric

# Grab a fresh slim copy of the image to reduce the final size
FROM node:18-buster-slim

# Required for Node runtimes which use [email protected]+ because
# by default npm writes logs under /home/.npm and Lambda fs is read-only
ENV NPM_CONFIG_CACHE=/tmp/.npm

# Include global arg in this stage of the build
ARG FUNCTION_DIR

# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}

# Copy in the built dependencies
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}

# Set runtime interface client as default command for the container runtime
ENTRYPOINT ["/usr/local/bin/npx", "aws-lambda-ric"]


# Pass the name of the function handler as an argument to the runtime
CMD ["index.handler"]

一切顺利,我测试了该应用程序并且它有效。 现在我想配置与 Datadog 的集成。因此,我使用了 Datadog Lambda 容器映像文档,并通过安装 Datadog Lambda 库和 Datadog Lambda 扩展更改了 Dockerfile:

# Define custom function directory
ARG FUNCTION_DIR="/function"

FROM node:18-buster as build-image

# Include global arg in this stage of the build
ARG FUNCTION_DIR

# Install build dependencies
RUN apt-get update && \
    apt-get install -y \
    g++ \
    make \
    cmake \
    unzip \
    libcurl4-openssl-dev

# Copy function code
RUN mkdir -p ${FUNCTION_DIR}
COPY . ${FUNCTION_DIR}

WORKDIR ${FUNCTION_DIR}

# Install Node.js dependencies
RUN npm install

# Install the runtime interface client
RUN npm install aws-lambda-ric

# Install the Datadog Lambda Library
RUN npm install datadog-lambda-js dd-trace

# Grab a fresh slim copy of the image to reduce the final size
FROM node:18-buster-slim

# Required for Node runtimes which use [email protected]+ because
# by default npm writes logs under /home/.npm and Lambda fs is read-only
ENV NPM_CONFIG_CACHE=/tmp/.npm

# Include global arg in this stage of the build
ARG FUNCTION_DIR

# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}

# Install the Datadog Lambda Extension
COPY --from=public.ecr.aws/datadog/lambda-extension:55 /opt/extensions/ /opt/extensions

# Copy in the built dependencies
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}

ENV DD_SITE="datadoghq.com"
ENV DD_API_KEY="SECRET"
ENV DD_LAMBDA_HANDLER="handler.handler"
# Redirect the handler function
CMD ["node_modules/datadog-lambda-js/dist/handler.handler"]

之后我收到来自 Lambda 的错误: 错误:找不到模块'/function/node_modules/datadog-lambda-js/dist/handler.handler'

但是模块已呈现。我在本地运行容器并看到这个目录。

我应该尝试什么?

amazon-web-services lambda dockerfile datadog
1个回答
0
投票

我刚刚尝试了 AWS 基础镜像,它可以工作!
一个新的 Dockerfile:

FROM public.ecr.aws/lambda/nodejs:20 as builder

COPY . ${LAMBDA_TASK_ROOT}
RUN npm install
RUN npm install datadog-lambda-js dd-trace

COPY --from=public.ecr.aws/datadog/lambda-extension:55 /opt/extensions/ /opt/extensions

ENV DD_LAMBDA_HANDLER="handler.handler"

CMD ["node_modules/datadog-lambda-js/dist/handler.handler"]
© www.soinside.com 2019 - 2024. All rights reserved.