Advanced

🏗️ 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).

💡 Simple Example:
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:

  1. Write one Terraform file that describes the test server.
  2. Run terraform apply to create it.
  3. Run terraform destroy to 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

CommandWhat It Does
terraform initDownloads the provider plugins.
terraform planShows what will change. No changes yet.
terraform applyCreates or updates resources.
terraform destroyDeletes all resources.
terraform fmtFormats your code.

✅ What You Learned

  • Terraform creates cloud servers with code.
  • It is repeatable, safe, and works with many clouds.
  • You write .tf files to describe what you want.
  • You use plan, apply, and destroy to manage resources.