Terraform staging environment
I was looking for a fairly simple way of handling different deployment environments with Terraform. Here are some of my notes on variables, workspaces, environments and variable overrides.
- Use workspaces to separate state files. This is important when using variable overrides to e.g. go from
testingby changing namespaces. Without a change of workspace, Terraform will think you want to destroy
staging, then set up `testing, instead of having them side by side.
- All files that ends with
.tfwill be loaded and appended onto each other.
- Resources with the same name will raise validation errors.
terraform.tfvarsand any other files that ends with
.auto.tfvarswill be automatically loaded, overriding default variables.
.tfvarsfiles may be references when running commands, e.g.
terraform plan -var-file=staging.tfvars
Variable files containing overrides (
.tfvars) have a simple syntax:
gce_region = "europe-west1-c" namespace = "myapp-staging"