Thuta Learning
ProjectsDevOpsintermediate

Project — Multi-Environment Setup

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Get comfortable with Project — Multi-Environment Setup, no intimidation required
  • Be able to run terraform commands and HCL code yourself
  • Apply this concept immediately in a real project

Let's think about this for a second

In real projects, dev environments usually run small/cheap instances while production runs bigger, redundant ones — a common pattern is to use the module from Project 1 as a base template and split variable values into `dev.tfvars`, `staging.tfvars`, and `production.tfvars` per environment. You then run something like `terraform apply -var-file="production.tfvars"` to pick the value set for a given environment and apply it — since all three environments share the same module code, there's no need to duplicate anything.

Let's connect this to a real scenario

Set `instance_type = "t3.micro"` and `instance_count = 1` in `environments/dev.tfvars`, and `instance_type = "t3.large"` and `instance_count = 3` in `environments/production.tfvars` — then apply the same root config (the one calling the module) with a different `-var-file` flag per environment. You should also split the remote backend (from the Advanced chapter) by environment key (`key = "dev/terraform.tfstate"` vs `key = "prod/terraform.tfstate"`).

Let's look at an example

bash
# environments/dev.tfvars
# instance_type = "t3.micro"
# instance_count = 1

# environments/production.tfvars
# instance_type = "t3.large"
# instance_count = 3

terraform apply -var-file="environments/dev.tfvars"
terraform apply -var-file="environments/production.tfvars"
You should see
$ terraform apply -var-file="environments/dev.tfvars"
  + instance_type = "t3.micro"
  + instance_count = 1
Plan: 1 to add, 0 to change, 0 to destroy.

Try it in 5 minutes

Write your own `dev.tfvars` and `production.tfvars` files, then apply a `local_file` resource (with content driven by a variable) twice, each time with a different `-var-file` flag — confirm the two output files end up with different content.

A quick word of caution

Before applying a production `.tfvars` file, double-check the environment name on your terminal — mixing up dev and production is one of the most common root causes of production incidents.

Easy traps

  • Sharing a single state file/key across all environments — applying to dev could accidentally hit production resources
  • Forgetting the `-var-file` flag and applying with default values instead (this can end up applying to the wrong environment)

Now try it yourself

Write your own `dev.tfvars` and `production.tfvars` files, then apply a `local_file` resource (with content driven by a variable) twice, each time with a different `-var-file` flag — confirm the two output files end up with different content.

You'll know it worked when: $ terraform apply -var-file="environments/dev.tfvars" + instance_type = "t3.micro" + instance_count = 1 Plan: 1 to add, 0 to change, 0 to destroy.

Project — Multi-Environment Setup | Thuta Learning