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

Passing In Variables At Runtime #437

Open
AdminTurnedDevOps opened this issue Jan 30, 2024 · 1 comment
Open

Passing In Variables At Runtime #437

AdminTurnedDevOps opened this issue Jan 30, 2024 · 1 comment

Comments

@AdminTurnedDevOps
Copy link

Hey All,

Trying to figure out how to pass in variables at runtime. For example:

terraform apply --auto-approve -var name="value" -var location="value"

I set up a Variables interface to pass in the values (setting up the values of the variables by passing them in at runtime. Already have that covered).

		variables := map[string]interface{}{
			"name":                name,
			"resource_group_name": resourceGroupName,
			"location":            location,
			"node_count":          nodeCount,
			"k8s_version":         k8sVersion,
		}

		err = tf.Apply(context.Background())
@doyleish
Copy link

doyleish commented Dec 6, 2024

tfVarTempFile, _ := os.CreateTemp(os.TempDir(), "tempvars-*.tfvars.json")
defer os.Remove(tfVarTempFile.Name())
configBytes, _ := json.Marshal(variables)
tfVarTempFile.Write(configBytes)
tfVarTempFile.Close()
tf.Apply(context.TODO(), tfexec.VarFile(tfVarTempFile.Name()))

This is how I did it. Not amazing but not a ton of work either.
You could also do tfexec.Var("k=v") for all pairs in your map

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

No branches or pull requests

2 participants