diff --git a/internal/exec/shell_utils.go b/internal/exec/shell_utils.go index fbf14a6f9..58d4b093b 100644 --- a/internal/exec/shell_utils.go +++ b/internal/exec/shell_utils.go @@ -185,13 +185,24 @@ func execTerraformShellCommand( } }() - // Set the Terraform environment variables to reference the var file - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_plan=-var-file=%s", varFile)) - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_apply=-var-file=%s", varFile)) - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_refresh=-var-file=%s", varFile)) - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_import=-var-file=%s", varFile)) - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_destroy=-var-file=%s", varFile)) - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_console=-var-file=%s", varFile)) + // Define the Terraform commands that may use var-file configuration + tfCommands := []string{"plan", "apply", "refresh", "import", "destroy", "console"} + + // Check for existing var-file arguments in TF_CLI environment variables + for _, cmd := range tfCommands { + 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) + componentEnvList = append(componentEnvList, fmt.Sprintf("%s=%s", envVar, newValue)) + } else { + componentEnvList = append(componentEnvList, fmt.Sprintf("%s=-var-file=%s", envVar, varFile)) + } + } // Set environment variables to indicate the details of the Atmos shell configuration componentEnvList = append(componentEnvList, fmt.Sprintf("ATMOS_STACK=%s", stack)) @@ -235,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 diff --git a/internal/exec/terraform.go b/internal/exec/terraform.go index fcf65bdb3..768d0346e 100644 --- a/internal/exec/terraform.go +++ b/internal/exec/terraform.go @@ -231,6 +231,14 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error { } } + // Check for any Terraform environment variables that might conflict with Atmos + 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 or be overwritten by Atmos", varName)) + } + } + // Set `TF_IN_AUTOMATION` ENV var to `true` to suppress verbose instructions after terraform commands // https://developer.hashicorp.com/terraform/cli/config/environment-variables#tf_in_automation info.ComponentEnvList = append(info.ComponentEnvList, "TF_IN_AUTOMATION=true")