cloudposse-archives / terraform-tfe-cloud-infrastructure-automation

Terraform Enterprise/Cloud Infrastructure Automation
Apache License 2.0
15 stars 8 forks source link

Support for Terraform Cloud Workspace prefix #7

Open six8 opened 2 years ago

six8 commented 2 years ago

Have a question? Please checkout our Slack Community or visit our Slack Archive.

Slack Community

Describe the Feature

To have terraform cloud automatically find workspaces to use with terraform workspace on the CLI, you configure it with a prefix:

terraform {
  backend "remote" {
    hostname = "app.terraform.io"
    organization = "company"

    workspaces {
      prefix = "my-app-"
    }
  }
}

See https://www.terraform.io/docs/language/settings/backends/remote.html#workspaces

terraform-tfe-cloud-infrastructure-automation puts the environment as a prefix. For example ue2. In order for terraform to find the workspaces by prefix, the environment would have to be used:

terraform {
  backend "remote" {
    hostname = "app.terraform.io"
    organization = "company"

    workspaces {
      prefix = "ue2-"
    }
  }
}

Project name as a prefix seems like a more expected prefix to support this behavior.

Expected Behavior

Workspace pattern is {project}-{stage}-{env} instead of {env}-{stage}-{project}. Essentially a reversed order -- least specific to most specific.

Use Case

To be able to use terraform workspace select to select a workspace by prefix.

Describe Ideal Solution

That the template used to set the workspace name is configurable.

Alternatives Considered

Using the env (ex ue2-) as a prefix. This doesn't work well because multiple, unrelated projects are on the same environment.

six8 commented 2 years ago

The docs say:

Our convention is to name files by $env-$stage.yaml (e.g. ue2-testing.yaml), so for example an $env could be ue2 (for us-east-2) and the $stage might be testing. Workspace names are derived from the $env-$stage-$project, which looks like ue2-testing-eks.

But it looks like the workspace name is $config_file_name-$project. This means I can name the file $project-$env-$stage instead of $env-$stage and get the desired effect of a project prefix.