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
- Create a CDK stack with a Python Lambda function
- Use
datadog-cdk-constructs-v2@3.12.0 with aws-cdk-lib@2.253.0
- 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}`
);
});
Description
datadog-cdk-constructs-v2@3.12.0crashes duringcdk synthwhen used withaws-cdk-lib@2.253.0. The error occurs insetGitEnvironmentVariables()whensourceCodeIntegrationis enabled (the default).Error
Root Cause
In
env.jslines 65-71,setGitEnvironmentVariables()does:The
elsebranch callslam.addEnvironment(), then immediately accesseslam.environment[DD_TAGS].valueon the next line. Inaws-cdk-lib@2.253.0, the internalenvironmentproperty structure appears to have changed — afteraddEnvironment(),lam.environment[DD_TAGS]doesn't have a.valueproperty (or is structured differently), causing the TypeError.This worked with
aws-cdk-lib@2.251.0but breaks with2.253.0.Steps to Reproduce
datadog-cdk-constructs-v2@3.12.0withaws-cdk-lib@2.253.0cdk synthWorkaround
Set
sourceCodeIntegration: falseto disable theaddGitCommitMetadata()call:Environment
datadog-cdk-constructs-v2: 3.12.0aws-cdk-lib: 2.253.0constructs: 10.5.1Suggested Fix
Line 71 in
env.jsshould re-checklam.environment[DD_TAGS]after theaddEnvironment()call, or useaddEnvironment()for the repository URL as well: