Terraform

Intermediate · Roadmap · 62 lessons · 10 groups

Learn Terraform and OpenTofu in depth: the HCL language, providers, state and how to change it safely, modules, environments, testing and policy, CI/CD for infrastructure, and real patterns on AWS, Kubernetes, GCP and Azure.

0%

Not started

Nothing saved yet. Progress is kept in this browser until you sign in.

Start the path

Getting started · 7

The language · 10

Providers · 5

State · 7

Lifecycle and safety · 6

Modules · 5

Structure and environments · 5

Testing and quality · 6

CI/CD and teams · 6

Real-world patterns · 5

01Why Terraform, and where it fits among IaC toolsWhy teams reach for Terraform: one workflow and one language across clouds. And where Ansible, CloudFormation, Pulumi and OpenTofu overlap with it or differ.Beginner · 9 min02Installing and pinning Terraform and OpenTofuInstall the binary, check it against the published checksum, then pin the CLI version in code so every laptop and CI runner runs the same Terraform or OpenTofu.Beginner · 3 min03Your first configuration with local resourcesWrite a first Terraform project that manages files on your own disk, so you can learn the files, the loop and the state with no cloud account and no bill.Beginner · 10 min04Init, validate, plan, apply and destroy in practiceWhat each command in the Terraform loop reads, writes and refuses to do, so a plan you review is the plan that runs, and destroy is never a surprise.Beginner · 8 min05HCL syntax: blocks, arguments and expressionsRead any Terraform file by knowing three things: blocks have a type and labels, arguments assign values, and expressions compute those values from references.Beginner · 13 min06Reading a plan and the dependency graphRead a Terraform plan line by line, spot the replacement that will delete data, and see how references become the graph that sets order and parallelism.Beginner · 5 min07Debugging with TF_LOG and reading errorsTriage a failing Terraform run: read the error's three parts, tell a code error from a provider API error, and turn on TF_LOG only when you need the HTTP calls.Beginner · 5 min01Input variables and validationTurn hard-coded values into input variables, learn which of six sources wins when several set one, and make bad values fail at plan time with validation.Beginner · 11 min02Outputs and localsUse locals to name a value once and reuse it, and outputs to publish results to the CLI, to a parent module and to scripts that need an address or an ID.Beginner · 7 min03Type constraints and complex variablesDeclare list, map, set, tuple and object variables, use optional attributes with defaults, and know when Terraform converts types for you and when it stops.Intermediate · 28 min04Conditionals and for expressionsUse the ternary operator to switch resources on and off, and for expressions to filter, reshape and group lists and maps, with the traps each one sets.Intermediate · 26 min05Built-in functions in practiceThe handful of Terraform functions that do most of the work, merge, lookup, flatten, cidrsubnet and more, and how to test each safely in terraform console.Intermediate · 7 min06count and for_eachCreate many copies of a resource with count or for_each, and see why removing one item from the middle of a list quietly rewrites everything after it.Intermediate · 10 min07Dynamic blocksGenerate repeated nested blocks, such as security group rules, from a map or list with a dynamic block, and learn when plain repeated blocks read better.Intermediate · 11 min08Templates and templatefileRender scripts and config files from variables with templatefile and the ${} and %{} template syntax, and skip the archived template_file data source.Intermediate · 6 min09Data sourcesRead things Terraform did not create, like an existing VPC, an AMI or a published IP list, with data blocks, and know when the read happens and what it costs.Intermediate · 19 min10Dependencies, implicit and explicitLet references order your resources, and add depends_on only for the hidden dependency Terraform cannot see, like a policy a server needs at boot.Intermediate · 4 min01Providers and the registryWhat a provider is, how a source address like hashicorp/aws turns into a signed download from a registry, and how Terraform's and OpenTofu's registries differ.Beginner · 6 min02Provider configuration, aliases and multi-regionConfigure a provider once, add aliased configurations for a second region or account, choose one per resource, and pass them into modules without surprises.Intermediate · 10 min03Version constraints and the lock fileWrite provider version constraints, understand what .terraform.lock.hcl records, and see h1 and zh hashes computed from real zips that match a real lock file.Intermediate · 8 min04Authenticating providers without static keysStop pasting access keys into Terraform: use the provider's credential chain, assume a role, and in CI swap a short-lived OIDC token for temporary credentials.Intermediate · 10 min05Utility providers: random, time and terraform_dataUse the random, time and built-in terraform_data providers to hold values steady, delay a step or trigger replacement, and leave null_resource behind.Intermediate · 10 min01Inside the state fileOpen terraform.tfstate and read it: serial, lineage, resource addresses, attributes and the secrets that sit there in plain text.Intermediate · 6 min02Remote backends: S3, GCS and azurermMove state off your laptop into a bucket: the S3, GCS and azurerm backends, the bootstrap problem, partial configuration and what to lock down.Intermediate · 26 min03State locking and concurrent appliesWhy two simultaneous applies corrupt state, how S3 use_lockfile and other backends lock it, and what to do when a crashed run leaves a stale lock.Intermediate · 9 min04The terraform state commandslist, show, mv, rm, pull and push: the commands that read and change state without hand-editing it, and the cases where a config block is the safer tool.Intermediate · 11 min05Moved blocks and safe refactoringRename a resource, move it into a module or switch from count to for_each without a destroy: the moved block, with a demo of the plan before and after.Intermediate · 18 min06Importing existing infrastructureAdopt resources created by hand with import blocks and -generate-config-out, find the right ID, and clean up generated code before you trust it.Intermediate · 14 min07Drift detection and refresh-only plansSomeone changed a resource in the console. How plan reports it, what -refresh-only does with state, and how to catch drift on a schedule with exit codes.Intermediate · 4 min01Lifecycle rules: create_before_destroy, prevent_destroy, ignore_changesThe lifecycle block changes how Terraform replaces, protects and ignores things. What each rule does, what it cannot stop, and a replace timeline run for real.Intermediate · 15 min02Replacing resources and replace_triggered_byForce a rebuild with apply -replace, let one resource's change trigger another's rebuild with replace_triggered_by, and why taint is on the way out.Intermediate · 9 min03Provisioners and why to avoid themlocal-exec, remote-exec and file run scripts during apply. Why Terraform cannot model them, how failure taints a resource, and what to use instead.Intermediate · 25 min04Preconditions, postconditions and check blocksWrite down what a resource assumes and what it guarantees. Which custom conditions stop an apply, which only warn, and how they differ from validation.Intermediate · 22 min05Sensitive values and secrets in stateMarking a variable or output sensitive hides it from the screen, not from state or plan files. What it protects, what it doesn't, and how to lock down the rest.Intermediate · 3 min06Ephemeral resources and write-only argumentsTerraform 1.10 and 1.11 can read a secret and pass it to a resource without ever writing it to state or plan files. How ephemeral blocks and _wo arguments work.Advanced · 19 min01What modules are and the root moduleEvery Terraform directory is already a module. How the root module calls child modules, how addresses and scope change, and what init does for them.Beginner · 15 min02Writing a module: inputs, outputs and structureDesign a module's interface first: which variables are required, which have defaults, what to output, the standard file layout and where providers belong.Intermediate · 23 min03Module sources and versioningWhere a module can come from (path, registry, Git), how to pin it with version or ref, and why the lock file does not cover modules.Intermediate · 24 min04Composition over nesting, and for_each on modulesKeep the module tree flat and pass dependencies in, then call a module once per item with for_each and read why keys, not counts, keep addresses stable.Advanced · 30 min05Documenting and publishing modulesGenerate input and output docs with terraform-docs, then publish with release tags to the Terraform registry, the OpenTofu registry or a private one.Intermediate · 14 min01Repository layouts for TerraformWhere Terraform code lives in a repo, how modules and live configuration split, and what to commit, with a script that catches a shared state key.Beginner · 9 min02Workspaces and what they are notA CLI workspace is a second copy of state in one directory with one backend and one set of credentials. Here is when that is enough and when it is not.Beginner · 21 min03Environments with directories and tfvarsOne root module per environment with its own backend, the same modules, and tfvars for the differences, plus the exact order Terraform picks values in.Intermediate · 21 min04Terragrunt and keeping config DRYTerragrunt generates repeated backend and provider blocks, wires unit outputs together and runs a tree in dependency order. What it costs and when it pays.Intermediate · 17 min05Splitting state and limiting blast radiusCut Terraform state by rate of change, ownership, access and cost of a mistake, pass values between layers, and stop before the cuts get too small.Advanced · 25 min01Formatting, validation and TFLintThe three cheap checks to run on every commit: terraform fmt, terraform validate and TFLint, what each knows and what each misses, in the order to run them.Beginner · 8 min02terraform test: the native testing frameworkWrite .tftest.hcl files with run blocks, assert and expect_failures, know which runs create real infrastructure, and run plan-only tests cheaply.Intermediate · 28 min03Mock providers and fast unit testsmock_provider, mock_resource defaults and the override blocks let terraform test run logic checks with no credentials, and show what mocks cannot prove.Intermediate · 42 min04Integration testing with TerratestTerratest is a Go library that applies real Terraform, checks the result and destroys it. When it beats terraform test, and its 2026 versions.Advanced · 9 min05Policy as code with OPA, Checkov and TrivyCheck the plan against rules before apply: plan JSON, OPA, Checkov and Trivy compared, plus Terraform Policy, generally available in 1.17.Intermediate · 15 min06Cost estimation with InfracostPut a price delta next to the plan in the pull request: real list prices, how Infracost works, and what its 2.0 CLI change means for older tutorials.Intermediate · 14 min01Terraform in CI: saved plans and approvalsRun Terraform from a pipeline with a saved plan, a pinned version, a queue and an approval gate, so the plan a person approved is the one applied.Intermediate · 25 min02OIDC from CI to the cloudGive a pipeline short-lived cloud credentials with OIDC instead of stored keys, write the trust policy in Terraform, and watch for GitHub's 2026 subject change.Intermediate · 28 min03Pull request automation with AtlantisAtlantis plans and applies Terraform from pull request comments. How atlantis.yaml, requirements and locks work, and the risk of running plan on PR code.Intermediate · 11 min04HCP Terraform and managed platformsWhat HCP Terraform runs for you (state, runs, policy, registry), how its workspaces differ from CLI ones, what it costs, and how to choose.Intermediate · 13 min05Upgrading Terraform and providersUpgrade Terraform and providers in small, boring steps: what the v1.x promises cover, release cadence from real dates, and a routine built on init -upgrade.Intermediate · 9 min06OpenTofu vs Terraform: encryption and migrationOpenTofu's state and plan encryption, how the two tools have diverged since 1.7 by release, and how to migrate safely with backups and a small first change.Advanced · 20 min01A VPC on AWSBuild an AWS network in Terraform: plan CIDR ranges with cidrsubnet, then VPC, subnets, gateways and routes with for_each, and what NAT and public IPv4 cost.Intermediate · 23 min02IAM roles and policies in TerraformWrite trust and permissions policies with aws_iam_policy_document, catch typos early, and see how a wildcard like s3:Put* expands against AWS's action list.Intermediate · 11 min03Compute and load balancing on AWSWire an ALB, target group, listener, launch template and Auto Scaling group with security groups that reference each other, and what the ALB costs per month.Intermediate · 32 min04Kubernetes clusters and the Kubernetes and Helm providersCreate a managed cluster, then configure the kubernetes and helm providers against it without the chicken-and-egg trap, including Helm provider 3's new syntax.Advanced · 19 min05GCP and Azure equivalentsMap the AWS patterns to Google Cloud and Azure resources, and learn the rules that change the code: global VPCs on GCP and resource groups on Azure.Intermediate · 23 min

Getting started

7 lessons

  1. 01 / 07Why Terraform, and where it fits among IaC toolsWhy teams reach for Terraform: one workflow and one language across clouds. And where Ansible, CloudFormation, Pulumi and OpenTofu overlap with it or differ.9 min
  2. 02 / 07Installing and pinning Terraform and OpenTofuInstall the binary, check it against the published checksum, then pin the CLI version in code so every laptop and CI runner runs the same Terraform or OpenTofu.3 min
  3. 03 / 07Your first configuration with local resourcesWrite a first Terraform project that manages files on your own disk, so you can learn the files, the loop and the state with no cloud account and no bill.10 min
  4. 04 / 07Init, validate, plan, apply and destroy in practiceWhat each command in the Terraform loop reads, writes and refuses to do, so a plan you review is the plan that runs, and destroy is never a surprise.8 min
  5. 05 / 07HCL syntax: blocks, arguments and expressionsRead any Terraform file by knowing three things: blocks have a type and labels, arguments assign values, and expressions compute those values from references.13 min
  6. 06 / 07Reading a plan and the dependency graphRead a Terraform plan line by line, spot the replacement that will delete data, and see how references become the graph that sets order and parallelism.5 min
  7. 07 / 07Debugging with TF_LOG and reading errorsTriage a failing Terraform run: read the error's three parts, tell a code error from a provider API error, and turn on TF_LOG only when you need the HTTP calls.5 min

The language

10 lessons

  1. 01 / 10Input variables and validationTurn hard-coded values into input variables, learn which of six sources wins when several set one, and make bad values fail at plan time with validation.11 min
  2. 02 / 10Outputs and localsUse locals to name a value once and reuse it, and outputs to publish results to the CLI, to a parent module and to scripts that need an address or an ID.7 min
  3. 03 / 10Type constraints and complex variablesDeclare list, map, set, tuple and object variables, use optional attributes with defaults, and know when Terraform converts types for you and when it stops.28 min
  4. 04 / 10Conditionals and for expressionsUse the ternary operator to switch resources on and off, and for expressions to filter, reshape and group lists and maps, with the traps each one sets.26 min
  5. 05 / 10Built-in functions in practiceThe handful of Terraform functions that do most of the work, merge, lookup, flatten, cidrsubnet and more, and how to test each safely in terraform console.7 min
  6. 06 / 10count and for_eachCreate many copies of a resource with count or for_each, and see why removing one item from the middle of a list quietly rewrites everything after it.10 min
  7. 07 / 10Dynamic blocksGenerate repeated nested blocks, such as security group rules, from a map or list with a dynamic block, and learn when plain repeated blocks read better.11 min
  8. 08 / 10Templates and templatefileRender scripts and config files from variables with templatefile and the ${} and %{} template syntax, and skip the archived template_file data source.6 min
  9. 09 / 10Data sourcesRead things Terraform did not create, like an existing VPC, an AMI or a published IP list, with data blocks, and know when the read happens and what it costs.19 min
  10. 10 / 10Dependencies, implicit and explicitLet references order your resources, and add depends_on only for the hidden dependency Terraform cannot see, like a policy a server needs at boot.4 min

Providers

5 lessons

  1. 01 / 05Providers and the registryWhat a provider is, how a source address like hashicorp/aws turns into a signed download from a registry, and how Terraform's and OpenTofu's registries differ.6 min
  2. 02 / 05Provider configuration, aliases and multi-regionConfigure a provider once, add aliased configurations for a second region or account, choose one per resource, and pass them into modules without surprises.10 min
  3. 03 / 05Version constraints and the lock fileWrite provider version constraints, understand what .terraform.lock.hcl records, and see h1 and zh hashes computed from real zips that match a real lock file.8 min
  4. 04 / 05Authenticating providers without static keysStop pasting access keys into Terraform: use the provider's credential chain, assume a role, and in CI swap a short-lived OIDC token for temporary credentials.10 min
  5. 05 / 05Utility providers: random, time and terraform_dataUse the random, time and built-in terraform_data providers to hold values steady, delay a step or trigger replacement, and leave null_resource behind.10 min

State

7 lessons

  1. 01 / 07Inside the state fileOpen terraform.tfstate and read it: serial, lineage, resource addresses, attributes and the secrets that sit there in plain text.6 min
  2. 02 / 07Remote backends: S3, GCS and azurermMove state off your laptop into a bucket: the S3, GCS and azurerm backends, the bootstrap problem, partial configuration and what to lock down.26 min
  3. 03 / 07State locking and concurrent appliesWhy two simultaneous applies corrupt state, how S3 use_lockfile and other backends lock it, and what to do when a crashed run leaves a stale lock.9 min
  4. 04 / 07The terraform state commandslist, show, mv, rm, pull and push: the commands that read and change state without hand-editing it, and the cases where a config block is the safer tool.11 min
  5. 05 / 07Moved blocks and safe refactoringRename a resource, move it into a module or switch from count to for_each without a destroy: the moved block, with a demo of the plan before and after.18 min
  6. 06 / 07Importing existing infrastructureAdopt resources created by hand with import blocks and -generate-config-out, find the right ID, and clean up generated code before you trust it.14 min
  7. 07 / 07Drift detection and refresh-only plansSomeone changed a resource in the console. How plan reports it, what -refresh-only does with state, and how to catch drift on a schedule with exit codes.4 min

Lifecycle and safety

6 lessons

  1. 01 / 06Lifecycle rules: create_before_destroy, prevent_destroy, ignore_changesThe lifecycle block changes how Terraform replaces, protects and ignores things. What each rule does, what it cannot stop, and a replace timeline run for real.15 min
  2. 02 / 06Replacing resources and replace_triggered_byForce a rebuild with apply -replace, let one resource's change trigger another's rebuild with replace_triggered_by, and why taint is on the way out.9 min
  3. 03 / 06Provisioners and why to avoid themlocal-exec, remote-exec and file run scripts during apply. Why Terraform cannot model them, how failure taints a resource, and what to use instead.25 min
  4. 04 / 06Preconditions, postconditions and check blocksWrite down what a resource assumes and what it guarantees. Which custom conditions stop an apply, which only warn, and how they differ from validation.22 min
  5. 05 / 06Sensitive values and secrets in stateMarking a variable or output sensitive hides it from the screen, not from state or plan files. What it protects, what it doesn't, and how to lock down the rest.3 min
  6. 06 / 06Ephemeral resources and write-only argumentsTerraform 1.10 and 1.11 can read a secret and pass it to a resource without ever writing it to state or plan files. How ephemeral blocks and _wo arguments work.19 min

Modules

5 lessons

  1. 01 / 05What modules are and the root moduleEvery Terraform directory is already a module. How the root module calls child modules, how addresses and scope change, and what init does for them.15 min
  2. 02 / 05Writing a module: inputs, outputs and structureDesign a module's interface first: which variables are required, which have defaults, what to output, the standard file layout and where providers belong.23 min
  3. 03 / 05Module sources and versioningWhere a module can come from (path, registry, Git), how to pin it with version or ref, and why the lock file does not cover modules.24 min
  4. 04 / 05Composition over nesting, and for_each on modulesKeep the module tree flat and pass dependencies in, then call a module once per item with for_each and read why keys, not counts, keep addresses stable.30 min
  5. 05 / 05Documenting and publishing modulesGenerate input and output docs with terraform-docs, then publish with release tags to the Terraform registry, the OpenTofu registry or a private one.14 min

Structure and environments

5 lessons

  1. 01 / 05Repository layouts for TerraformWhere Terraform code lives in a repo, how modules and live configuration split, and what to commit, with a script that catches a shared state key.9 min
  2. 02 / 05Workspaces and what they are notA CLI workspace is a second copy of state in one directory with one backend and one set of credentials. Here is when that is enough and when it is not.21 min
  3. 03 / 05Environments with directories and tfvarsOne root module per environment with its own backend, the same modules, and tfvars for the differences, plus the exact order Terraform picks values in.21 min
  4. 04 / 05Terragrunt and keeping config DRYTerragrunt generates repeated backend and provider blocks, wires unit outputs together and runs a tree in dependency order. What it costs and when it pays.17 min
  5. 05 / 05Splitting state and limiting blast radiusCut Terraform state by rate of change, ownership, access and cost of a mistake, pass values between layers, and stop before the cuts get too small.25 min

Testing and quality

6 lessons

  1. 01 / 06Formatting, validation and TFLintThe three cheap checks to run on every commit: terraform fmt, terraform validate and TFLint, what each knows and what each misses, in the order to run them.8 min
  2. 02 / 06terraform test: the native testing frameworkWrite .tftest.hcl files with run blocks, assert and expect_failures, know which runs create real infrastructure, and run plan-only tests cheaply.28 min
  3. 03 / 06Mock providers and fast unit testsmock_provider, mock_resource defaults and the override blocks let terraform test run logic checks with no credentials, and show what mocks cannot prove.42 min
  4. 04 / 06Integration testing with TerratestTerratest is a Go library that applies real Terraform, checks the result and destroys it. When it beats terraform test, and its 2026 versions.9 min
  5. 05 / 06Policy as code with OPA, Checkov and TrivyCheck the plan against rules before apply: plan JSON, OPA, Checkov and Trivy compared, plus Terraform Policy, generally available in 1.17.15 min
  6. 06 / 06Cost estimation with InfracostPut a price delta next to the plan in the pull request: real list prices, how Infracost works, and what its 2.0 CLI change means for older tutorials.14 min

CI/CD and teams

6 lessons

  1. 01 / 06Terraform in CI: saved plans and approvalsRun Terraform from a pipeline with a saved plan, a pinned version, a queue and an approval gate, so the plan a person approved is the one applied.25 min
  2. 02 / 06OIDC from CI to the cloudGive a pipeline short-lived cloud credentials with OIDC instead of stored keys, write the trust policy in Terraform, and watch for GitHub's 2026 subject change.28 min
  3. 03 / 06Pull request automation with AtlantisAtlantis plans and applies Terraform from pull request comments. How atlantis.yaml, requirements and locks work, and the risk of running plan on PR code.11 min
  4. 04 / 06HCP Terraform and managed platformsWhat HCP Terraform runs for you (state, runs, policy, registry), how its workspaces differ from CLI ones, what it costs, and how to choose.13 min
  5. 05 / 06Upgrading Terraform and providersUpgrade Terraform and providers in small, boring steps: what the v1.x promises cover, release cadence from real dates, and a routine built on init -upgrade.9 min
  6. 06 / 06OpenTofu vs Terraform: encryption and migrationOpenTofu's state and plan encryption, how the two tools have diverged since 1.7 by release, and how to migrate safely with backups and a small first change.20 min

Real-world patterns

5 lessons

  1. 01 / 05A VPC on AWSBuild an AWS network in Terraform: plan CIDR ranges with cidrsubnet, then VPC, subnets, gateways and routes with for_each, and what NAT and public IPv4 cost.23 min
  2. 02 / 05IAM roles and policies in TerraformWrite trust and permissions policies with aws_iam_policy_document, catch typos early, and see how a wildcard like s3:Put* expands against AWS's action list.11 min
  3. 03 / 05Compute and load balancing on AWSWire an ALB, target group, listener, launch template and Auto Scaling group with security groups that reference each other, and what the ALB costs per month.32 min
  4. 04 / 05Kubernetes clusters and the Kubernetes and Helm providersCreate a managed cluster, then configure the kubernetes and helm providers against it without the chicken-and-egg trap, including Helm provider 3's new syntax.19 min
  5. 05 / 05GCP and Azure equivalentsMap the AWS patterns to Google Cloud and Azure resources, and learn the rules that change the code: global VPCs on GCP and resource groups on Azure.23 min
Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark