Set up workspace name as a variable

We are using terraform cloud and we got the following terraform code:

backend "remote" {
hostname = "app.terraform.io"
organization = "myorganization"
  workspaces {
    name = "myorganization-test-global"
     }
}

We would like to manage the workspace name as a variable.
Is TF_CLI_ARGS_init variable a way to solve our problem?
I have some trouble finding the correct way to correctly assign the value I need to TF_CLI_ARGS_init.
If TF_CLI_ARGS_init is not the correct solution, can someone suggest us a different approach?

Regards
Gio

Hi @desesseintes,

I was the person who responded to you over in GitHub and suggested you start a topic here. For completeness I just wanted to elaborate on my idea I shared over there, although others in this forum who have more Terraform Cloud knowledge might have other ideas to suggest.

My idea was to create a file such as backend.hcl containing the following content:

workspaces {
  name = "myorganization-test-global"
}

…and then run terraform init -backend-config=backend.hcl.

Of course, with a hard-coded backend.hcl file that wouldn’t be any different than what you currently have, but the complete solution would then be to have a script dynamically generate that file before running terraform init, where it can write in whichever workspace name is appropriate.

If you will be frequently changing workspace name in the same working directory then you may also need to use the -reconfigure option to terraform init, to tell Terraform not to offer to migrate the state from one workspace to another and instead to treat the change strictly as a reconfiguration.

1 Like