Skip to content

fix: prevent TypeError in setGitEnvironmentVariables with CDK 2.253+#597

Closed
hari-euka wants to merge 1 commit into
DataDog:mainfrom
hari-euka:fix/cdk-2.253-source-code-integration-crash
Closed

fix: prevent TypeError in setGitEnvironmentVariables with CDK 2.253+#597
hari-euka wants to merge 1 commit into
DataDog:mainfrom
hari-euka:fix/cdk-2.253-source-code-integration-crash

Conversation

@hari-euka
Copy link
Copy Markdown

Summary

Fixes #596DatadogLambda.addLambdaFunctions() crashes with TypeError: Cannot read properties of undefined (reading 'value') when used with aws-cdk-lib >= 2.253.0.

Root Cause

In src/env.ts, setGitEnvironmentVariables() accesses the internal lam.environment[DD_TAGS].value property unconditionally on line 71, after the else branch calls lam.addEnvironment():

// Before (broken with CDK 2.253+)
lambdas.forEach((lam) => {
    if (lam.environment[DD_TAGS] !== undefined) {
        lam.environment[DD_TAGS].value += `,git.commit.sha:${hash}`;
    } else {
        lam.addEnvironment(DD_TAGS, `git.commit.sha:${hash}`);
    }
    // This line crashes — after addEnvironment(), the internal
    // .environment structure in CDK 2.253 doesn't expose .value
    lam.environment[DD_TAGS].value += `,git.repository_url:${gitRepoUrl}`;
});

In aws-cdk-lib@2.253.0, the internal environment property structure on Lambda functions changed. After calling addEnvironment(), lam.environment[DD_TAGS] may not have a .value property, causing the TypeError.

Fix

Use addEnvironment() via the public API consistently instead of directly mutating the internal .environment property:

// After (works with all CDK versions)
const gitTags = `git.commit.sha:${hash},git.repository_url:${gitRepoUrl}`;

lambdas.forEach((lam) => {
    const existingDdTags = lam.environment[DD_TAGS]?.value;
    if (existingDdTags) {
        lam.addEnvironment(DD_TAGS, `${existingDdTags},${gitTags}`);
    } else {
        lam.addEnvironment(DD_TAGS, gitTags);
    }
});

Key changes:

  • Uses optional chaining (?.value) to safely read existing value
  • Builds the full git tags string upfront (both git.commit.sha and git.repository_url)
  • Uses addEnvironment() for all writes — never directly mutates .value
  • Simpler code — no split between setting sha and repo url

Tests

All 240 existing tests pass, including the 4 DD_TAGS tests that verify:

  • git.commit.sha and git.repository_url are set by default
  • Existing DD_TAGS values are preserved when adding git metadata
  • Source code integration can be disabled

Workaround

For users who need an immediate fix before this is released:

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

setGitEnvironmentVariables() crashes with 'Cannot read properties of
undefined (reading "value")' when used with aws-cdk-lib >= 2.253.0.

The issue is on line 71 where lam.environment[DD_TAGS].value is
accessed unconditionally after the else branch calls addEnvironment().
In CDK 2.253, the internal .environment property structure changed,
so .value may not exist after addEnvironment().

Fix: use addEnvironment() consistently via the public API instead of
directly mutating the internal .environment property. This also
simplifies the code by building the full git tags string upfront
and using optional chaining for the existing value check.

Fixes DataDog#596
@hari-euka hari-euka requested a review from a team as a code owner May 7, 2026 23:50
@hari-euka hari-euka requested a review from nina9753 May 7, 2026 23:50
@samchungy
Copy link
Copy Markdown
Contributor

samchungy commented May 10, 2026

Unfortunately this doesn't actually fix the underlying issue that you can't actually access environment variables anymore: aws/aws-cdk#37773

@TalUsvyatsky
Copy link
Copy Markdown
Contributor

Thanks for opening this PR, this was ultimately resolved with #601 and https://github.com/DataDog/datadog-cdk-constructs/releases/tag/v2-4.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants