Terraform Tutorial
Learn Terraform from scratch: Infrastructure as Code fundamentals, the core workflow, variables, state management and reusable modules with hands-on examples.
34 lessons in this tutorial
Introduction to Terraform
Learn what Terraform is and how Infrastructure as Code works, why it beats clicking through consoles, and how it compares to Ansible and CloudFormation.
Installing Terraform
Install Terraform on macOS, Linux and Windows with the official package managers, verify the version, switch versions with tfenv and run your first project.
The Terraform Core Workflow
Understand the Terraform init, plan, apply and destroy loop: what each command does, how to read a plan and the mistakes that cause outages.
terraform init
Learn what terraform init does: download providers, configure the backend, write the lock file, use -upgrade and -reconfigure and run it in CI/CD.
terraform plan
Read terraform plan symbols correctly, save a plan file with -out, target resources, detect drift and run plan in CI/CD without surprises on apply.
terraform apply
Use terraform apply safely: review the plan, apply a saved plan file, tune parallelism, use -auto-approve in CI/CD and recover from common apply errors.
terraform destroy
Run terraform destroy safely: read the destroy plan, preview it first, remove single resources with -target, and keep code tidy with fmt and validate.
Terraform Variables and Outputs
See how data flows through Terraform: input variables, locals and outputs, where each belongs, how variable precedence works and how to handle secrets safely.
Terraform Input Variables
Declare Terraform input variables with types, defaults and validation, then set them via tfvars files, CLI flags and environment variables, including secrets.
Terraform Local Values
Learn how Terraform local values work, when to choose locals over input variables or data sources, and how a locals block removes repeated expressions.
Terraform Output Values
Declare Terraform output values, mark them sensitive, read them with the terraform output command and JSON, expose module outputs and use them in CI/CD.
Terraform State and Backends
Understand why Terraform keeps state, what is inside it, how remote backends and locking make teamwork safe, and the state commands that rescue you.
Terraform State Overview
Learn what Terraform state is, why terraform.tfstate exists, how it maps to real resources, and why drift, state security and locking matter.
Terraform Remote Backends
Configure a Terraform remote backend on S3 with DynamoDB state locking, bootstrap it, compare Terraform Cloud and GCS, and migrate state safely.
Terraform State Commands
Use the Terraform state commands with confidence: state list, show, mv and rm, plus terraform import and refresh, and when each is safe to run.
Terraform Modules
Learn what Terraform modules are, when to write one, how to design a clean interface, version and source them, and move resources in without recreating them.
Using Terraform Modules
Learn how to call Terraform modules with a module block from local paths or the Registry, pass inputs, pin versions, run init and use module outputs.
Writing Terraform Modules
Learn how to write a reusable Terraform module: structure main.tf, variables.tf and outputs.tf, build an S3 bucket module and call it locally.
Terraform Data Sources
Learn how Terraform data sources read existing infrastructure, how the data block works, how to reference its attributes and when to use data over resource.
Terraform count and for_each
Choose between Terraform count and for_each: see why count's positional identity can destroy resources, when each is correct and how to migrate safely.
Terraform Count
Learn how the Terraform count meta-argument works: use count.index, build conditional resources, reference counted resources and know when count fits.
Terraform for_each
Learn how Terraform for_each works with sets, maps and objects, how each.key and each.value are used, how to reference instances and when it beats count.
Terraform Expressions and Functions
Learn Terraform expressions: types, references, conditionals, for expressions, key function categories, try and can, and testing in terraform console.
Terraform String Functions
Work with strings in Terraform: interpolation, format, case and whitespace helpers, replace and regex functions, templatefile and conditionals.
Terraform Collection Functions
Learn Terraform collection functions such as concat, flatten, distinct and slice, plus map and set helpers and for expressions for dynamic code.
Terraform Dynamic Blocks
Learn Terraform dynamic blocks: generate repeated nested blocks from a collection, with security group and IAM policy examples and the limits to respect.
Terraform Workspaces
Learn how Terraform workspaces separate state, the workspace new, select and list commands, terraform.workspace in code, and when to avoid them.
Terraform with AWS
Configure the Terraform AWS provider, authenticate without long-lived keys using assume role and OIDC, manage regions and accounts, and tag everything.
Terraform AWS VPC Setup
Build a custom AWS VPC with Terraform: public and private subnets, an internet gateway, route tables and a NAT gateway, explained step by step.
Terraform AWS EC2 Web Server
Launch an EC2 web server with Terraform: add a security group, find the latest Ubuntu AMI, create a key pair, write user data and attach an Elastic IP.
Terraform AWS S3 Static Assets
Create an S3 bucket with Terraform: unique names with random_id, versioning, lifecycle rules, public access settings, a bucket policy and object uploads.
Terraform Provisioners
Learn how Terraform provisioners work: local-exec, remote-exec, file and connection blocks, null_resource, and why provisioners should be a last resort.
Terraform CI/CD Pipeline
Build a Terraform pipeline in GitLab CI/CD with validate, plan, manual apply and destroy stages, GitLab-managed state and secure credential handling.
Terraform Security
Secure Terraform code by scanning with tfsec and Checkov, protecting secrets in state, fixing common misconfigurations and adding checks to CI/CD.
Terraform Tutorial: common questions
Is Terraform hard to learn?
The core workflow is small: write configuration, run terraform init, plan and apply. Most people are productive within a few days. The harder parts, state management, modules and safe refactoring, are covered later in the series.
Do I need an AWS account to follow this Terraform tutorial?
Not for the first lessons, which use local providers. The AWS lessons create real resources, so a free-tier AWS account is useful there, and the Terraform playground on this site lets you practise the CLI without one.
Is Terraform still worth learning in 2026?
Yes. Terraform and its open-source fork OpenTofu share the same HCL language and workflow, so the skills transfer to both, and infrastructure as code remains a core requirement in DevOps and cloud job listings.
What should I learn before Terraform?
Comfort with the command line and a basic understanding of one cloud provider, such as what a VPC, an EC2 instance and an S3 bucket are. You do not need programming experience.