Lesson 1 of 16
Section 1: Foundations, HCL2 Grammar & The Core Lifecycle25 min read & walkthrough

IaC Paradigms: Declarative State vs Imperative Automation

01 / PARADIGM
Declarative State

Define the desired end state. The engine figures out the diff and convergence path.

02 / ARCHITECTURE
Provider Plugin RPC

Terraform Core communicates with cloud providers via high-speed gRPC plugins.

03 / IDEMPOTENT
Safe Re-Runs

Applying the same configuration 100 times produces the exact identical infrastructure.

The Big Idea

In the early days of cloud computing, engineers automated infrastructure using imperative scripts: long Bash files running AWS CLI commands, Python scripts with Boto3, or manual web console clicking. While this works for spinning up a single development sandbox, it breaks down catastrophically in production.

Imperative automation tells the computer how to execute every step: 'create a security group, wait 10 seconds, create a subnet, create an EC2 instance'. If an API call times out at step 3, your script crashes, leaving half-baked, orphaned resources that cost money and prevent reruns. You cannot safely run the script twice because it attempts to recreate resources that already exist.

Terraform operates on the Declarative Paradigm: you declare what your desired final state looks like, not how to build it. When you execute terraform apply, the engine inspects your cloud account, compares reality against your code, calculates the exact mathematical set difference, and issues the minimal API calls required to converge reality to your code.

💡

Jargon Buster: Plain-English Translations

  • Declarative: Specifying the desired outcome without describing the individual procedural execution steps.
  • Imperative: Specifying step-by-step procedural instructions that execute sequentially regardless of current state.
  • Idempotence: The mathematical property where an operation can be applied multiple times without changing the result beyond the initial application.
  • Convergence: The process of bringing actual cloud infrastructure into alignment with the declared desired state in code.

Declarative Desired State vs Imperative Scripting

# Declarative: Terraform expresses desired topology
terraform {
  required_version = ">= 1.9.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.50"
    }
  }
}

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

# The engine reconciles this resource automatically
resource "aws_vpc" "production" {
  cidr_block           = "10.0.0.0/16"
  enable_dns_hostnames = true
  enable_dns_support   = true

  tags = {
    Name        = "production-core-vpc"
    Environment = "production"
    ManagedBy   = "terraform"
  }
}
⚠️

Beginner Traps & Gotchas

1. Treating Terraform like a procedural programming script

The Gotcha: Trying to force step-by-step execution ordering using sleep timers or sequential file naming.

The Fix: Let Terraform build the Directed Acyclic Graph (DAG) automatically based on resource attribute references.

2. Modifying infrastructure manually through cloud web consoles

The Gotcha: Making emergency firewall or instance changes in AWS Console, introducing silent state drift.

The Fix: Treat your code repository as the single source of truth. Always codify changes in HCL and apply via Terraform.