A Algo3DVerse
04 — DevOps
DevOps — Lesson 4 of 6

Infrastructure
as Code

Provision servers, networks, and databases from configuration files — not clicking through cloud consoles. Watch terraform apply provision resources and detect drift in 3D.

Before IaC, infrastructure was "snowflakes" — manually configured servers impossible to reproduce exactly. When one broke, you rebuilt it from memory. IaC makes infrastructure version-controlled, reproducible, and reviewable — like code.

Provisioning Infrastructure Live

Three resource stacks: Network ? Compute ? Database. Press terraform apply to provision in dependency order. Press Drift Detected to see Terraform reconcile a manual change.

drag — scroll to zoom

Code example

Terraform HCL for AWS infrastructure and the core terraform workflow commands.

INFRASTRUCTURE AS CODE — TERRAFORM
# main.tf — provision AWS infrastructure
terraform {
  required_providers {
    aws = { source = "hashicorp/aws", version = "~> 5.0" }
  }
  backend "s3" {
    bucket         = "my-terraform-state"
    key            = "prod/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "terraform-locks"  # prevents concurrent apply
  }
}

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

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  name    = "prod-vpc"
  cidr    = "10.0.0.0/16"
  azs             = ["us-east-1a", "us-east-1b"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24"]
  enable_nat_gateway = true
}

resource "aws_instance" "api" {
  count         = 3
  ami           = "ami-0c02fb55956c7d316"
  instance_type = "t3.small"
  subnet_id     = module.vpc.private_subnets[count.index % 2]
  tags = { Name = "api-${count.index}", Env = var.environment }
}
# Initialize — download providers and modules
terraform init

# Preview what will change (always before apply)
terraform plan -out=tfplan

# Read the plan in human-friendly format
terraform show tfplan

# Apply the plan (prompts for confirmation)
terraform apply tfplan

# Check for drift between state and real infrastructure
terraform plan   # shows any divergence from config

# Tear down a specific resource (careful in prod)
terraform destroy -target=aws_instance.api[2]

# Format all .tf files consistently
terraform fmt -recursive

# Validate config syntax without hitting the API
terraform validate

# See the current state of a resource
terraform state show aws_instance.api[0]

Key concepts

📋

Declarative Config

desired state — HCL — idempotent

You describe what you want (3 EC2 instances, a VPC with CIDR 10.0.0.0/16), not how to create it. Terraform works out the API calls. Running apply twice is safe — idempotent by design.

💾

State File

tfstate — source of truth — remote

Terraform's record of what resources it manages and their real IDs. Store it remotely (S3 + DynamoDB lock) so your team shares the same state and concurrent applies don't corrupt it.

⚡

Plan / Apply / Destroy

workflow — safe — preview

The core workflow: plan (preview changes), apply (execute), destroy (tear down). Always run plan before apply in production. The plan output is the diff of your infrastructure.

🔍

Drift Detection

reconcile — audit — consistency

When real infrastructure diverges from IaC config due to manual console changes, that's drift. Terraform detects it on the next plan. Eliminating drift means infrastructure is always reproducible.

→ Kubernetes DevOps — Algo3DVerse Monitoring ?
Press ? to begin the narration walkthrough.
0 / 9