Stage 4 · Provision
Terraform Fundamentals
Core Concepts
Resources, providers, state, and the execution plan — the four pillars every Terraform user must understand.
What Is Terraform?
Terraform is an open-source infrastructure as code tool created by HashiCorp. It lets you define, provision, and manage cloud infrastructure using a declarative configuration language called HCL (HashiCorp Configuration Language). Instead of clicking through a console or writing imperative scripts, you describe the desired end state and Terraform figures out how to get there.
Terraform is cloud-agnostic. The same workflow provisions AWS VPCs, Azure Kubernetes clusters, GCP databases, and even SaaS resources like Datadog or Cloudflare. This universality is its primary advantage over cloud-native tools like CloudFormation or ARM templates.
Resources
A resource is the most basic building block in Terraform. Each resource block describes one or more infrastructure objects — a virtual machine, a DNS record, a firewall rule, a database. Resources have a type (like aws_instance) and a local name that you choose.
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
tags = {
Name = "web-server"
}
}This block declares an AWS EC2 instance. aws_instance is the resource type, web is the local name, and the arguments inside the block configure the resource. Terraform tracks this as aws_instance.web in the state file.
The local name (web, database, primary) is how you reference the resource elsewhere in your configuration. Choose names that make the code self-documenting: aws_instance.web is clearer than aws_instance.rs1.
Providers
Providers are plugins that tell Terraform how to interact with a specific cloud platform, SaaS provider, or API. Each provider offers resources and data sources. You declare which providers your configuration requires in a terraform block.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "us-east-1"
}The required_providers block pins which provider source and version to use. The provider block configures the provider itself — in this case, setting the AWS region. Multiple provider blocks of the same type can exist with aliases for multi-region or multi-account setups.
State
Terraform maintains a state file that maps your configuration to real-world infrastructure. The state records the ID of each resource, its attributes, and its dependencies. Every terraform plan and terraform apply reads the state to determine what needs to change.
Terraform does not query your cloud provider to determine current state during planning. It reads the state file, compares it to your configuration, and produces a plan based on that comparison. This is why state corruption or drift can cause unexpected changes.
The Execution Plan
Before making any changes, Terraform generates an execution plan. The plan shows what will be created, modified, or destroyed. This is the core safety mechanism — you review the plan before approving changes.
$ terraform plan
Terraform will perform the following actions:
# aws_instance.web will be created
+ resource "aws_instance" "web" {
+ ami = "ami-0c55b159cbfafe1f0"
+ instance_type = "t3.micro"
+ id = (known after apply)
+ tags = {
+ "Name" = "web-server"
}
}
Plan: 1 to add, 0 to change, 0 to destroy.The + prefix means the resource will be created. A - prefix means it will be destroyed. A tilde ~ means it will be modified in place. A -/+ means it will be replaced (destroyed and recreated).
Resource Lifecycle
Every resource follows a lifecycle: create, update, and destroy. Terraform manages transitions between these states. You can customize lifecycle behavior using lifecycle blocks.
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
lifecycle {
create_before_destroy = true
prevent_destroy = false
ignore_changes = [tags]
}
}create_before_destroy ensures a new instance is created before the old one is terminated. prevent_destroy blocks accidental deletion. ignore_changes tells Terraform to skip certain attributes during diff — useful for tags managed externally.
The state file is JSON and technically editable, but manual edits can corrupt it and cause Terraform to make destructive changes. Always use terraform state commands or import blocks to modify state.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.