Terraform Tutorial
Learn Terraform step by step: 37 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Infrastructure as Code Basics
- What Is Terraform? Infrastructure as Code Explained Learn what Terraform is, why teams describe servers, networks and databases as code, how declarative IaC differs from scripts, and how Terraform compares to other tools.
- Installing Terraform and Your First Configuration Install Terraform or OpenTofu on Windows, macOS or Linux, write a first main.tf with the local and random providers, and run init, plan, apply and destroy safely.
- The Core Terraform Workflow: Write, Plan, Apply Understand the Terraform workflow in depth: init, fmt, validate, plan, saved plans, apply, destroy, -target, -replace, refresh-only and exit codes, with a plan diff stand-in.
- How Terraform Works: Core, Providers, State and the Graph Look inside Terraform: the core binary, provider plugins and their gRPC protocol, state, the resource graph, parallel execution, and the phases of plan and apply.
The HCL Language
- HCL Syntax and Project Files Learn HCL syntax for Terraform: blocks, labels, arguments, comments, identifiers, how .tf files are loaded, a good file layout, JSON syntax and override files.
- Types and Values in Terraform Learn Terraform's type system: string, number, bool, list, set, map, tuple, object, null and any, automatic type conversion, and how to write type constraints.
- Expressions, Operators and Conditionals Master Terraform expressions: references, string interpolation, heredocs, template directives, arithmetic and logical operators, the conditional operator and common patterns.
- for Expressions, Splat and Dynamic Blocks Transform lists and maps with Terraform for expressions, filter with if, group with the ellipsis, use splat expressions and generate repeated nested blocks with dynamic.
- Terraform Built-in Functions A practical tour of Terraform's built-in functions: strings, numbers, collections, encoding, filesystem, IP networking and type functions, including try, coalesce and templatefile.
Providers and Resources
- Providers and Version Constraints Configure Terraform providers: required_providers and source addresses, version constraints like ~>, the dependency lock file, provider blocks, aliases for multi-region setups.
- Resources and References Learn the resource block in Terraform: arguments versus computed attributes, referencing other resources, implicit dependencies, timeouts, naming and the resource life cycle.
- Dependencies and the Resource Graph Understand implicit and explicit dependencies in Terraform: depends_on, the resource graph, terraform graph, dependency cycles, parallelism and destroy order.
- count and for_each: Creating Many Resources Create many similar resources with Terraform count and for_each: syntax, addresses, count.index, each.key, the index-shift problem, toset, maps, modules and when to choose which.
- Lifecycle Rules and Provisioners Control resource behavior with the Terraform lifecycle block: create_before_destroy, prevent_destroy, ignore_changes, replace_triggered_by and conditions, plus provisioners and terraform_data.
Variables, Outputs and Data Sources
- Input Variables and tfvars Files Parameterize Terraform with input variables: type, default, description, nullable, passing values with -var, tfvars files and TF_VAR environment variables, and the precedence order.
- Variable Validation and Sensitive Values Reject bad input early with Terraform validation blocks, handle secrets with sensitive and ephemeral variables, understand what ends up in state, and use check blocks.
- Local Values and Outputs Use Terraform locals to name and reuse expressions and outputs to expose results: syntax, sensitive and described outputs, terraform output -json and -raw, and module outputs.
- Data Sources: Reading Existing Infrastructure Use Terraform data sources to read existing infrastructure: aws_ami, availability zones, caller identity, IAM policy documents, filters, timing, errors and when to prefer resources.
State Management
- Terraform State: What It Is and Why It Exists Understand Terraform state: what terraform.tfstate stores, how Terraform uses it to map code to real resources, and how to inspect it safely with state commands.
- Remote State, Backends and State Locking Store Terraform state remotely with an S3 or HCP Terraform backend, understand state locking, migrate local state and share outputs with terraform_remote_state.
- State Commands: Move, Import, Remove and Replace Refactor safely with Terraform state commands: state mv, moved blocks, import blocks, removed blocks and the -replace flag, with plans that show the effect.
- Drift Detection, Refresh and Lifecycle Rules Detect and handle configuration drift in Terraform: refresh-only plans, lifecycle ignore_changes, prevent_destroy and scheduled drift checks for real infrastructure.
Modules
- Terraform Modules: Your First Reusable Module Learn what Terraform modules are, how to write one with variables and outputs, call it from a root module and pass values between modules step by step.
- Designing Good Terraform Modules: Structure and Validation Design Terraform modules that last: standard file layout, input validation, sensible defaults, composition over nesting, versioned interfaces and documentation habits.
- Module Sources, the Registry and Version Constraints Use modules from the Terraform Registry, Git and local paths, pin them with version constraints (~>, >=), upgrade safely and publish your own modules.
- count, for_each and Testing with Modules Create many module instances with for_each and count, pass maps of settings, avoid index-shift bugs and write native terraform test files for your modules.
Workflows and Collaboration
- The Terraform Workflow: init, validate, plan, apply, destroy Master the everyday Terraform workflow: fmt, validate, plan -out, apply with a saved plan, destroy, variable files, targeted runs and flags that make CI reliable.
- Terraform Workspaces and Managing Multiple Environments Compare Terraform workspaces, directory-per-environment and tfvars layouts for dev, staging and prod. Learn when each pattern is safe and how to avoid cross-environment mistakes.
- Terraform in CI/CD: Plan on Pull Request, Apply on Merge Automate Terraform with GitHub Actions: plan on pull requests, apply on merge, short-lived OIDC credentials, approvals, plan comments, Atlantis and HCP Terraform.
- Terraform Testing, Policy as Code and Security Scanning Build a safety net for Terraform: validate, tflint, terraform test, preconditions, static scanners such as Checkov and Trivy, OPA and Sentinel policy as code on plan JSON.
Cloud Examples
- Terraform on AWS: A VPC, Load Balancer and Web Servers Build a classic AWS web stack with Terraform: VPC, public and private subnets with cidrsubnet, security groups, an application load balancer and an auto scaling group.
- Terraform on Azure and Google Cloud: Equivalent Examples See how Terraform works on Azure and Google Cloud: provider setup, a resource group with storage, a GCP bucket and service account, naming rules and multi-cloud design tips.
- Terraform with Kubernetes, Helm, DNS and SaaS Providers Use Terraform beyond the big clouds: the Kubernetes and Helm providers, Cloudflare DNS, GitHub repositories, provider chaining and the cluster-then-workload pattern.
Projects and Practice
- Project: Host a Static Website with S3, CloudFront and Terraform Build a complete static website stack with Terraform: private S3 bucket, CloudFront with origin access control, HTTPS certificate, DNS and a content-type upload map.
- Project: A Three-Tier Application Built from Modules Assemble a three-tier application from network, database and service modules, deploy it to dev and prod with different settings, and test the wiring with simulations.
- Terraform Best Practices, Pitfalls and OpenTofu A practical checklist of Terraform best practices: structure, state, naming, versions, secrets and review habits, plus common mistakes and how OpenTofu differs from Terraform.
- Terraform Practice: Ten Exercises with Solutions Test your Terraform skills with ten graded exercises on state, moved blocks, modules, version constraints, for_each, drift and policy, each with a hint and full solution.