Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform variable precedence #255

Closed
matt-smith opened this issue Mar 8, 2019 · 5 comments
Closed

terraform variable precedence #255

matt-smith opened this issue Mar 8, 2019 · 5 comments
Labels
enhancement New feature or request

Comments

@matt-smith
Copy link

matt-smith commented Mar 8, 2019

When setting the same variable (for example "example_var") in both VarFiles and Vars terraformOptions, ordering is not honoured and therefore the variable precedence is incorrect. The value of the variable in the VarFile will always be applied despite the ordering inside of the test.

For example:

...
	terraformOptions := &terraform.Options{
		// The path to where our Terraform code is located
		TerraformDir: "../examples/terraform-basic-example",

		// Variables to pass to our Terraform code using -var-file options
		VarFiles: []string{"varfile.tfvars"},

		// Variables to pass to our Terraform code using -var options
		Vars: map[string]interface{}{
			"example_var": "test123",
		},

		// Disable colors in Terraform commands so its easier to parse stdout/stderr
		NoColor: true,
	}
...

and varfile.tfvars contained

...
example_var = "test456"
...

example_var would always be set to test456

looks like https://github.com/gruntwork-io/terratest/blob/master/modules/terraform/format.go#L14-L15 is causing this behaviour.

@brikis98
Copy link
Member

brikis98 commented Mar 8, 2019

Are you expecting -var-file to go first always?

The docs say:

Any -var and -var-file options on the command line, in the order they are provided.

"The order they are provided" doesn't really make much sense in a terraform.Options struct. There's no way in Go to know what order you provided those variables.

@matt-smith
Copy link
Author

Hey @brikis98, thanks for your response and explanation.

I think it's more likely that a user would want one or two variables passed in as -var's to take precedence over a larger -var-file perhaps. What do you think?

I've opened a small PR PR-256 with my changes.

@brikis98 brikis98 added enhancement New feature or request help wanted labels Mar 18, 2019
@brikis98
Copy link
Member

Having -var take precedence over -var-file probably makes sense. That said, in most use cases, we only use one of -var or -var-file, not both, as keeping things in one place makes it easier to reason about the code.

@sebastianczech
Copy link

sebastianczech commented Jan 5, 2023

As far as I was checking, #1217 resolves that issue. If you define SetVarsAfterVarFiles: true, then values passed in Vars take precedence over values from VarFiles e.g.:

terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
	TerraformDir: ".",
	VarFiles:     []string{"example.tfvars"},
	Vars: map[string]interface{}{
		"example": "value_passed_from_test",
	},
	SetVarsAfterVarFiles: true,
})

@matt-smith
Copy link
Author

perfect thanks @sebastianczech , I'll close this issue in that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants