Terraform Course
From zero to production: HCL, providers, remote state, modules, multi-environment and real-world patterns — with concrete, easy-to-follow examples.
Foundations
0/6 visitedWhat Terraform is, the HCL block syntax, the init/plan/apply workflow, and your first resource.
- Step 1
Terraform 1: What is IaC and why Terraform?
Infrastructure as Code (IaC) means describing infrastructure in declarative files you version, revie…
Start lesson - Step 2
Terraform 2: Installing Terraform + the CLI
Terraform is a single static binary you put on your PATH; `terraform -version` confirms it works. Pa…
Start lesson - Step 3
Terraform 3: HCL basics — blocks, arguments, expressions
HCL is built from blocks (a type, labels, and a body of arguments) plus expressions that reference o…
Start lesson - Step 4
Terraform 4: HCL types — primitives and collections
HCL has primitive types (string, number, bool) and collection types (list, set, map, object). Model …
Start lesson - Step 5
Terraform 5: The core workflow — init, plan, apply, destroy
The everyday Terraform loop is init then plan then apply, with destroy to tear it down. Model the fo…
Start lesson - Step 6
Terraform 6: Reading a plan diff (+ / ~ / -)
A plan diff uses symbols: + creates, ~ updates in place, - destroys, and -/+ replaces. Given a list …
Start lesson
Providers & Resources
0/5 visitedConfiguring providers, resource dependencies, implicit vs explicit ordering, and the resource graph.
- Step 7
Terraform 7: The terraform{} block & provider version constraints
Every Terraform configuration starts with a `terraform{}` block that pins Terraform itself and decla…
Start lesson - Step 8
Terraform 8: Configuring the azurerm provider (features {})
Declaring a provider in `required_providers` only downloads it — you still need a `provider` block t…
Start lesson - Step 9
Terraform 9: Your first resource — azurerm_resource_group
A resource group is the natural first Azure resource: it is the container everything else lives in. …
Start lesson - Step 10
Terraform 10: Arguments vs attributes & cross-resource references
Inside a resource you *set* arguments (inputs) and *read* attributes (outputs Azure computes). Wirin…
Start lesson - Step 11
Terraform 11: Implicit dependencies & depends_on
Terraform builds a dependency graph from the references between resources and creates them in the ri…
Start lesson
Variables, Outputs & Locals
0/6 visitedInput variables, validation, sensitive values, outputs, and locals for derived expressions.
- Step 12
Terraform 12: Input variables (type, default, description, validation)
Input variables are the parameters of a Terraform module — they let one configuration serve dev, uat…
Start lesson - Step 13
Terraform 13: Passing values — tfvars, -var, env vars & precedence
Declaring a variable is half the story — you also have to give it a value. Terraform accepts values …
Start lesson - Step 14
Terraform 14: Output values
Outputs export values from your configuration — a resource group id, a Key Vault URI, a connection s…
Start lesson - Step 15
Terraform 15: Locals & string interpolation
Locals are named expressions computed once and reused — perfect for a naming convention or a derived…
Start lesson - Step 16
Terraform 16: Data sources — reading existing infrastructure
A `resource` block *creates and owns* infrastructure; a `data` block only *reads* infrastructure tha…
Start lesson - Step 17
Terraform 17: for_each over a map with data + key vault secrets
Now combine everything: a `locals` map describes several services, `for_each` stamps out one resourc…
Start lesson
Expressions & Meta-arguments
0/5 visitedcount, for_each, dynamic blocks, conditionals, and functions for real-world expressiveness.
- Step 18
Terraform 18: Built-in functions & expressions
HCL ships a standard library of pure functions — `join`, `lookup`, `merge`, `format`, `coalesce` — t…
Start lesson - Step 19
Terraform 19: The count meta-argument & count.index
`count` on a resource block creates N copies indexed `0..N-1`; inside the block `count.index` gives …
Start lesson - Step 20
Terraform 20: for_each over a map/set with each.key / each.value
`for_each` creates one instance per entry of a **map** or **set of strings**, keyed by a stable iden…
Start lesson - Step 21
Terraform 21: Conditionals (ternary) & dynamic blocks
The ternary `condition ? a : b` chooses a *value*; a `dynamic` block generates *nested blocks* from …
Start lesson - Step 22
Terraform 22: The lifecycle block
The `lifecycle` block tunes how Terraform *replaces* and *updates* a resource: `create_before_destro…
Start lesson
State
0/5 visitedHow state works, remote backends, state locking, importing existing infrastructure, and drift.
- Step 23
Terraform 23: What Terraform state is and why it exists
Terraform records what it built in a state file (`terraform.tfstate`) — a JSON map from each resourc…
Start lesson - Step 24
Terraform 24: Local vs remote state for team collaboration
By default state is a single file on your laptop — fine solo, disastrous for a team. A remote backen…
Start lesson - Step 25
Terraform 25: Remote state with the azurerm backend
The `azurerm` backend keeps state as a blob in a Storage Account container. You declare it in `backe…
Start lesson - Step 26
Terraform 26: State locking, drift, and refresh
A remote backend locks state during writes so two applies can't corrupt it, and Terraform refreshes …
Start lesson - Step 27
Terraform 27: Refactoring safely with state mv, import, and moved
Renaming a resource in `.tf` normally makes Terraform destroy the old and create the new — rarely wh…
Start lesson
Structuring & Environments
0/5 visitedOrganizing a codebase, workspaces vs directories, and multi-environment (dev/staging/prod) patterns.
- Step 28
Terraform 28: 1: Organizing your .tf files
Terraform loads every `.tf` file in a directory and merges them, so file names are for humans, not t…
Start lesson - Step 29
Terraform 29: 2: Multi-environment layout (dev / uat / prod)
Real projects run the same infrastructure in several isolated environments. A common pattern gives e…
Start lesson - Step 30
Terraform 30: 3: A naming + tagging convention (env-service-type)
Consistent names and tags make a cloud account searchable, billable, and safe to clean up. Adopt an …
Start lesson - Step 31
Terraform 31: 4: Workspaces vs directories — the trade-offs
Terraform offers two ways to run one config across many environments: CLI workspaces (one directory,…
Start lesson - Step 32
Terraform 32: 5: Keeping environments DRY with shared modules
Directory-per-environment risks copy-paste drift. The fix: put the real resources in one shared modu…
Start lesson
Modules
0/6 visitedWriting reusable modules, module composition, versioning, and the public Terraform Registry.
- Step 33
Terraform 33: Terraform Modules 1: Using a module
A module is a reusable folder of `.tf` files you call with a `module` block: you point `source` at i…
Start lesson - Step 34
Terraform 34: Terraform Modules 2: Writing your own module
A module is just a folder: resources in `main.tf`, `variable` blocks for its inputs, `output` blocks…
Start lesson - Step 35
Terraform 35: Terraform Modules 3: Sources and version pinning
`source` can be a local path, a registry module, or a git URL — and for git/registry you should alwa…
Start lesson - Step 36
Terraform 36: Terraform Modules 4: Deploying many with for_each
A `module` block accepts `for_each`, so one block can stamp out N copies from a map — one function a…
Start lesson - Step 37
Terraform 37: Terraform Modules 5: The shared common-modules repo
Mature teams keep one `terraform-common` repo of blessed, versioned modules — tagging, monitoring, k…
Start lesson - Step 38
Terraform 38: Terraform Modules 6: Composing modules into a service
A real service is several modules wired together: a network module produces a subnet, a security mod…
Start lesson
Real-world, Multi-provider & Capstone
0/6 visitedMulti-provider stacks, CI/CD for Terraform, policy-as-code, and a full production-shaped capstone module.
- Step 39
Terraform 39: Key Vault + secrets done right
Terraform should reference an existing Key Vault via a data source and write secrets with `for_each`…
Start lesson - Step 40
Terraform 40: Service Bus wiring with for_each
The real-world pattern: a `locals` map of subscribers/publishers drives a data-source lookup of each…
Start lesson - Step 41
Terraform 41: A reusable module for names + tags
Before the capstone, factor the boring-but-critical parts — standardized resource names and mandator…
Start lesson - Step 42
Terraform 42: A second provider + provider aliases
Real stacks span more than one provider — here `azuread` (identity) alongside `azurerm` — and often …
Start lesson - Step 43
Terraform 43: CI/CD — plan in PR, gated apply
Terraform belongs in a pipeline: remote state with locking, `fmt`/`validate`/`plan` on every PR, and…
Start lesson - Step 44
Terraform 44: Capstone — a multi-env service module
Compose everything: one `service` module builds an RG + Key Vault + Service Bus namespace/topics + s…
Start lesson
Work it out and check
Calculate or infer the answer before checking. Use hints if stuck, then solve another case to apply what you learned. Practice lasts this visit and does not mark lessons complete.
Read the plan before releasing
For these completed plans with -detailed-exitcode, enter the exit code. No commands run here; a changed plan still needs review and approval.
Case 1/2 · Simulated practice, no infrastructure execution
A: No changes. Your infrastructure matches the configuration.
B: Plan: 1 to add, 0 to change, 0 to destroy.Repair the example
Edit the file, check its contract, and explain the repair. No commands run and copying does not award progress.
Protect a Terraform output
Learning goal: Separate output redaction from protection of stored secrets.
Repair this .tf.json fragment: keep output demo_token and its fake value, and mark it sensitive using a boolean. Keep only the supplied keys. The flag redacts ordinary CLI display, not state storage.
Predict first: Does changing "true" to true encrypt the output in state? What does it actually change?
Show reference solution
{
"output": {
"demo_token": {"value": "fake-training-token", "sensitive": true}
}
}In this JSON contract, sensitive is a boolean, not a string. It marks the output for redaction in ordinary human-readable output. It is not encryption or access control: output values remain in state, and querying a named output or using -raw or -json can disclose them.
How to verify beyond this exercise
Use only the supplied fake token in an isolated, provider-free scratch directory. terraform validate checks configuration, not confidentiality. If you apply this output-only example locally, compare ordinary terraform output with terraform output -raw demo_token, then protect the generated state. Never substitute a real credential for this demonstration.
Terraform output values