Configure the variable in question with a default map, defining the key as your expected workspace value.
variable "pet_prefix" {
type = string
description = "Prefix for random pet generator"
default = {
test = "test-pet"
prod = "prod-pet"
}
}
Since you can no longer use terraform.workspace
for meaningful input, you can supply your own custom variable that will denote which workspace has been chosen. In this example, that variable is defined as variable "tf_workspace" {...}
. To supply the variable, you can simply add it into your workspace variables in Terraform Cloud/Terraform Enterprise like so.
- Select workspace in question.
- Navigate to
Variables
. - Add / Edit variables under
Terraform Variables
section.- Make the key whatever you are using to distinguish the workspace on, e.g
tf_workspace
. - Make the value the workspace, e.g,
test
.
- Make the key whatever you are using to distinguish the workspace on, e.g
In order to select the proper default variable, you can use a generic map lookup using the built in syntax map[key]
.
resource "random_pet" "this" {
keepers = {
ts = timestamp()
}
prefix = var.pet_prefix[var.tf_workspace] # use variable
}
These are not all inclusive.
Pros | Cons |
---|---|
Fairly easy to understand/use. | Can override defaults which could break the map lookup. |
Can version and review changes. | Easily fat finger and mess up variables and/or edit wrong environment variables. |
Empowers single file structure; no folder nesting. | |
Keeps variable validation. | |
Keeps variable type checking. |