Skip to content

DatadogLambda v3.12.0 crashes with aws-cdk-lib 2.253.0 — TypeError in setGitEnvironmentVariables #596

@hari-euka

Description

@hari-euka

Description

datadog-cdk-constructs-v2@3.12.0 crashes during cdk synth when used with aws-cdk-lib@2.253.0. The error occurs in setGitEnvironmentVariables() when sourceCodeIntegration is enabled (the default).

Error

node_modules/datadog-cdk-constructs-v2/lib/env.js:65
  lambdas.forEach((lam) => {
TypeError: Cannot read properties of undefined (reading 'value')
    at node_modules/datadog-cdk-constructs-v2/lib/env.js:71:20
    at Array.forEach (<anonymous>)
    at setGitEnvironmentVariables (env.js:65:11)
    at DatadogLambda.addGitCommitMetadata (datadog-lambda.js:225:31)
    at DatadogLambda.addLambdaFunctions (datadog-lambda.js:178:14)

Root Cause

In env.js lines 65-71, setGitEnvironmentVariables() does:

lambdas.forEach((lam) => {
    if (lam.environment[exports.DD_TAGS] !== undefined) {
        lam.environment[exports.DD_TAGS].value += `,git.commit.sha:${hash}`;
    } else {
        lam.addEnvironment(exports.DD_TAGS, `git.commit.sha:${hash}`);
    }
    lam.environment[exports.DD_TAGS].value += `,git.repository_url:${gitRepoUrl}`;  // <-- CRASH
});

The else branch calls lam.addEnvironment(), then immediately accesses lam.environment[DD_TAGS].value on the next line. In aws-cdk-lib@2.253.0, the internal environment property structure appears to have changed — after addEnvironment(), lam.environment[DD_TAGS] doesn't have a .value property (or is structured differently), causing the TypeError.

This worked with aws-cdk-lib@2.251.0 but breaks with 2.253.0.

Steps to Reproduce

  1. Create a CDK stack with a Python Lambda function
  2. Use datadog-cdk-constructs-v2@3.12.0 with aws-cdk-lib@2.253.0
  3. Run cdk synth
import { DatadogLambda } from "datadog-cdk-constructs-v2";

const datadog = new DatadogLambda(this, "Datadog", {
    pythonLayerVersion: 91,
    extensionLayerVersion: 55,
    apiKeySecretArn: "arn:aws:secretsmanager:...",
    site: "datadoghq.com",
    env: "staging",
    service: "my-lambda",
    version: "1.0.0",
    // sourceCodeIntegration defaults to true
});

datadog.addLambdaFunctions([lambdaFunction]);  // crashes here

Workaround

Set sourceCodeIntegration: false to disable the addGitCommitMetadata() call:

const datadog = new DatadogLambda(this, "Datadog", {
    // ... other props
    sourceCodeIntegration: false,
});

Environment

  • datadog-cdk-constructs-v2: 3.12.0
  • aws-cdk-lib: 2.253.0
  • constructs: 10.5.1
  • Node.js: 22.22.2
  • CDK CLI: 2.1119.0

Suggested Fix

Line 71 in env.js should re-check lam.environment[DD_TAGS] after the addEnvironment() call, or use addEnvironment() for the repository URL as well:

lambdas.forEach((lam) => {
    lam.addEnvironment(
        exports.DD_TAGS,
        lam.environment[exports.DD_TAGS]?.value
            ? `${lam.environment[exports.DD_TAGS].value},git.commit.sha:${hash},git.repository_url:${gitRepoUrl}`
            : `git.commit.sha:${hash},git.repository_url:${gitRepoUrl}`
    );
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions