Skip to content

Commit 82cb41d

Browse files
committed
[SVLS-9359] add setEnvironment method to seed construct-tracked env vars
1 parent 4eb6d10 commit 82cb41d

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ The DatadogLambda construct takes in a list of lambda functions and installs the
404404

405405
**Configuring `DD_*` environment variables:** Set Datadog environment variables through `DatadogLambdaProps` (e.g. `enableDatadogTracing`, `logLevel`, `tags`). If you need to set a `DD_*` variable directly on a function, call `func.addEnvironment()` **after** `addLambdaFunctions()` -- values set before will be overridden by the construct.
406406

407+
To set a value the construct should respect (rather than override), use `datadogLambda.setEnvironment(func, key, value)` **before** `addLambdaFunctions()`. This is useful for setting `DD_TAGS` per function: when source code integration is enabled, the construct appends git metadata (`git.commit.sha`, `git.repository_url`) to the value you set instead of overriding it.
408+
407409
While Lambda function based log groups are handled by the `addLambdaFunctions` method automatically, the construct has an additional function `addForwarderToNonLambdaLogGroups` which subscribes the forwarder to any additional log groups of your choosing.
408410

409411
## Step Functions

src/datadog-lambda.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,20 @@ export class DatadogLambda extends Construct {
182182
}
183183
}
184184

185+
/**
186+
* Sets an environment variable on the given Lambda function and records it in the
187+
* construct's internal tracker, so the construct treats it as a value it manages.
188+
*
189+
* Call this before `addLambdaFunctions()` to seed values the construct will respect.
190+
* The main use case is setting `DD_TAGS` per function: when source code integration is
191+
* enabled, the construct appends git metadata (`git.commit.sha`, `git.repository_url`)
192+
* to the tracked `DD_TAGS` rather than overriding it.
193+
*/
194+
public setEnvironment(lambdaFunction: LambdaFunction, key: string, value: string): void {
195+
const [extractedLambdaFunction] = extractSingletonFunctions([lambdaFunction]);
196+
setTrackedEnv(extractedLambdaFunction, key, value);
197+
}
198+
185199
public overrideGitMetadata(gitCommitSha: string, gitRepoUrl?: string): void {
186200
if (gitCommitSha) {
187201
this.gitCommitShaOverride = gitCommitSha;

test/datadog-lambda.spec.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,3 +1096,74 @@ describe("overrideGitMetadata", () => {
10961096
});
10971097
});
10981098
});
1099+
1100+
describe("setEnvironment", () => {
1101+
it("appends git metadata to a DD_TAGS value seeded before addLambdaFunctions", () => {
1102+
const app = new App();
1103+
const stack = new Stack(app, "stack");
1104+
const hello = new lambda.Function(stack, "HelloHandler", {
1105+
runtime: lambda.Runtime.NODEJS_18_X,
1106+
code: lambda.Code.fromInline("test"),
1107+
handler: "hello.handler",
1108+
});
1109+
const datadogLambda = new DatadogLambda(stack, "Datadog", {
1110+
nodeLayerVersion: NODE_LAYER_VERSION,
1111+
extensionLayerVersion: EXTENSION_LAYER_VERSION,
1112+
apiKey: "ABC",
1113+
enableDatadogTracing: false,
1114+
flushMetricsToLogs: false,
1115+
logLevel: "debug",
1116+
});
1117+
datadogLambda.overrideGitMetadata("fake-sha", "fake-url");
1118+
datadogLambda.setEnvironment(hello, DD_TAGS, "team:serverless");
1119+
datadogLambda.addLambdaFunctions([hello], stack);
1120+
1121+
Template.fromStack(stack).hasResourceProperties("AWS::Lambda::Function", {
1122+
Environment: {
1123+
Variables: {
1124+
[DD_TAGS]: Match.stringLikeRegexp("team:serverless"),
1125+
},
1126+
},
1127+
});
1128+
Template.fromStack(stack).hasResourceProperties("AWS::Lambda::Function", {
1129+
Environment: {
1130+
Variables: {
1131+
[DD_TAGS]: Match.stringLikeRegexp("git\\.commit\\.sha:fake-sha"),
1132+
},
1133+
},
1134+
});
1135+
Template.fromStack(stack).hasResourceProperties("AWS::Lambda::Function", {
1136+
Environment: {
1137+
Variables: {
1138+
[DD_TAGS]: Match.stringLikeRegexp("git\\.repository_url:fake-url"),
1139+
},
1140+
},
1141+
});
1142+
});
1143+
1144+
it("does not override a value seeded before addLambdaFunctions", () => {
1145+
const app = new App();
1146+
const stack = new Stack(app, "stack");
1147+
const hello = new lambda.Function(stack, "HelloHandler", {
1148+
runtime: lambda.Runtime.NODEJS_18_X,
1149+
code: lambda.Code.fromInline("test"),
1150+
handler: "hello.handler",
1151+
});
1152+
const datadogLambda = new DatadogLambda(stack, "Datadog", {
1153+
nodeLayerVersion: NODE_LAYER_VERSION,
1154+
apiKey: "ABC",
1155+
enableDatadogTracing: true,
1156+
flushMetricsToLogs: false,
1157+
});
1158+
datadogLambda.setEnvironment(hello, "DD_TRACE_ENABLED", "false");
1159+
datadogLambda.addLambdaFunctions([hello], stack);
1160+
1161+
Template.fromStack(stack).hasResourceProperties("AWS::Lambda::Function", {
1162+
Environment: {
1163+
Variables: {
1164+
DD_TRACE_ENABLED: "false",
1165+
},
1166+
},
1167+
});
1168+
});
1169+
});

0 commit comments

Comments
 (0)