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

2 Likes

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.

4 Likes

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

1 Like

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.

2 Likes

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

1 Like

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

Guys, that thread is a life saver ! :smiley: ā€¦I just lost so much time to figure out how to manage the variables in TF cloud for different environments etc !

The documentations does not HELP in regards to that ! Some updates there are needed !

Thank you !

2 Likes

This is good stuff @apparentlymart. Thanks I am quoting you in another thread.

@apparentlymart Iā€™ve just stumbled upon this from https://developer.hashicorp.com/terraform/cloud-docs/workspaces/variables#11-terraform-tfvars-variable-file


When this has changed so that TF Cloud ā€œgave upā€ overwriting terraform.tfvars passed over from TF caller end? :thinking: 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

Hi @yermulnik,

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.