Resource Blocks and the Resource Lifecycle
What you'll learn: How Terraform manages resources from creation through updates to deletion using resource blocks.
What is a Resource Block?
A resource block is Terraform's way of declaring a piece of infrastructure you want to create and manage. You've already seen resource blocks in earlier lessons—now we'll understand what Terraform does with them over time.
resource "aws_instance" "web_server" {
ami = "ami-12345678"
instance_type = "t2.micro"
}
This declares your desired state. Terraform's job is to make reality match this declaration.
The Resource Lifecycle
Think of a resource like a plant you're caring for. Terraform manages it through three key phases:
1. Creation
When you first run terraform apply, Terraform creates the resource from scratch. Like planting a seed, the resource didn't exist before—now it does.
2. Updates (In-Place or Replacement)
If you change attributes in your resource block, Terraform detects the difference. Some changes can be applied in-place (like watering your plant). Others require replacement—destroying the old resource and creating a new one (like replanting entirely).
3. Deletion
When you remove a resource block or run terraform destroy, Terraform deletes the resource. The infrastructure is cleaned up and removed.
How Terraform Decides What to Do
Terraform compares your configuration to its state file. It calculates what needs to be created, updated, or destroyed to match your declared desired state. You see this plan during terraform plan before applying changes.
Key Takeaway: Resource blocks declare your infrastructure's desired state, and Terraform automatically manages the full lifecycle—creation, updates, and deletion—by comparing your configuration to reality stored in the state file.