I have multiple environment specific tfvars in a repo, need to pass specific file so it can use these variables, I dont find any way to do it from terraform cloud.
As these are envionment specific file, I dont have it named terraform.tfvars, rather its as dev.tfvars, prod.tfvars
When using Terraform Cloud, the workspace settings in Terraform cloud replace the use of CLI-provided variables files. For each Terraform Cloud workspace you can configure workspace variables.
If your goal is to use the same configuration multiple times with different variables to create multiple ācopiesā of the same infrastructure, the intended way to do that with Terraform Cloud is as follows:
Create one workspace for each of your environments that all share a common name prefix (e.g. networking-) but have different suffixes giving the environment name (like networking-stage, networking-prod, etc).
In your shared configuration, write a remote backend configuration block that specifies the naming prefix you chose in the previous step:
Run terraform init locally against that configuration to configure the backend. That should reach out to the Terraform Cloud API and verify that the given organization exists and that your credentials grant access to it.
Run terraform workspace list to see the local workspaces that correspond to the remote workspaces you created in the first step. If you use the names I gave as examples, youād see workspaces named stage and prod.
Return to the Terraform Cloud UI and configure the environment-specific variables for each of the networking- workspace using the variable management UI. (You can also manage these in Terraform using the tfe provider, but I wonāt get into the details of that here for brevity.)
In your local shell, run terraform workspace select stage to select the staging workspace. You can run terraform apply here to push your current configuration up to the remote networking-stage workspace and run Terraform using the variables you configured for that specific workspace.
Similarly, run terraform workspace select prod to select the production workspace. Again, run terraform apply to push up the configuration and run Terraform with the separate workspace-specific variables for production.
You can now switch between your environments using terraform workspace select and it will cause Terraform (via Terraform Cloud) to always use the set of variables and latest state snapshot associated with the selected workspace. You no longer need to manually coordinate using the correct .tfvars file for the state you are currently working with.
As you may have seen on the Variables documentation page I linked above, you can still use the .auto.tfvars and terraform.tfvars files that Terraform automatically reads, for any variable values you want to be consistent between your environments, but any per-environment values should be stored remotely in the workspace settings.
@apparentlymart I will be using (almost) same variable for all environment, however value would change depends on environment, example - Development can have 1 VM, but Prod might 10. Size of VM might be b4ms for Dev, but very high configuration for prod. How do I manage that without adding variable on Terraform cloud (and read from file)
You can then configure your Terraform Cloud workspaces only with that single environment variable value (set to either dev or prod) and keep all of the details inside the configuration.
I was able to use backend, and create multiple workspace on Terraform cloud.
One thing i am still seeing issue with is using terraform.tfvars
I have defined all the environment specific variable on Cloud, and left a variable value in terraform.tfvars which will be consistent everywhere (I am just testing for now).
Variable defination file - win-variables.tf
variable āwindows_admin_usernameā {
type = string
description = āā
}
Value is defined in terraform.tfvars
windows_admin_username = ālocaladminstratorā
Looking back at what I wrote before Iām afraid I can see I made an error: the terraform.tfvars file isnāt used in Terraform Cloud, because Terraform Cloud actually overwrites that file with the variables you configured in the web UI, in order to make those variable values available to the Terraform run.
If you rename that file to something ending in .auto.tfvars then it should work. For example, you could rename it to terraform.auto.tfvars. .auto.tfvars files take precedence over terraform.tfvars, which means that in the Terraform Cloud context values set in .auto.tfvars will override a value provided via the Variables web UI in Terraform Cloud (because Terraform Cloud writes those into terraform.tfvars).
When this has changed so that TF Cloud āgave upā overwriting terraform.tfvars passed over from TF caller end? Was it a change in Terraform version or a change in TF Cloud? if TF version, could you please refer to a version? If TF Cloud ā then since when this has changed? Thanks
I work on Terraform Community Edition rather than Terraform Cloud and so Iām not sure exactly whatās changed here, but the note you indicated was added in this pull request:
I notice that thereās a comment thread about the treatment of terraform.tfvars which seems to suggest that the behavior differs when your run occurs in a Terraform Cloud Agent. Some hidden context here is that Terraform Cloud has been transitioning to using HashiCorp-managed Terraform Cloud Agents for workspaces that donāt have their own external agent pool configured, and so I guess this change in behavior correlates with that migration.
If you are on a paid tier of Terraform Cloud then Iād suggest contacting HashiCorp Support if you need a more specific answer. Thanks!
@apparentlymart Thanks for the feedback. I was almost sure you know every single bit about TF and stuff around it =)
Thanks for the pointer to the that exact comment on the PR. I didnāt even think there might be this much useful info within resolved comments that I see collapsed (since they are resolved). Iād wish they put that info in the documentation so clarify diffs between regular TF, TFC and TFE.
Appreciate your assistance as usual! Thanks and Happy New Year.