BuildBot

IaC Core

Workspaces & environments

Lesson 5 of 5

What you'll learn

  • Run one config across many environments without forking it
  • Understand workspaces versus separate state per environment
  • See promotion as applying the same config with a different variables map

The same config should build dev, staging, and prod — differing only in inputs (sizes, counts, domains). The dangerous anti-pattern is copy-pasting a config per environment, where they silently drift apart.

variable "environment" { type = string }

locals {
  sizing = {
    dev     = { count = 1, type = "t3.small" }
    staging = { count = 2, type = "t3.medium" }
    prod    = { count = 6, type = "t3.large" }
  }
  cfg = local.sizing[var.environment]
}

resource "aws_instance" "api" {
  count         = local.cfg.count
  instance_type = local.cfg.type
}

Workspaces vs. separate state

A workspace gives one config multiple independent state files (terraform workspace new prod). It's fine for lightweight cases, but for hard prod isolation many teams prefer separate state backends plus per-environment *.tfvars, so a fat-fingered command can't target the wrong environment.

terraform workspace select staging
terraform apply -var-file=staging.tfvars

Promotion is repetition with new inputs

Promoting dev to staging to prod means applying the same config with the next environment's variables. Because the config is identical, what reaches prod is exactly what you exercised in staging — only the inputs changed. Tools like Pulumi express the same idea in general-purpose languages (TypeScript, Go) instead of HCL, but the model — declarative resources, state, a dependency graph, environment-scoped inputs — is the same.

Promote the artifact, not a rewrite

Confidence comes from changing only inputs between environments. If config also changes on the way to prod, you're testing something you never ran in staging.

Render one config across environments

Run it. The same render function produces each environment by swapping its variables map.

Loading editor…
Knowledge check

What should change as you promote a deployment from dev to staging to prod?

Saved on this device. Sign in to sync your progress everywhere.