Module 6 · Project 06 · Build AWS Infrastructure with Terraform · Lesson 22 of 48
Plan, apply, state and drift
The Terraform workflow, what the state file is and where to keep it, and how variables, outputs and modules keep code reusable.
In this lesson · about 30 minutes
- Use init, plan, apply and destroy, and read a plan's + ~ - symbols
- Explain state, remote state and locking, and detect drift
- Use variables, outputs and modules for dev, staging and production
The workflow
terraform init: download providers and set up the state backend.terraform plan: compare your code with what exists and print what would change. Read it every time.terraform apply: make those changes (it shows the plan again and asks for "yes").terraform destroy: delete everything in this configuration. Great for practice environments, so you stop paying.
In a plan, + means create, ~ update in place, - destroy, and -/+ replace (destroy and recreate, because that setting can't be changed on a live resource). A surprise -/+ on a database is how data gets lost, which is why you read plans.
State
Terraform keeps a state file that maps each resource in your code to the real thing it created (aws_instance.web → i-0a1b2c…). Without it, Terraform wouldn't know what it manages.
- State can contain secrets, so never commit it to Git.
- Teams keep it in a remote backend, such as an S3 bucket with versioning, plus locking so two people can't apply at the same time.
- When someone changes a resource by hand, reality no longer matches the code: that's drift. The next plan shows it, and apply puts it back to what the code says.
Try it · watch it run
Terraform: plan, apply and state
Plan and apply the network and server. Then change the instance size, add a database, cause drift in the console, and read each plan before applying.
Edit the code (main.tf)
Apply is only enabled right after a plan: always read the plan first.
AWS account (what really exists) · state file tracks 0 resources
VPC 10.0.0.0/16
aws_vpc.main
Internet gateway
aws_internet_gateway.gw
Public subnet A
aws_subnet.public_a
Private subnet A
aws_subnet.private_a
Public route table
aws_route_table.public
Security group: 80, 443
aws_security_group.web
EC2 web (t3.micro)
aws_instance.web
$ terraform init Terraform has been successfully initialized!
Variables, outputs and modules
# variables.tf
variable "region" { type = string, default = "us-east-1" }
variable "environment" { type = string }
variable "instance_type" { type = string, default = "t3.micro" }
# outputs.tf
output "alb_dns_name" { value = aws_lb.web.dns_name }
# environments/staging/main.tf
module "network" {
source = "../../modules/network"
cidr_block = "10.1.0.0/16"
environment = "staging"
}
module "compute" {
source = "../../modules/compute"
subnet_ids = module.network.private_subnet_ids # one module's output feeds another
instance_type = "t3.small"
}
- Variables are the inputs; outputs print useful values (like the load balancer's address) and pass them between modules.
- A module is a folder of Terraform code you can reuse:
network/,compute/,database/,security/. - Each environment is a small folder that calls the same modules with different inputs and its own state, so a mistake in dev can't touch production. The same idea as Project 02's inventories.
Format and check code with terraform fmt and terraform validate, and run them in CI (Project 04), with the plan posted on the pull request for review.
Hands-on lab (simulated terminal)
Lab: plan and apply a VPC
A folder with the files above and AWS credentials for a practice account.
Step 1 of 5. Type each command and press Enter. Nothing runs on your computer.
1. Initialize the folder.
Check your understanding0 of 3 correct
Key terms
Say the meaning to yourself, then tap a card to check.
Quiz score: 0 of 3
Written by Noviq Academy in its own words. The labs are simulations: no real servers are touched. When you build the projects yourself, use a practice virtual machine or a free-tier cloud account you control, watch your cloud spending, and never run cleanup or delete commands on a machine you care about. Tool names belong to their owners; versions and output are shown for learning and may differ slightly from yours. Read-aloud and the whiteboard voice use your device's built-in voices.