Terraform from scratch

Home

/

Blog

Terraform from scratch

This is not a crash course. This is not a tutorial. This document is written as a **continuous learning handbook** intended to be read over many days. Expect depth. Expect repetition. Expect real-world thinking.

Terraform is a foundational skill for modern infrastructure engineers. If you understand Terraform deeply, cloud providers become interchangeable, systems become predictable, and scaling stops being scary.


1. The Real Problem Terraform Solves

To understand Terraform, you must first understand the **pain it eliminates**. Before Infrastructure as Code, infrastructure lived in three places:

  • Human memory
  • Manual cloud consoles
  • Outdated documentation

This created environments where:

  • Production ? Staging
  • Hotfixes were undocumented
  • No one knew who changed what
  • Rebuilding was impossible under pressure

Terraform solves this by enforcing **source-controlled infrastructure truth**.

If infrastructure is not in code, it does not exist.

2. Infrastructure as Code - Beyond the Buzzword

Infrastructure as Code (IaC) is often misunderstood as "automation". Automation is only a side effect.

The real benefits are:

  • Reproducibility
  • Auditability
  • Predictability
  • Disaster recovery

With IaC, infrastructure becomes:

  • Versioned (Git)
  • Reviewable (Pull Requests)
  • Testable (Plans)

Terraform is a **declarative IaC engine**, meaning you describe the final state, not the steps to reach it.


3. Declarative vs Imperative - Why Terraform Wins

Imperative tools say:

"Create a server, then configure it, then attach storage."

Declarative tools say:

"I want one server with storage attached."

Terraform builds a dependency graph and figures out execution order automatically.

Trying to force Terraform into an imperative mindset is the #1 beginner mistake.

4. Terraform Architecture - How It Actually Works

Terraform has four major internal components:

  • Core engine
  • Providers
  • State management
  • Execution planner

When you run Terraform:

  • Configuration is parsed
  • Providers are initialized
  • State is loaded
  • A dependency graph is created
  • A plan is generated

5. Installing Terraform and Verifying Environment

After installation, always verify:

terraform version

In professional environments, Terraform versions are pinned to avoid breaking changes between releases.

Many companies use:

.tfenv-version

to enforce consistent Terraform versions across teams.


6. Understanding Terraform Configuration Files

Terraform configuration files use the HashiCorp Configuration Language (HCL). It is designed to be readable, structured, and machine-friendly.

Key characteristics of HCL:

  • Blocks define intent
  • Arguments define configuration
  • Expressions reference data

Example structure:

resource "aws_instance" "web" { ami = "ami-123456" instance_type = "t3.micro" }

This block defines a **desired object**, not a command.


7. Providers - Terraform's API Bridge

Providers allow Terraform to communicate with external systems such as AWS, Azure, GCP, Kubernetes, GitHub, and hundreds more.

Example AWS provider:

provider "aws" { region = "us-east-1" }

Providers manage:

  • Authentication
  • API requests
  • Resource lifecycle
Terraform itself does not know how to create servers. Providers do.

8. Resources - Real Infrastructure Objects

Resources are the heart of Terraform. Each resource represents a real object in the target system.

Examples of resources:

  • Virtual machines
  • Networks
  • Databases
  • Load balancers
  • DNS records

Example:

resource "aws_s3_bucket" "logs" { bucket = "company-prod-logs" acl = "private" }

Terraform tracks this resource using state.


9. Terraform Workflow - Professional Usage

The standard workflow:

terraform init terraform plan terraform apply

In enterprise environments:

  • Plan is reviewed
  • Apply is gated
  • Changes are logged
Running terraform apply without reviewing the plan is dangerous.

10. Terraform State - The Single Most Important Concept

Terraform state maps configuration to real infrastructure. It answers the question:

"What exists right now?"

Without state, Terraform cannot:

  • Detect drift
  • Plan changes
  • Safely update resources

State files must be:

  • Protected
  • Backed up
  • Locked

© SNA Mart. All Rights Reserved. Designed by HTML Codex