So far in this challenge, I've built containers, set up CI/CD with Jenkins, and pushed images to Docker Hub.
But there was one missing piece: how do I create the actual servers where all this runs?
Today, I started learning Terraform, a tool that lets you create infrastructure (servers, networks, databases) using code instead of clicking around in a web console.
No more "ClickOps". No more "it works on my machine". Just code that creates real infrastructure.
First: What is Infrastructure as Code (IaC)?
The Manual Way (ClickOps)
AWS Console -> Click "Launch Instance" -> Choose settings -> Click "Create"
EC2 Console -> Click "Create Security Group" -> Add rules -> Click "Create"
VPC Console -> Click "Create VPC" -> Configure -> Click "Create"
The problems:
- No record of what you created
- Can't reproduce exactly the same way twice
- Easy to make mistakes
- No version control
- Only the person who clicked knows what happened
The IaC Way (Terraform)
# main.tf
resource "aws_instance" "my_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
The benefits:
- Code is documentation (everyone can see what exists)
- Version control with Git (track every change)
- Repeatable everywhere (dev, staging, production)
- Easy to review changes before applying
- No human error from clicking wrong buttons
What is Terraform?
Terraform is an open-source tool created by HashiCorp that lets you define infrastructure as code and then creates that infrastructure for you.
The Analogy
| Concept | Analogy |
|---|---|
| Terraform code | A blueprint for a house |
| Terraform plan | Walking through the blueprint before building |
| Terraform apply | Actually building the house |
| Terraform destroy | Demolishing the house |
| State file | The house's deed (records what was built) |
Which Providers Does Terraform Support?
Terraform works with almost EVERY cloud provider:
| Provider | What it creates |
|---|---|
| AWS | EC2 servers, VPC networks, S3 storage |
| Azure | Virtual machines, databases, storage |
| Google Cloud | Compute instances, Kubernetes clusters |
| Kubernetes | Pods, services, deployments |
| Docker | Containers, images, networks |
| Local | Files on your computer (what we'll use today) |
Terraform Core Commands
The Workflow
Write Code → init → plan → apply → destroy
(main.tf) (setup) (preview) (create) (cleanup)
Command 1: terraform init
What it does: Prepares your working directory for Terraform.
terraform init
What happens:
- Downloads required provider plugins (AWS, Azure, Local, etc.)
- Sets up backend for storing state
- Initializes modules
Example output:
Initializing provider plugins...
- Finding latest version of hashicorp/local...
- Installing hashicorp/local v2.8.0...
- Installed hashicorp/local v2.8.0 (signed by HashiCorp)
Terraform has been successfully initialized!
When to use: First time in a folder, or when you add new providers.
Command 2: terraform plan
What it does: Shows what Terraform WILL do without actually doing it.
terraform plan
What happens:
- Compares your code against existing infrastructure
- Shows what will be added, changed, or deleted
- Does NOT make any changes (safe to run anytime)
Example output:
Terraform will perform the following actions:
+ resource "local_file" "hello" {
+ content = "Hello from Terraform!"
+ filename = "./hello.txt"
}
Plan: 1 to add, 0 to change, 0 to destroy.
When to use: Always run this BEFORE apply to review what will change.
Command 3: terraform apply
What it does: Actually creates or updates infrastructure.
terraform apply
What happens:
- Shows you the plan
- Asks for confirmation (
yesto proceed) - Creates the resources
Example output:
Do you want to perform these actions?
Enter a value: yes
local_file.hello: Creating...
local_file.hello: Creation complete
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
When to use: After reviewing the plan, to actually create resources.
Command 4: terraform destroy
What it does: Removes all infrastructure created by Terraform.
terraform destroy
What happens:
- Shows what will be destroyed
- Asks for confirmation
- Deletes everything
Example output:
Do you want to destroy these resources? Enter a value: yes
local_file.hello: Destroying...
local_file.hello: Destruction complete
Destroy complete! Resources: 1 destroyed.
When to use: When you want to clean up and avoid ongoing charges.
Your First Terraform Project
Step 1: Install Terraform
On Ubuntu/Kali Linux:
sudo snap install terraform --classic
terraform --version
Step 2: Create a Project Folder
mkdir -p ~/Desktop/terraform-demo
cd ~/Desktop/terraform-demo
Step 3: Create main.tf
nano main.tf
Step 4: Add the Terraform Code
resource "local_file" "hello" {
content = "Hello from Terraform!"
filename = "${path.module}/hello.txt"
}
Understanding the code:
| Part | Meaning |
|---|---|
resource |
"I want to create something" |
"local_file" |
The type of resource (a file on my computer) |
"hello" |
A name I give this resource (can be anything) |
content |
What text to put inside the file |
filename |
Where to save the file (./hello.txt means current folder) |
${path.module} |
Terraform variable meaning "current folder" |
Step 5: Initialize Terraform
terraform init
What this does: Downloads the "local" provider plugin so Terraform can create files.
Step 6: Preview the Plan
terraform plan
What this does: Shows you what Terraform WILL create without actually creating it.
You should see:
Terraform will perform the following actions:
+ resource "local_file" "hello" {
+ content = "Hello from Terraform!"
+ filename = "./hello.txt"
}
Plan: 1 to add, 0 to change, 0 to destroy.
Step 7: Apply the Configuration
terraform apply
When prompted, type: yes
What this does: Actually creates the file.
Step 8: Verify the File Was Created
ls -la
cat hello.txt
You should see: Hello from Terraform!
Step 9: Check Terraform State
terraform show
terraform state list
What this does: Shows what Terraform is tracking.
Step 10: Clean Up (Optional)
terraform destroy
Type: yes
What this does: Removes the file.
Understanding Terraform Files
When you run Terraform, it creates several files:
| File | Purpose | Should you edit it? |
|---|---|---|
main.tf |
Your infrastructure code | YES (this is where you write code) |
terraform.tfstate |
Current state of infrastructure | NO (Terraform manages this) |
terraform.tfstate.backup |
Backup of previous state | NO |
.terraform/ |
Provider plugins | NO |
.terraform.lock.hcl |
Locks provider versions | NO |
The State File is IMPORTANT
What it contains:
{
"version": 4,
"resources": [
{
"mode": "managed",
"type": "local_file",
"name": "hello",
"instances": [{
"attributes": {
"content": "Hello from Terraform!",
"filename": "./hello.txt"
}
}]
}
]
}
Why it matters: Terraform uses this file to track what it created. Without it, Terraform doesn't know what exists.
Best practice: Store state files remotely (S3, Azure Storage) when working with a team.
Terraform Commands Cheat Sheet
| Command | What it does | When to use |
|---|---|---|
terraform init |
Downloads plugins, sets up backend | First time in a folder |
terraform plan |
Previews what will change | Before every apply |
terraform apply |
Creates/updates resources | After reviewing plan |
terraform destroy |
Deletes all resources | When cleaning up |
terraform fmt |
Formats code consistently | Before committing |
terraform validate |
Checks if code is valid | After writing code |
terraform show |
Shows current state | To see what exists |
terraform state list |
Lists resources in state | To see tracked resources |
Terraform vs Other DevOps Tools
| Tool | Purpose | Where it fits |
|---|---|---|
| Terraform | Create infrastructure (servers, networks) | Day 0: Provision resources |
| Ansible | Configure existing servers (install software) | Day 1: Configure the servers |
| Docker | Package applications into containers | Build phase |
| Jenkins | Automate builds and deployments | CI/CD pipeline |
| Kubernetes | Run and scale containers | Run phase |
They work together in this flow:
Terraform creates the server
↓
Ansible configures the server (installs Docker)
↓
Jenkins builds and pushes Docker images
↓
Docker runs the containers
↓
Kubernetes orchestrates multiple containers
Key Takeaways
| Concept | Explanation |
|---|---|
| Infrastructure as Code | Define servers, networks, databases in code |
| Terraform | Tool that turns code into cloud resources |
init |
Sets up Terraform in a directory |
plan |
Previews what will change (safe to run anytime) |
apply |
Actually creates/changes resources |
destroy |
Removes everything (be careful!) |
| State file | Tracks what Terraform created (don't edit manually) |
What's Next
Now that you understand basic Terraform commands, tomorrow we will:
- Create real infrastructure in the cloud (AWS)
- Provision a server to run your Docker containers
- Combine Terraform with your existing CI/CD pipeline
From local files to actual cloud servers!
Resources
- Terraform Documentation
- Terraform CLI Commands
- Local Provider Documentation
- Terraform Best Practices
Let's Connect!
Have you used Terraform before? What infrastructure have you coded?
Drop a comment or connect on LinkedIn. Let's learn together!
Top comments (0)