Learn Terraform

Define cloud infrastructure as code and apply changes with a reviewable plan

24 lessons · 4 hours · 4 levels · covers Terraform 1.14

Terraform describes infrastructure (networks, servers, databases, DNS, SaaS settings) in HCL files, shows a plan of what will change, and applies it through providers for AWS, Azure, Google Cloud, Kubernetes and thousands of other APIs. It is the most widely used infrastructure-as-code tool; OpenTofu is its open-source fork with the same language.

Before you start

Terraform builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics8 lessons · 1 hour in total
2. PracticalUse it productively at work, with the usual idioms15 lessons · 2 hours in total
3. AdvancedThe less common features, performance and trade-offs20 lessons · 3 hours in total
4. ExpertInternals and the edge cases experts know24 lessons · 4 hours in total

The Terraform tree

TerraformInfrastructure as code and TerraformInfrastructure ascode and TerraformProviders and terraform initProviders andterraform initResources, arguments and referencesResources,arguments and refe…plan, apply and destroyplan, apply anddestroyInput variables and outputsInput variables andoutputsWhat state is and why it mattersWhat state is andwhy it mattersData sourcesData sourcesExpressions, types and conditionalsExpressions, typesand conditionalsRemote backends and lockingRemote backendsand lockingLocals and built-in functionsLocals and built-infunctionscount and for_eachcount and for_eachdepends_on and the lifecycle blockdepends_on and thelifecycle blockWriting and using modulesWriting and usingmodulesModule sources, versions and the registryModule sources,versions and the re…Environments: directories vs workspacesEnvironments:directories vs work…Ignoring files with .gitignoreIgnoring files with.gitignoreRemotes and remote-tracking branchesRemotes andremote-tracking br…
Essentials and Practical shown (15 of 24 lessons); the full list is below. Click a lesson to read it.

All 24 lessons, in order

Basics

  1. Infrastructure as code and Terraform · Essentials · 7 min

    Describe infrastructure in versioned files and let Terraform work out and apply the changes.

  2. Providers and terraform init · Essentials · 8 min

    Declare required providers with version constraints and initialise a working directory with terraform init.

  3. Resources, arguments and references · Essentials · 10 min

    Write resource blocks, set their arguments, and connect resources by referencing attributes.

  4. plan, apply and destroy · Essentials · 8 min

    Read plan output symbols, apply saved plans, target carefully and destroy what you no longer need.

  5. Input variables and outputs · Essentials · 10 min

    Parameterise configurations with typed variables, set them from files and the environment, and expose outputs.

State

  1. What state is and why it matters · Essentials · 10 min

    Terraform's state maps your configuration to real resource IDs; protect it, never commit it, and never edit it by hand.

  2. Remote backends and locking · Practical · 10 min

    Store state remotely in S3, GCS, Azure Storage or HCP Terraform, with locking so two runs never collide.

  3. Refactoring: moved, import and removed · Advanced · 12 min

    Rename and restructure without recreating resources, bring existing infrastructure under management, and let go of resources safely.

The language

  1. Data sources · Essentials · 7 min

    Read existing infrastructure and computed information with data blocks instead of hard-coding IDs.

  2. Expressions, types and conditionals · Essentials · 10 min

    Use HCL's types, operators, conditional expressions, splats and for expressions to compute values.

  3. Locals and built-in functions · Practical · 10 min

    Name intermediate values with locals and transform data with built-in functions like merge, lookup and cidrsubnet.

  4. count and for_each · Practical · 12 min

    Create several instances of a resource from a number with count or from a map or set with for_each.

  5. depends_on and the lifecycle block · Practical · 10 min

    Add hidden dependencies with depends_on and control replacement and deletion with lifecycle settings.

  6. Dynamic blocks and complex collections · Advanced · 10 min

    Generate repeated nested blocks with dynamic, and flatten nested data structures for for_each.

  7. Validation, pre/postconditions and checks · Advanced · 10 min

    Reject bad inputs with variable validation, assert assumptions with pre/postconditions, and monitor with check blocks.

Modules

  1. Writing and using modules · Practical · 12 min

    Package resources into reusable modules with inputs and outputs, and call them from root configurations.

  2. Module sources, versions and the registry · Practical · 8 min

    Use modules from the Terraform Registry, git repositories and private registries, pinned to versions.

  3. Testing modules with terraform test · Advanced · 12 min

    Write .tftest.hcl files with run blocks and assertions, using plan-only runs and mocked providers.

Team workflow

  1. Environments: directories vs workspaces · Practical · 10 min

    Separate dev, staging and prod state with per-environment directories or CLI workspaces, and know the trade-offs.

  2. Terraform in CI/CD · Advanced · 12 min

    Run plans on pull requests, apply reviewed saved plans on merge, and authenticate with short-lived credentials.

Internals

  1. State internals, refresh and drift · Expert · 12 min

    Understand the state file's serial and lineage, refresh-only plans, drift handling and safe state surgery.

  2. The dependency graph and execution · Expert · 12 min

    How Terraform builds a resource graph, walks it in parallel, handles unknown values and orders replacements.

  3. How providers work · Expert · 12 min

    Providers are separate plugin processes speaking gRPC; understand schemas, the lock file's hashes, mirrors and dev overrides.

  4. Secrets, sensitive and ephemeral values · Expert · 12 min

    Know what ends up in state and plans, and keep secrets out with ephemeral resources, ephemeral variables and write-only arguments.