Introduction to Terraform
What you'll learn
- •Understand what Terraform does and how it works
- •Install Terraform on your machine
- •Write your first Terraform configuration
- •Run terraform init, plan, and apply
How Terraform Works
Terraform follows a simple workflow:
- Write - Define resources in
.tffiles - Plan - Preview what Terraform will do
- Apply - Create, update, or delete resources
- Destroy - Clean up when done
Under the hood, Terraform:
- Reads your configuration files
- Compares desired state to actual state
- Calculates the minimal set of changes needed
- Executes those changes via cloud provider APIs
Note
Installing Terraform
macOS (with Homebrew)
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
Linux
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
Verify Installation
terraform version
You should see output like:
Terraform v1.6.0
on darwin_arm64
Your First Configuration
Create a new directory and file:
mkdir terraform-intro && cd terraform-intro
Create main.tf with this content:
terraform {
required_providers {
local = {
source = "hashicorp/local"
version = "~> 2.0"
}
}
}
resource "local_file" "hello" {
content = "Hello, Terraform!"
filename = "${path.module}/hello.txt"
}
This configuration uses the local provider to create a file. It's perfect for learning without needing cloud credentials.
The Terraform Workflow
Step 1: Initialize
terraform init
This downloads the required providers and sets up your working directory.
Step 2: Plan
terraform plan
Terraform shows you what it will do:
Terraform will perform the following actions:
# local_file.hello will be created
+ resource "local_file" "hello" {
+ content = "Hello, Terraform!"
+ filename = "./hello.txt"
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
Tip
Step 3: Apply
terraform apply
Terraform asks for confirmation, then creates the resource:
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
local_file.hello: Creating...
local_file.hello: Creation complete after 0s [id=...]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Check your directory - hello.txt now exists!
cat hello.txt
Step 4: Destroy (When Done)
terraform destroy
This removes all resources managed by this configuration.
Warning
Understanding State
Terraform keeps track of what it manages in a state file (terraform.tfstate). This file maps your configuration to real resources.
cat terraform.tfstate
You'll see JSON describing the resources Terraform created.
Danger
Practice Exercise
- Modify
main.tfto change the file content - Run
terraform plan- what does it show? - Run
terraform applyto update the file - Clean up with
terraform destroy
Key Takeaways
- Terraform is declarative: describe what you want, not how to build it
- The workflow is: init → plan → apply → (eventually) destroy
- State tracks the mapping between config and real resources
- Always review plans before applying
Next, we'll learn about providers and resources in depth.