Skip to main content
DevOps

Terraform Tutorial with Examples.

Terraform tutorial for infrastructure as code: provision and manage cloud resources across AWS, Azure and GCP with HCL.

Architecture at a glance

Click any box to open the lesson that teaches it

Terraform architecture at a glance

config (.tf) → init → validate → plan → apply → provider → cloud, backed by state

Terraform architecture at a glance A .tf configuration of providers, resources, variables, outputs, locals, modules, count/for_each, lifecycle settings and data sources drives a workflow of init, validate, plan and apply; the provider plugin turns apply into real create, update and delete calls against a cloud API, the same mechanism multi-cloud patterns build on. State records what actually exists: plan diffs against it and apply writes back to it, a remote backend adds locking, workspaces give one config a state per environment, and import brings existing infrastructure under management. CI/CD runs plan on every pull request and gates apply, terraform test checks real assertions, and a guardrail strip covers sensitive values, provisioners as a last resort, and policy scanning. Config (.tf) HCL config Workflow init → validateplan → apply Provider → cloud APIs Multi-cloud patterns AWS · Azure · GCP Configuration language Vars & outputs Locals Modules count/for_each lifecycle block Data sources plan reads ↔ apply writes State (tfstate) tfstate plan's diff source Backend + lock Workspaces Import infra Test, ship & guard IaC vs config mgmt terraform test CI/CD: GitHub Actions sensitive = true Provisioners IaC scanning · Checkov Conftest: policy as code
How it fits together (5 steps)
  1. Start at Terraform Fundamentals, writing a first .tf file; the workflow runs init → validate → plan → apply, and it's the provider plugin that turns apply into the real create/update/delete calls against a cloud API — the same mechanism behind multi-cloud patterns.
  2. The configuration language: variables and outputs, locals and modules for reuse, count and for_each to repeat resources safely, the lifecycle block's meta-arguments, and data sources for reading what Terraform doesn't manage.
  3. State is what actually exists and what plan diffs the configuration against; a remote backend adds locking so two runs can't corrupt it, workspaces give one config a separate state per environment, and import brings infrastructure created outside Terraform under its management.
  4. Shipping it: IaC vs. configuration management is why this is declarative in the first place, terraform test checks a plan or apply against a real assertion, not just that it ran, and CI/CD with GitHub Actions runs plan on every pull request and gates apply behind review.
  5. Guardrails: sensitive = true masks a secret in plan output but state still keeps it in plain text; provisioners are a last resort HashiCorp itself warns against; and Checkov plus policy as code with conftest catch what review alone can miss.

Beginner

Start here — no prior experience needed

7 lessons
terraform beginner
5 min read

Write a first Terraform config, apply it, change it and destroy it, seeing the real plan each time and why Terraform compares config, state and reality.

terraform beginner
5 min read

What terraform init, fmt, validate, plan -out and apply each guarantee, with real output, including the errors each step catches and a stale saved plan.

terraform beginner
5 min read

What terraform.tfstate holds, what happens when it is lost or a resource is renamed, and how state mv, state rm and moved blocks fix it, with real output.

terraform beginner
5 min read

Input variables with types, defaults and validation, the order -var, tfvars and TF_VAR_ values win in, and outputs that scripts read, with real output.

terraform beginner
5 min read

How providers supply resource types, how arguments differ from attributes, how references order an apply, and what a provider version mismatch looks like.

terraform beginner
5 min read

A locals block computes a value once from variables or other locals, so one edit cascades everywhere, shown with the real replacement plan it causes.

terraform beginner
5 min read

Build a local Terraform module with inputs, validation and outputs, call it twice, then move it to for_each with moved blocks, all with real plan output.

Intermediate

For developers with core concepts down

7 lessons
terraform intermediate
5 min read

count keys instances by position, for_each by name. See the real plan when an item is removed from the middle, and the for_each error on unknown keys.

terraform intermediate
5 min read

Three lifecycle settings with real plans: a change that never applies, a destroy that fails, and create_before_destroy deleting the file it just created.

terraform intermediate
5 min read

Data sources read what Terraform doesn't manage. See when they're read, a hidden dependency that fails, the reference and depends_on fixes, and a cycle.

terraform intermediate
5 min read

sensitive = true masks a secret in plan output, but state keeps it in plain text. Real runs show both, plus why an ephemeral variable is refused.

terraform intermediate
5 min read

A second Terraform run blocked by the state lock, -lock-timeout waiting it out, and a hand-made hotfix that plan would silently revert, all from real runs.

terraform intermediate
5 min read

Terraform workspaces give one config separate state per environment. See them work, fail on an unknown workspace, and compare with a directory per env.

terraform intermediate
5 min read

A GitHub Actions workflow for Terraform, with the real output of each step it runs: fmt -check, init, validate, plan -detailed-exitcode and a saved plan.

Advanced

Production-grade patterns for experienced engineers

3 lessons

Also part of Terraform

Taught in other topics. Each link opens the lesson in its home topic.

4 lessons