CD4: Terraform for teams

Lesson 3 of 5 · reading 15–25 minutes; practice and project time is on the module page

Environments: dev, staging and production

🎯You'll be able to

Run the same configuration as several environments with separate state, different sizes and names that never collide, and move changes through them in order.

🏠The everyday picture

A theatre company rehearses in a hall, does a dress rehearsal on the real stage, then performs for the audience. It's the same play each time; the audience is what changes. Mistakes are cheapest in the rehearsal hall.

📘What it really is

📝A worked example

Input: RouteRani's acceptance tests run a second environment next to your own. Look at tests/lib.js: it sets TF_VAR_env=staging and different ports (TF_VAR_http_port=18080 and so on), and runs Terraform in a copied folder with its own state.

Expected output: while npm run test:infra runs, docker ps shows routerani-staging-* containers alongside your routerani-dev-* ones, on port 18080 instead of 8080. Your dev environment keeps running untouched, and the test removes staging when it finishes.

🌍Where it's used

📝Case scenario

A school-management company used Terraform workspaces for dev and prod in one cloud account. An engineer testing a clean-up script ran terraform destroy believing they were in the dev workspace; they were in prod, which was selected from the previous day. Students couldn't see timetables for two hours. They moved to a folder per environment, a separate cloud account for prod, and prod applies only from the pipeline.

🖱️Try it

Apply the finished kit twice: once normally, and once from a copy of the infra/ folder with TF_VAR_env=prod and TF_VAR_http_port=28080 set (and TF_VAR_monitoring=false). Destroy the copy afterwards. Success: two environments run side by side with different names, ports, passwords and state.

⚠️Common mistakes

🛠️For your project

Every RouteRani test runs in a staging environment next to your dev one, so names must include var.env.

✅Quick check

Why put each environment's state separately?

Show the answer

So a change or mistake in one can't plan or apply changes to another.

What should differ between staging and production?

Show the answer

Size, counts and names, deliberately; not the shape of the system.

← PreviousModules: reuse without copy-paste
ProLearners Assist