Skip to content

Instantly share code, notes, and snippets.

@andyshinn
Last active December 29, 2023 20:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyshinn/505646686712b349291d63d2cbb6c519 to your computer and use it in GitHub Desktop.
Save andyshinn/505646686712b349291d63d2cbb6c519 to your computer and use it in GitHub Desktop.
Terraform variables per-workspace
$ terraform workspace new staging   
Created and switched to workspace "staging"!

You're now on a new, empty workspace. Workspaces isolate their state,
so if you run "terraform plan" Terraform will not see any existing state
for this configuration.
$ terraform apply

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

count_api = 4
count_www = 1
shared_all = shared
size_api = n1-standard-1
size_www = n1-standard-1
$ terraform workspace new production
Created and switched to workspace "production"!

You're now on a new, empty workspace. Workspaces isolate their state,
so if you run "terraform plan" Terraform will not see any existing state
for this configuration.
$ terraform apply                   

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

count_api = 18
count_www = 2
shared_all = shared
size_api = n1-standard-4
size_www = n1-standard-4
$ terraform workspace new testing   
Created and switched to workspace "testing"!

You're now on a new, empty workspace. Workspaces isolate their state,
so if you run "terraform plan" Terraform will not see any existing state
for this configuration.
$ terraform apply

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

count_api = 1
count_www = 1
shared_all = shared
size_api = n1-standard-1
size_www = n1-standard-1
output "count_api" {
value = local.workspace["instance_count_api"]
}
output "count_www" {
value = local.workspace["instance_count_www"]
}
output "size_api" {
value = local.workspace["instance_size_api"]
}
output "size_www" {
value = local.workspace["instance_size_www"]
}
output "shared_all" {
value = var.shared_all
}
locals {
env = {
defaults = {
instance_count_api = 1
instance_count_www = 1
instance_size_api = "n1-standard-1"
instance_size_www = "n1-standard-1"
}
production = {
instance_count_api = 18
instance_count_www = 2
instance_size_api = "n1-standard-4"
instance_size_www = "n1-standard-4"
}
staging = {
instance_count_api = 4
instance_count_www = 1
}
}
workspace = merge(local.env["defaults"], contains(list(local.env), terraform.workspace) ? local.env[terraform.workspace] : local.env["defaults"])
}
variable "shared_all" {
description = "This variable is shared by all workspaces"
default = "shared"
}
@ivanpetrushev
Copy link

Line 23 modified for recent versions of Terraform:

workspace = merge(
    local.env["defaults"], 
    contains(keys(local.env), terraform.workspace) ? 
      local.env[terraform.workspace] : 
      local.env["defaults"]
  )

@randuhmm
Copy link

@ivanpetrushev - this could be simplified so that the final map is empty if there is no match for the current workspace.

workspace = merge(
    local.env["defaults"], 
    contains(keys(local.env), terraform.workspace) ? 
      local.env[terraform.workspace] : 
      tomap({})
  )

Basically, there is no need to merge local.env["defaults"] with itself, an empty map will do just fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment