Hi. We have a Terraform project that’s tracked on Github and linked with a TFE workspace. It contains a variables.tf file.
We want to maintain that single
variables.tf with their variables descriptions and defaults, however we find ourselves copy pasting everything inside TFE variables section.
Is there any way to let TFE pick up those variable definitions?
As far as I know *.tfvars files are used to provide values (and usually git ignored)
variables blocks in your configuration are declarations of variables, stating what’s expected by the module, while
.tfvars files and the stored variables in a Terraform Cloud workspaces are
definitions of variables, which decide on the actual values to assign.
If you don’t actually need your values to vary by workspace then you can just assign
default values to all of the variable declarations and leave them totally unset in Terraform Cloud or
.tfvars files, but in that case it might be better to use Local Values since it would seem that those are not values intended for outside configuration.
Stored variables associated with a Terraform Enterprise workspace are a way to have Terraform Enterprise automatically generate a
terraform.tfvars file for you, using values configured in a systematic way, possibly across many workspaces using Variable Sets.
However, if you don’t actually need that sort of systematic or centralized administration of input variables, there is no reason you can’t put your variable values in
.auto.tfvars files in the working directory, which will work just the same in Terraform Enterprise as they do in Terraform CLI.
There is no rule that says all
.tfvars files must be excluded from Git; whether that’s appropriate will depend on what contents are in that file: whether they are sensitive or not, whether they are constant across many runs or should vary per-run. If you have non-sensitive variables which should stay constant across many runs, creating an
.auto.tfvars file containing their values is a perfectly normal and reasonable approach.
The only special rule about
.tfvars files for Terraform Cloud and Enterprise is that you can’t use a
terraform.tfvars file as you might in plain Terraform CLI, because as I mentioned above Terraform Cloud and Enterprise use that name for the auto-generated file based on the variable values stored in the workspace settings.