Labs · DevOps
Lab: Learn the Terraform Workflow (init, fmt, validate, plan, apply, destroy) with the Local Provider, No Cloud Bill
Course: DevOps · Chapter 12: Terraform Introduction
Chapter 12 explains why Terraform and the basic workflow; this lab practises that workflow with zero cloud cost.
Chala mitrano! AWS account nasla tari Terraform shikta yeto. Aaj laptop varach init, plan, apply, destroy.
Suppose we are…
Suppose we have just joined a Netflix-style platform team, and the first rule we hear is: "Nobody clicks in the console; everything is Terraform." Before touching the company AWS account, we want to practise the workflow somewhere it cannot cost money or break anything.
In this lab Terraform creates a plain text file on your laptop. The resource is tiny, but the loop is exactly the one you will use for VPCs and databases: write, init, plan, apply, change, destroy.
Goal of this lab
By the end you will have:
- Terraform installed and
terraform versionworking. - One
local_fileresource with a variable and an output. - A full loop including a change, a manual delete that Terraform heals, and
terraform destroy.
What you need
- Terraform 1.6 or newer. On Windows:
winget install Hashicorp.Terraform; on Mac:brew tap hashicorp/tap && brew install hashicorp/tap/terraform; on Linux, the HashiCorp package repository. - 30 minutes. No cloud account needed.
Part 1: Write the code
Check the install and create a folder:
terraform version mkdir tf-first && cd tf-firstWhat you should see:
Terraform v1.x.x.Create
main.tf:main.tf
terraform { required_version = ">= 1.6" required_providers { local = { source = "hashicorp/local" version = "~> 2.5" } } } variable "team" { type = string default = "platform" } resource "local_file" "welcome" { filename = "${path.module}/welcome.txt" content = "Hello ${var.team} team, this file was created by Terraform.\n" } output "file_path" { value = local_file.welcome.filename }
Part 2: init, fmt, validate, plan, apply
Download the provider:
terraform initWhat you should see:
Installing hashicorp/local v2.5.xandTerraform has been successfully initialized!Format and check the code:
terraform fmt terraform validateWhat you should see:
Success! The configuration is valid.See what would happen:
terraform planWhat you should see:
+ resource "local_file" "welcome"andPlan: 1 to add, 0 to change, 0 to destroy.Apply it and type
yes:terraform apply cat welcome.txtWhat you should see:
Apply complete! Resources: 1 added, the outputfile_path, and the file text withplatform.Look at what Terraform remembers:
terraform state list terraform outputWhat you should see:
local_file.welcomeandfile_path = "./welcome.txt".
Part 3: Change and drift
Change the variable from the command line and plan:
terraform plan -var team=devopsWhat you should see:
must be replacedbecause the content changed, andPlan: 1 to add, 0 to change, 1 to destroy.Delete the file by hand, then plan again:
rm welcome.txt terraform planWhat you should see: Terraform notices the file is missing and plans
1 to add. This is how it heals drift.Apply to bring it back:
terraform apply -auto-approve-auto-approveis fine for a practice file on your laptop; in a team, read the plan and type yes.
Clean up: destroy everything
Delete everything this lab created. Type
yeswhen asked:terraform destroyWhat you should see:
Destroy complete! Resources: 1 destroyed.andwelcome.txtis gone.
Ravindra Bagale's Tip
Commit .terraform.lock.hcl to Git, but never .terraform/ or *.tfstate. The lock file pins provider versions for the whole team.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Running plan before init | Missing provider error | Run terraform init first |
Editing terraform.tfstate by hand | State breaks, Terraform loses track | Use terraform state commands |
Committing .terraform/ and state | Huge repo, secrets in Git | Add them to .gitignore |
Using -auto-approve everywhere | No human reads the plan | Only on throwaway practice |
Self-check checklist
0 of 4 done
Try-at-home challenge
Add a second variable env with a validation block that only allows dev, stage or prod, and try terraform plan -var env=test to see the error.
Samajla ka? Write, init, plan, apply, destroy: the same loop for a file or a full VPC. Aata pudhe jaauya for_each.