Myrkytyn
Myrkytyn

Reputation: 101

AWS Lambda Container Image with Datadog error. datadog-lambda-js

community, I need your advice!

Short explanation:
I'm trying to configure Datadog integration with AWS Lambda Container Image for NodeJS app.

I got an error from Lambda:

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

Any ideas on how to debug this?


Extended explanation:
I deployed Lambda Container Image. I created a Dockerfile like in the AWS Documentation using a non-AWS base image. My 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"]

Everything worked well, I tested the app and it worked. Now I want to configure integration with Datadog. So I used Datadog documentation for Lambda Container Images and changed the Dockerfile with the installation of Datadog Lambda Library and Datadog Lambda Extension:

# 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"]

After that I got an error from Lambda: Error: Cannot find module '/function/node_modules/datadog-lambda-js/dist/handler.handler'

But the module presented. I ran the container locally and saw this directory. Proof that module presented

What should I try?

Upvotes: 1

Views: 421

Answers (2)

adamsxs
adamsxs

Reputation: 86

For custom AWS ECR images that use the Datadog Lambda Layer, you need to add the Datadog Lambda Layer AND still use the AWS RIC entrypoint. Your second dockerfile code snippet should still include the ENTRYPOINT from your first snippet.

ENTRYPOINT ["/usr/local/bin/npx", "aws-lambda-ric"]
CMD ["node_modules/datadog-lambda-js/dist/handler.handler"]

Switching to the AWS-provided image fixed things for you since it has the RIC configured.

Upvotes: 0

Myrkytyn
Myrkytyn

Reputation: 101

I just tried the AWS base image and it works!
A new 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"]

Upvotes: 0

Related Questions