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)
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).