How do i pass -var-file on Teffaform cloud

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

Hi @umashankar2,

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:

    terraform {
      backend "remote" {
        organization = "example"
        workspaces {
          prefix = "networking-"
        }
      }
    }
    
  • 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 use a single input variable to select values within your configuration:

variable "environment" {
  type = string
}

locals {
  per_environment_settings = tomap({
    dev = {
      vm_count = 1
    }
    prod = {
      vm_count = 10
    }
  })

  vm_count = local.per_environment_settings[var.environment]
}

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.

@apparentlymart Thats great. Thank you for your response.

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”

However, I still see issue when run the plan:

Does that mean its not reading variable from terraform.tfvars file?

Hi @umashankar2,

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

Thank you @apparentlymart, I relaized that later and renamed it as .auto.tfvars. That works.