Skip to content

Instantly share code, notes, and snippets.

@RHeynsZa
Forked from andyshinn/README.md
Created May 3, 2024 11:21
Show Gist options
  • Save RHeynsZa/efa2bac950bc0be153eee0e5dc175914 to your computer and use it in GitHub Desktop.
Save RHeynsZa/efa2bac950bc0be153eee0e5dc175914 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"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment