Infrastructure as Code with Terraform

Describe infrastructure in code, and plan, apply, test and share it safely with Terraform: providers, variables, modules, remote state with locking, workspaces, CI pipelines, drift and import.

Intermediate · Cloud & DevOps · about 18 hours · 5 modules · certificate on completion

Taught by Lachlaneè Mitchèll, Katarína Novákovà, AI Learning Companions.

About this course

Clicking through a cloud console doesn't scale: nobody can review it, repeat it or say exactly what changed last Tuesday. Terraform describes infrastructure as code, so it can be reviewed, versioned, tested and applied the same way every time. This course teaches Terraform hands-on, on your own laptop, with providers that need no cloud account: local files, random values, Docker containers and a PostgreSQL server for shared state.

You'll write configurations with providers, resources, variables, outputs and expressions; read plans and know before you apply whether a change updates or replaces; move state to a remote backend and watch locking stop two runs from colliding; refactor safely with moved blocks; and build a reusable module that runs nginx containers in dev and prod workspaces. The last module covers what makes Terraform safe for a team: formatting and validation gates, unit tests with mock providers, plan-and-apply pipelines in CI, drift detection, importing resources created by hand, and keeping secrets out of state and code. Every module ends with a lab whose expected output you can check, and the capstone has you codify a small platform end to end, from module tests to a CI workflow.

What you'll learn

  • Write Terraform configurations with providers, resources, variables, outputs and expressions
  • Keep state safe in a remote backend with locking, and refactor it with moved and removed blocks
  • Build reusable modules and run several environments with workspaces or separate root modules
  • Test configurations with fmt, validate and terraform test, and plan and apply them in a CI pipeline
  • Detect drift, import existing resources and protect the secrets that state contains

Skills you'll gain

Terraform, Infrastructure as code, HCL, Terraform modules, Remote state and locking, Terraform testing, CI/CD for infrastructure, Drift detection, Secrets management

Tools

Terraform, Docker, PostgreSQL, GitHub Actions

Before you start

Comfort on the command line (Linux Command Line Essentials covers it) and with Git (Git and GitHub Workflows for Teams helps). Install Terraform 1.11 or later (the labs were checked with 1.16) and Docker. Every lab uses providers that need no cloud account; Cloud Computing Fundamentals helps you map the ideas onto a cloud provider.

Modules

  1. Terraform Foundations: Providers, Resources and the Workflow

    What infrastructure as code is and how Terraform works: providers and their versions, resources and the dependency graph, and the init, plan, apply and destroy workflow.

    • Infrastructure as code, and how Terraform works (12 min)
    • Providers, versions and the lock file (12 min)
    • Resources, plans and the dependency graph (13 min)
    • Hands-on lab: Build, change and destroy local infrastructure
  2. Variables, Outputs and Expressions

    Make configurations reusable with typed, validated input variables and per-environment tfvars files, compute values with locals and expressions, create many similar resources with for_each and count, and publish outputs.

    • Input variables and tfvars files (12 min)
    • Locals, outputs and expressions (12 min)
    • count, for_each, dynamic blocks and data sources (13 min)
    • Hands-on lab: Parameterise a configuration for two environments
  3. State, Backends and Locking

    What Terraform's state holds and why it matters, how to keep it in a shared remote backend with locking, and how to refactor and repair it safely with moved and removed blocks.

    • What state is, and why it matters (12 min)
    • Remote backends and state locking (13 min)
    • Refactoring and repairing state (12 min)
    • Hands-on lab: Move state to PostgreSQL and see locking at work
  4. Modules, Workspaces and Environments

    Package infrastructure as reusable modules with clear inputs and outputs, share and version them, and run one configuration as several environments with workspaces or separate root modules.

    • Writing reusable modules (13 min)
    • Sharing and versioning modules (11 min)
    • Workspaces and environments (12 min)
    • Hands-on lab: A Docker web module across two workspaces
  5. Testing, CI, Drift and Security

    Check and test configurations before they change anything, run plan and apply in a CI pipeline, detect and resolve drift, import existing resources, and protect the secrets in state.

    • Static checks and terraform test (13 min)
    • Plan and apply in CI (12 min)
    • Drift detection and import (12 min)
    • Securing state and secrets (12 min)
    • Hands-on lab: Test, check and repair your configurations

Project: Codify a local platform end to end

Your team wants its local demo platform managed entirely with Terraform. Extend the web module from module 4, or write your own, so that one module creates a Docker network, nginx containers serving a generated page, and a Redis container whose password comes from random_password. Run it as dev and prod (with workspaces or a root module each), with state in the PostgreSQL backend and locking on. Gate it with fmt, validate and terraform test using mock providers, and adapt the starter GitHub Actions workflow so pull requests are checked and planned and main is applied only after approval. Show drift detection and an import of a resource created by hand, and write down how state and secrets are protected.

Part of

The products this course names are trademarks of their owners, who haven't made or endorsed it.

Start the course free