Retain custom environment variables in env-config.json between translation - #2131
Merged
Conversation
JayPolanco
previously approved these changes
May 28, 2026
sky333999
reviewed
May 28, 2026
jefchien
force-pushed
the
retain-envconfig
branch
from
May 28, 2026 20:23
bc795a3 to
0a5f49e
Compare
movence
reviewed
May 29, 2026
okankoAMZ
reviewed
Jun 3, 2026
|
|
||
| // ReplaceEnvConfigFile merges the given values into the env-config.json at path, | ||
| // first removing keysToRemove so stale values don't persist. | ||
| func ReplaceEnvConfigFile(path string, values map[string]string, keysToRemove []string) error { |
Contributor
There was a problem hiding this comment.
I know that this function is NOT atomic so it can technically corrupt the env.json via a race condition. I think it should be okay since we have the 0644; however, if the user sets to run CWAgent as X user, is it possible for that user to right to the env-config.json and cause the race condition?
Contributor
Author
There was a problem hiding this comment.
I suppose if they're writing to it at the exact same time as the translation. I think that's difficult to avoid.
Contributor
|
This PR was marked stale due to lack of activity. |
Contributor
|
This PR was marked stale due to lack of activity. |
jefchien
force-pushed
the
retain-envconfig
branch
from
June 25, 2026 15:38
309be7f to
2bc5f5c
Compare
mitali-salvi
approved these changes
Jun 25, 2026
okankoAMZ
approved these changes
Jun 26, 2026
jefchien
pushed a commit
that referenced
this pull request
Jul 21, 2026
Adds a 'set-env' action to the Linux and Windows ctl scripts that wraps the agent binary's internal -setenv/-envconfig flags, so callers (e.g. setup scripts) no longer need to invoke the raw agent binary or hardcode the env-config.json path. Usage: amazon-cloudwatch-agent-ctl -a set-env -e KEY=VALUE amazon-cloudwatch-agent-ctl.ps1 -a set-env -e KEY=VALUE Keys set this way are retained across config translations (#2131) unless they are translator-managed keys.
jefchien
pushed a commit
that referenced
this pull request
Jul 21, 2026
Adds a 'set-env' action to the Linux and Windows ctl scripts that wraps the agent binary's internal -setenv/-envconfig flags, so callers (e.g. setup scripts) no longer need to invoke the raw agent binary or hardcode the env-config.json path. Usage: amazon-cloudwatch-agent-ctl -a set-env -e KEY=VALUE amazon-cloudwatch-agent-ctl.ps1 -a set-env -e KEY=VALUE Keys set this way are retained across config translations (#2131) unless they are translator-managed keys.
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.
Description of the issue
When the config translator runs (e.g. on
fetch-config), it overwritesenv-config.jsonentirely. Any values written to that file outside of translation are lost. This means custom environment variables are not retained.Description of changes
Adds file-level helpers to the existing
cfg/envconfigpackage (ReadFile,LoadFile,MergeFile) that centralize allenv-config.jsonI/O.The key behavioral change is in
TranslateJSONMapToEnvConfigFile. Instead of overwritingenv-config.json, it now callsMergeFilewithtoenvconfig.ManagedKeys, the explicit set of keys that translation owns. On each translation, managed keys are reset to reflect the current JSON config (clearing stale values), while unmanaged keys are preserved.Since the agent is trying to retain custom environment variables now, an incorrectly formatted
env-config.jsonwill prevent the agent from starting rather than wiping the values.License
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Tests
Added unit tests. Built and tested on EC2 instance.
Requirements
Before commiting your code, please do the following steps.
make fmtandmake fmt-shmake lintIntegration Tests
To run integration tests against this PR, add the
ready for testinglabel.