🏗️ Terraform Intro
Write code to create cloud servers. Stop clicking buttons.
📖 What is Terraform?
Terraform is a tool that lets you create cloud servers and services by writing code instead of clicking buttons in a web portal. This is called Infrastructure as Code (IaC).
Instead of calling a builder every time you want a house, you write a blueprint once. The builder follows the blueprint every time. Terraform is that blueprint for cloud servers.
🤔 Why Use Terraform?
- Repeatable: Create the same setup every time.
- Version control: Store your server setup in Git.
- Multi-cloud: Works with AWS, Azure, Google Cloud, and more.
- Safe: Plan changes before applying them.
- Team-friendly: Everyone uses the same setup.
🌍 Real Client Example
Client: A startup that needs a test environment every week for new features.
Problem: Every time they create a test server by hand, they forget a setting. Tests fail. Time is wasted.
Solution with Terraform:
- Write one Terraform file that describes the test server.
- Run
terraform applyto create it. - Run
terraform destroyto delete it when done.
Result: Every test server is exactly the same. No missed settings. No wasted time.
✍️ Your First Terraform File
This file creates a simple Azure resource group:
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "example" {
name = "my-resource-group"
location = "canadacentral"
tags = {
environment = "dev"
owner = "ali"
}
}
What this does:
provider— Tells Terraform to use Azure.resource— Defines something to create.name— The name of the resource group.location— The data center region.tags— Extra labels for tracking.
⌨️ Basic Terraform Commands
| Command | What It Does |
|---|---|
terraform init | Downloads the provider plugins. |
terraform plan | Shows what will change. No changes yet. |
terraform apply | Creates or updates resources. |
terraform destroy | Deletes all resources. |
terraform fmt | Formats your code. |
✅ What You Learned
- Terraform creates cloud servers with code.
- It is repeatable, safe, and works with many clouds.
- You write
.tffiles to describe what you want. - You use
plan,apply, anddestroyto manage resources.