terraformterraform-provider-azureterraform-remote-state

Use variable in Terraform remote backend


# Using a single workspace:
terraform {
  backend "remote" {
    hostname = "app.terraform.io"
    organization = "company"

    workspaces {
      name = "my-app-prod"
    }
  }
}

For Terraform remote backend, would there be a way to use variable to specify the organization / workspace name instead of the hardcoded values there?

The Terraform documentation didn't seem to mention anything related either.


Solution

  • The backend configuration documentation goes into this in some detail. The main point to note is this:

    Only one backend may be specified and the configuration may not contain interpolations. Terraform will validate this.

    If you want to make this easily configurable then you can use partial configuration for the static parts (eg the type of backend such as S3) and then provide config at run time interactively, via environment variables or via command line flags.

    I personally wrap Terraform actions in a small shell script that runs terraform init with command line flags that uses an appropriate S3 bucket (eg a different one for each project and AWS account) and makes sure the state file location matches the path to the directory I am working on.