Clone existing workspace on app.terraform.io

Hello,

I have an existing workspace setup on app.terraform.io with VCS integration (GitLab) and some terraform variables. I want to create a new workspace out of it but this new workspace runs from a different branch. Can we clone existing workspace to avoid copying all the variables and other settings again to save time.

1 Like

Let me get this straight:

You have an existing workspace with state, VCS settings (branch and folder), and Variables, right?

Do you want to clone the exact state, or just the Terraform Variables and VCS settings?

Could you explain in more detail?

Hello @v61,

I don’t want to copy state but only VCS settings and Terraform variables. After cloning the workspace, I will only change the branch to run terraform against. For now, my Terraform variables may remain same but are guaranteed to change.

1 Like

If you want to copy over Terraform Variables, I know of a tool for this.

It’s probably not relevant to your case, but I have used and recommended tfh to clients, in the past, for copying over Variables and Values from one Terraform Enterprise workspace to another:

However, it does not copy the values for Sensitive Variables, because these are stored in Vault with a write-only policy.

I guess you could even write your own scripts for this, because tfh pretty much just wraps the Terraform Enterprise API.

You can’t copy or clone workspaces. The tf-helper tool is nice and you can also use the TFE provider to help manage multiple workspaces. https://www.terraform.io/docs/providers/tfe/index.html

1 Like

Cloning a existing Terraform Workspace is a very handy feature. Terrafrom should consider implementing this at the earliest.

I’m wondering why such a simple feature is not implemented in the enterprise edition after charging such a huge license fee.

7 Likes

Dear TF Cloud devs, please consider adding this function of cloning of workspaces.

It will greatly simplify creating new workspaces that are very similar to previous. So we won’t have to again complete any vars, vsc config etc.

I’ve used this function in the past with Buildkite, you can look there for inspiration Defining Your Pipeline Steps | Buildkite Documentation

If this is something you do frequent, perhaps it’s worth checking out and converting to variable sets? (see link).

We here use terraform to create all workspaces :slight_smile: (That was way prior they had the variable sets). We have one bootstrap workspace called terraformio with all variables we ever need defined there. Then we have code like this:

locals {
  workspaces = {
    test = {
      "name"               = "my-test"
      "work_dir"           = "my-terraform"
      "branch"             = "feature/test"
      "vcs_id"             = "test/myterraform"
      "ingress_submodules" = false
      "terraform_version"  = "1.0.11"
      variables = [
        {
          # name of the var in target namespace
          name         = "MY_VAR"
          # value of this var, taken from the value of the var defined in this bootsrap workspace
          value        = var.VAR_DEFINED_IN_THIS_WORKSPACE
          is_sensitive = true
        },
      ]
    }
  }
}

Then we loop over this stuff and :bangbang: all is there.

We want to move to variable sets but currently I see no support for these in tfe_ provider so we are delaying.

Var set link: Manage Variable Sets in Terraform Cloud | Terraform - HashiCorp Learn

Here is the npm package for this. just do
npm i -g terraform-workspace-clone

and it will ask for a few details like url, original ws name, clone name, token and that’s it.
NPM package: terraform-workspace-clone - npm

You can also do npx terraform-workspace-clone for on-demand run.