Are Terraform variables loaded at runtime or during deployment?

99 Views Asked by At

I have a service which is hosted on AWS and configured through Terraform. It has a configuration file for each environment it's deployed in, for example: dev-myService-region1.tfvars. How can I tell if the variables from this file are loaded at runtime or during deployment? Thanks!

1

There are 1 best solutions below

0
On

I'm not sure what you mean by the distinction between "at runtime" and "during deployment", but I'll describe how Terraform treats input variables and hopefully you can answer your own question based on this information.

The values for all of the input variables declared in the root module are specified as Planning Options.

Although most planning options are configured exclusively by command line options, Terraform supports various other strategies for finding and loading input variable values in particular, including .tfvars files and Environment Variables.

Regardless of how exactly you specify them, they always end up stored as part of the plan. If you save a plan file to disk with terraform plan -out=tfplan then all of the values you specified will be saved as part of that file.

When you apply a plan, either from a saved file using terraform apply tfplan or when using terraform apply to create a plan and apply it together in a single run, Terraform always uses the input variable values that were specified when creating the plan. This helps ensure that the actions taken during the apply phase will match what was proposed during the planning phase, as long as nothing has changed outside of Terraform since the plan was created.