Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Input Variables per Local Terraform Workspace
workspace_variables = {
dev = {
instance_count = 2
ami_id = "ami-abc123"
instance_type = "t2.micro"
}
test = {
instance_count = 3
ami_id = "ami-a1b2c3"
instance_type = "t2.medium"
}
prod = {
instance_count = 5
ami_id = "ami-1a2b3c"
instance_type = "t2.large"
}
}
resource "aws_instance" "this" {
count = local.instance_count
ami = local.ami_id
instance_type = local.instance_type
tags = {
Name = "project-xyz-${terraform.workspace}-${count.index}"
}
}
variable "instance_count" {
type = number
default = null
}
variable "ami_id" {
type = string
default = null
}
variable "instance_type" {
type = string
default = null
}
variable "env" {
type = string
default = null
}
variable "workspace_variables" {
type = map(object({
instance_count = number
ami_id = string
instance_type = string
}))
default = {}
}
locals {
default_vars = lookup(var.workspace_variables, terraform.workspace, {
instance_count = 2
ami_id = "ami-abc123"
instance_type = "t2.micro"
})
instance_count = var.instance_count != null ? var.instance_count : local.default_vars["instance_count"]
ami_id = var.ami_id != null ? var.ami_id : local.default_vars["ami_id"]
instance_type = var.instance_type != null ? var.instance_type : local.default_vars["instance_type"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment