Skip to content

Conversation

@emjacobs
Copy link

@emjacobs emjacobs commented May 5, 2025

Child function for GetEnvVars that runs terraform output and adds each output to the list of environment variables, has another child function for formatting the json returned by the terraform output command

…h output to the list of environment variables, has another child function for formatting the json returned by the terraform output command
// Each output is prefixed with 'TF_VAR_' to make it available as a Terraform variable.
// Returns a map of environment variable names to their formatted values, or an error if the command fails.
func (e *TerraformEnvPrinter) getTerraformOutputs(projectPath string) (map[string]string, error) {
cmd := e.shims.Command("terraform", "-chdir="+projectPath, "output", "-json")
Copy link
Contributor

@rmvangun rmvangun May 5, 2025

Choose a reason for hiding this comment

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

For command/exec specifically, the shell package is used when executing commands. This approach abstracts execution so that in the future, we could execute commands against other types of shells--docker exec, SSH, etc.

You can find examples of this elsewhere, but it will end up being something like shell.ExecSilent("terraform", "-chdir=...")

return nil, fmt.Errorf("failed to run terraform output: %w", err)
}
var outputs TerraformOutputs
if err := json.Unmarshal(outputJSON, &outputs); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

This would be shimmed for test coverage

@rmvangun
Copy link
Contributor

rmvangun commented May 5, 2025

Very close!

Other than a couple small things, the key piece is that, if you're in a terraform project folder, the TF_VAR_* that should be injected would be from the previous Terraform modules. In the blueprint.yaml, that would mean, for something like:

terraform:
  - path: projectA
  - path: projectB

If an output.tf existed in the first folder, those outputs would be available on the second. So, you might create an example project, like so:

terraform/
├─ projectA/
│  ├─ main.tf
│  ├─ variables.tf
│  └─ output.tf
└─ projectB/
   ├─ main.tf
   ├─ variables.tf
   └─ output.tf

if projectA/output.tf is:

output "some_output" {
  value = some_resource.this.some_value
}

Then when you are in the terraform/projectB folder, running windsor env would include an output like:

...
export TF_VAR_some_output=some-value
...

@rmvangun rmvangun force-pushed the main branch 2 times, most recently from d52bbb4 to 42a726f Compare May 8, 2025 16:26
@rmvangun
Copy link
Contributor

Implemented in #1478

@rmvangun rmvangun closed this Jul 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants