Skip to content

Retain custom environment variables in env-config.json between translation - #2131

Merged
jefchien merged 13 commits into
mainfrom
retain-envconfig
Jun 26, 2026
Merged

Retain custom environment variables in env-config.json between translation#2131
jefchien merged 13 commits into
mainfrom
retain-envconfig

Conversation

@jefchien

@jefchien jefchien commented May 28, 2026

Copy link
Copy Markdown
Contributor

Description of the issue

When the config translator runs (e.g. on fetch-config), it overwrites env-config.json entirely. 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/envconfig package (ReadFile, LoadFile, MergeFile) that centralize all env-config.json I/O.

The key behavioral change is in TranslateJSONMapToEnvConfigFile. Instead of overwriting env-config.json, it now calls MergeFile with toenvconfig.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.json will 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.

  1. Run make fmt and make fmt-sh
  2. Run make lint

Integration Tests

To run integration tests against this PR, add the ready for testing label.

@jefchien
jefchien requested a review from a team as a code owner May 28, 2026 14:28
@jefchien jefchien added the ready for testing Indicates this PR is ready for integration tests to run label May 28, 2026
JayPolanco
JayPolanco previously approved these changes May 28, 2026
Comment thread cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go Outdated
@jefchien
jefchien force-pushed the retain-envconfig branch from bc795a3 to 0a5f49e Compare May 28, 2026 20:23
Comment thread cfg/envconfig/file.go Outdated
Comment thread cfg/envconfig/file.go Outdated
Comment thread cfg/envconfig/file.go Outdated
Comment thread cfg/envconfig/file.go Outdated

// 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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose if they're writing to it at the exact same time as the translation. I think that's difficult to avoid.

Comment thread cfg/envconfig/file.go Outdated
@github-actions

Copy link
Copy Markdown
Contributor

This PR was marked stale due to lack of activity.

@github-actions github-actions Bot added the Stale label Jun 11, 2026
@github-actions github-actions Bot removed the Stale label Jun 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR was marked stale due to lack of activity.

@github-actions github-actions Bot added the Stale label Jun 19, 2026
@github-actions github-actions Bot removed the Stale label Jun 23, 2026
@jefchien
jefchien merged commit 154a39f into main Jun 26, 2026
402 of 406 checks passed
@jefchien
jefchien deleted the retain-envconfig branch June 26, 2026 19:44
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready for testing Indicates this PR is ready for integration tests to run

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants