Tfvars per workspace - one configuration

Hi folks, I’m not sure if this is possible but this what I’m trying to do:

I have have one configuration that can be used multiple times with different inputs (tfvars files).

I would have no problem normally using the CLI by just specifying the -var-file flag, but switching to TFC workspaces (VCS integration), I can’t do that (or I don’t know how). How do I specify a specific tfvars file for a workspace.

For example, dev.tfvars should be loaded in my workspace-dev, but there’s no way to do that. The only thing I can do is setup an *.auto.tfvars, but that’ll load for all workspaces.

My example repo would like this:
project/

  • main.tf
  • dev.tfvars
  • stage.tfvars
  • prod.tfvars

I would like to load the respective tfvars file to the appropriate workspace.

Is this possible?

Hello, welcome to the forum!

Using Terraform Cloud’s VCS integration, var files via the command line are largely replaced by Terraform Cloud Workspace Variables [docs], which are API-driven variable values (both environment and Terraform input variables) persisted for that workspace. You can use different tfvar files on a single configuration via CLI (using -var-file as normal), but for VCS you’ll need to move those values in to TFC (or duplicate your configurations).

Thanks for the quick response Chris. So let me get this right, in the scenario my team needs to setup a new project/workspace, that workflow looks:

  1. create 3 different workspaces (one for each env)
  2. connect vcs to all 3
  3. create 3 variable sets for each “env” and associate it with the corresponding workspace.

Is that correct?

And the only way to automate the above is to create another workspace, that bootstraps these other workspaces, but now I’ve just put the variable values in this bootstrap workspace TF code as tfe_variables that I then associate with the appropriate workspace?

The above sounds convoluted. Am I grokking this correctly?

I guess the question is then, what is the best way to automate the creation of the workspace and where should that workspace bootstrap code live. Is this now meta TF I need to keep in my projects TF?

Thanks

That is the gist, essentially, yes. One slight alteration might be that TFC workspaces have their own set of workspace variables that are somewhat akin to an environment-specific tfvars file and you might manage those in your bootstrapping, rather than separate variable sets per-workspace, which are intended to be groups of variable values shared across multiple workspaces.

I guess the question is then, what is the best way to automate the creation of the workspace and where should that workspace bootstrap code live. Is this now meta TF I need to keep in my projects TF?

With the API you can automate the creation of these workspaces in any sort of way you like, but yes, it’s extremely common for TFC users to use a ‘boostrap’ or ‘meta’ workspace and the TFC/E Terraform provider to manage TFC workspaces with Terraform itself.

Got it. Thanks, @chrisarcand .

This article solve my issue.
Maybe is a solution also for you.

K.

This was exactly it. Thank you. I don’t know why or how I overlooked this…