LearnDevOpsHands-On Labs
All Tutorials
๐ŸŒ
IaC Standard ยท Intermediate

Terraform (Infrastructure as Code)

Provision multi-cloud infrastructure declaratively. Master HCL, state locks, reusable modules, workspaces, and CI/CD pipelines.

34 Modules & Lessons
Estimated Time: ~12 Hours

Curriculum & Notes

Click any title or "See Notes" to read concepts

1Module 1: IaC Foundations & Workflow

7 lessons
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.

2Module 2: Variables, Outputs & Locals

7 lessons
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.

3Module 3: State Management & Backends

7 lessons
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.

4Module 4: Writing Reusable Modules

7 lessons
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.

5Module 5: Advanced HCL & Production CI/CD

6 lessons
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.