Skip to content
Ameen Abozaid
MID-058

Infrastructure as Code

  • 6 Lessons
  • 3 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Terraform fundamentals
  • state
  • modules
  • reviewable changes

Learn first

3 h of practice (estimate)

Learning outcomes

  1. Describe infrastructure declaratively with Terraform and apply changes through plan and review.
  2. Manage remote state with locking and explain state drift.
  3. Structure configuration into reusable modules and environments.
  4. Run Terraform from CI with reviewable plans.

Lessons

  1. 01Why infrastructure as code
  2. 02Terraform basics
  3. 03State and drift
  4. 04Modules and environments
  5. 05Terraform in CI
  6. 06Demo: rebuilding the clicked infrastructure as code

Lab and exercises

Lab

MID-058-LAB1 · Shop infrastructure

Outcomes: 1–4 · Time: ~3 h Rebuild the MID-056 environment in Terraform: modules for network, database, and storage; separate dev and staging environments; remote state with locking; a pipeline that runs fmt, validate, and plan on pull requests and posts the plan. Acceptance: destroy and recreate the environment from code alone.

Exercises (assessment: plan review exercise)

IDOutcomeTask
MID-058-EX011Write a Terraform resource for a private object-storage bucket and run plan.
MID-058-EX021Review a given plan: identify the destructive change and propose a safer path.
MID-058-EX032Configure a remote backend with locking and explain what drift is with an example.
MID-058-EX043Refactor one root module into network and database modules with inputs and outputs.
MID-058-EX053Design the folder layout for dev, staging, and prod with separate state.
MID-058-EX064Write the pipeline stages for pull requests and for merge, including approval for prod.
Show solutions
  • EX01 Bucket resource with a private ACL or block-public-access setting; init, then plan shows one resource to add.
  • EX02 A resource with -/+ (forced replacement) on the database means data loss; change the argument that forces replacement, or use a migration or lifecycle { prevent_destroy = true }.
  • EX03 Backend block pointing to remote storage with a lock table or equivalent; drift: someone widens a security group in the console, and the next plan shows the difference.
  • EX04 Move resources into modules/network and modules/database, expose outputs such as private_subnet_ids, and wire them in the environment root.
  • EX05 modules/*, envs/dev, envs/staging, envs/prod, each with its own backend key.
  • EX06 PR: fmt, validate, security scan, plan posted as a comment. Merge: apply the saved plan; production requires approval.

Outcome 4 is also covered by the lab criterion (plans on PRs) and quiz Q09–Q10.

Quiz

Outcomes: O1 declarative IaC and plan · O2 state and drift · O3 modules and environments · O4 Terraform in CI.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1The main benefit of infrastructure as code is:O1
  2. 2terraform plan does:O1
  3. 3A resource shown with -/+ in a plan means:O1
  4. 4Why use remote state with locking?O2
  5. 5Drift is:O2
  6. 6State files may contain:O2
  7. 7A module is:O3
  8. 8Separate state per environment gives:O3
  9. 9In CI, the plan should be:O4
  10. 10Manual console changes should be:O4

Practical part

Plan review (MID-058-EX02) plus lab MID-058-LAB1. Pass: quiz at least 8/10, remote state with locking, plans posted on PRs, and a full destroy and recreate from code alone.

Common mistakes

Local state files; manual changes in the console; one giant root module.

Assessment

Plan review exercise.

Career relevance

Infrastructure governance (ARC-020).

Type to search levels, phases, modules and lessons.