Skip to content

Commit

Permalink
Update handling of Terraform environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
milldr committed Jan 2, 2025
1 parent 0cbee45 commit 971be07
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions internal/exec/shell_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,11 @@ func execTerraformShellCommand(
envVar := fmt.Sprintf("TF_CLI_ARGS_%s", cmd)
existing := os.Getenv(envVar)
if existing != "" {
u.LogWarning(atmosConfig, fmt.Sprintf("Found Terraform environment variable '%s' which may conflict with or be overwritten by Atmos", envVar))
// Remove any surrounding quotes from existing value
existing = strings.Trim(existing, "\"")
// Create new value by combining existing and new var-file argument
newValue := fmt.Sprintf("%s -var-file=%s", existing, varFile)
newValue := fmt.Sprintf("\"%s -var-file=%s\"", existing, varFile)
componentEnvList = append(componentEnvList, fmt.Sprintf("%s=%s", envVar, newValue))
} else {
componentEnvList = append(componentEnvList, fmt.Sprintf("%s=-var-file=%s", envVar, varFile))
Expand Down Expand Up @@ -245,7 +246,7 @@ func execTerraformShellCommand(
pa := os.ProcAttr{
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
Dir: componentPath,
Env: append(os.Environ(), componentEnvList...),
Env: append(componentEnvList, os.Environ()...), // Give priority to Atmos defined env vars
}

// Start a new shell
Expand Down
2 changes: 1 addition & 1 deletion internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {
for _, envVar := range os.Environ() {
if strings.HasPrefix(envVar, "TF_") {
varName := strings.Split(envVar, "=")[0]
u.LogWarning(atmosConfig, fmt.Sprintf("Found Terraform environment variable '%s' which may conflict with Atmos", varName))
u.LogWarning(atmosConfig, fmt.Sprintf("Found Terraform environment variable '%s' which may conflict with or be overwritten by Atmos", varName))
}
}

Expand Down

0 comments on commit 971be07

Please sign in to comment.