CodeForFinance
← Back to DevOps & Cloud

Infrastructure as Code with Terraform

Define your servers, databases, and networks in code. Version it, review it, automate it.

Why Infrastructure as Code?

Clicking through the AWS console is fine for learning. But in production, you need to know exactly what infrastructure exists, be able to recreate it in minutes, and track every change. That is what Infrastructure as Code (IaC) gives you.

Terraform is the most popular IaC tool. It works with AWS, Azure, GCP, Cloudflare, and hundreds of other providers. You write configuration files, Terraform figures out what needs to change, and applies those changes.

HCL - The Language

Terraform uses HCL (HashiCorp Configuration Language). It is declarative - you describe what you want, not how to create it. Terraform figures out the order of operations and dependencies automatically.

provider "aws" {
  region = "eu-west-2"
}

# Create a VPC
resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
  tags = {
    Name = "main-vpc"
  }
}

# Create a subnet
resource "aws_subnet" "web" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"
  tags = {
    Name = "web-subnet"
  }
}

# Create an EC2 instance
resource "aws_instance" "web" {
  ami           = "ami-0abcdef1234567890"
  instance_type = "t3.micro"
  subnet_id     = aws_subnet.web.id

  tags = {
    Name = "web-server"
  }
}

Notice how the subnet references the VPC with aws_vpc.main.id. Terraform understands this dependency and creates the VPC first.

Variables - Making it Reusable

Hard-coding values works for a demo. Real projects use variables so the same code works across staging, production, and different regions.

# variables.tf
variable "environment" {
  description = "Deployment environment"
  type        = string
  default     = "staging"
}

variable "instance_count" {
  description = "Number of instances"
  type        = number
  default     = 2
}

# main.tf - using variables
resource "aws_instance" "web" {
  count         = var.instance_count
  ami           = "ami-0abcdef1234567890"
  instance_type = "t3.micro"

  tags = {
    Name        = "web-${var.environment}-${count.index}"
    Environment = var.environment
  }
}

The Workflow: Init, Plan, Apply

Every Terraform workflow follows the same three steps. plan is the most important - it shows you exactly what will happen before anything changes. Always review the plan.

# Initialise Terraform (download providers)
terraform init

# Preview what will change
terraform plan

# Apply the changes
terraform apply

# Destroy everything (careful!)
terraform destroy

# Format your code
terraform fmt

# Validate configuration
terraform validate

Key Takeaways

  • - Terraform manages infrastructure across any cloud provider
  • - State files track what exists - store them remotely (S3 + DynamoDB)
  • - Always run plan before apply and review the output
  • - Use variables and modules to keep your code DRY and reusable
  • - Combine with CI/CD to automate infrastructure changes on merge

Developer Essentials

As an Amazon Associate we may earn from qualifying purchases.