fix: stop reading private aws-cdk-lib Function.environment field#621
Open
ava-silver wants to merge 5 commits into
Open
fix: stop reading private aws-cdk-lib Function.environment field#621ava-silver wants to merge 5 commits into
ava-silver wants to merge 5 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
ava-silver
marked this pull request as ready for review
June 24, 2026 21:28
ava-silver
force-pushed
the
ava.silver/svls-9359/stop-reading-private-cdk-function-environment-field
branch
from
June 24, 2026 21:32
31ab410 to
5b06be5
Compare
janine-c
approved these changes
Jun 25, 2026
janine-c
left a comment
There was a problem hiding this comment.
Just some optional writing suggestions 🙂
|
|
||
| The DatadogLambda construct takes in a list of lambda functions and installs the Datadog Lambda Library by attaching the Lambda Layers for [.NET][19], [Java][15], [Node.js][2], and [Python][1] to your functions. It redirects to a replacement handler that initializes the Lambda Library without any required code changes. Additional configurations added to the Datadog CDK construct will also translate into their respective environment variables under each lambda function (if applicable / required). | ||
|
|
||
| **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. |
There was a problem hiding this comment.
Suggested change
| **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. | |
| To configure `DD_*` environment variables, set Datadog environment variables through `DatadogLambdaProps` (for example, `enableDatadogTracing`, `logLevel`, or `tags`). If you need to set a `DD_*` variable directly on a function, call `func.addEnvironment()` **after** `addLambdaFunctions()`. This construct overwrites any values that had been set before. |
2 tasks
ava-silver
force-pushed
the
ava.silver/svls-9359/stop-reading-private-cdk-function-environment-field
branch
from
June 25, 2026 21:19
5b06be5 to
062d08f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

What does this PR do?
Removes all reads of
aws-cdk-lib's privateFunction.environmentfield from the library. Every read is replaced with aWeakMap-backed tracker that records the library's ownaddEnvironmentcalls. Adds a publicDatadogLambda.setEnvironment()method so users can seed env vars the construct will respect.Motivation
Closes #620
aws-cdk-lib changed
Function.environment's internal shape in a semver-minor (2.252 → 2.253), breakingcdk synthfor all users ofsourceCodeIntegration(#596). The v4.0.0 fix re-pointed the access at the new internal shape and raised the peer floor -- it kept the private coupling, so the library remained one CDK refactor away from breaking again.Three call sites read across the private boundary:
env.ts--setGitEnvironmentVariablesreadsDD_TAGSto append git metadataenv.ts--applyEnvVariableschecks each key before writing a defaultdatadog-lambda.ts--overrideGitMetadatareadsDD_TAGSto rewrite git componentsChanges
src/env-tracker.ts(new)WeakMap<LambdaFunction, Map<string, string>>(ddEnvTracker) and three helpers --setTrackedEnv,getTrackedEnv,hasTrackedEnv-- that record every env write the library makes. Internal, not re-exported fromindex.ts.src/env.tsaddEnvironmentcalls insetGitEnvironmentVariables,applyEnvVariables, andsetDDEnvVariablesgo throughsetTrackedEnv.setGitEnvironmentVariablesparameter type tightened fromany[]toLambdaFunction[].src/datadog-lambda.tsoverrideGitMetadatareadsDD_TAGSfromgetTrackedEnvinstead oflambdaFunction.environment.map.get(DD_TAGS), removing theanycast. Tag rewriting extracted to anupsertTaghelper.setEnvironment(lambdaFunction, key, value), which writes throughsetTrackedEnvso the construct treats the value as one it manages.README.mdDD_*vars viaDatadogLambdaProps; callfunc.addEnvironment()afteraddLambdaFunctions()to override; or callsetEnvironment()before to seed a value the construct will respect.Behavior change
Previously the library read the private field to detect whether the user had already set a
DD_*var on a function beforeaddLambdaFunctions(), skipping its default if so -- and to append git metadata onto a user-setDD_TAGS. Without a public CDK read API (which does not exist), arbitrary pre-set env vars are no longer visible to the construct. The new contract:DatadogLambdaPropsfields (enableDatadogTracing,logLevel,tags, etc.)datadogLambda.setEnvironment(func, key, value)beforeaddLambdaFunctions(). The construct will not override it, and when source code integration is enabled it appends git metadata (git.commit.sha,git.repository_url) onto a seededDD_TAGSrather than overriding it. This preserves the prior per-functionDD_TAGS+ git-metadata workflow.func.addEnvironment("DD_*", value)afteraddLambdaFunctions()-- CDK's last-write-wins semantics handle this naturally.addLambdaFunctions()(not viasetEnvironment): will be overridden by the library's defaults.Testing Guidelines
The existing suite covers the tracker migration; two tests were updated to reflect the behavior change (comments explain the new contract), and the
overrideGitMetadatatests now assert viaTemplate.fromStack()instead of reading the CDK private field. New tests coversetEnvironment: git metadata appending onto a seededDD_TAGS, and the construct not overriding a seeded value.Run locally with
yarn test.Types of Changes
Check all that apply