Terraform: Infrastructure as Code

Master Terraform for automated infrastructure provisioning, state management, and multi-cloud deployments

40 min read
Not Started
Loading...

What is Terraform?

Terraform is an open-source infrastructure as code (IaC) tool created by HashiCorp. It allows you to define and provision infrastructure using a declarative configuration language called HCL (HashiCorp Configuration Language). Terraform can manage infrastructure across multiple cloud providers and services.

Unlike imperative approaches where you specify step-by-step instructions, Terraform uses a declarative approach where you describe the desired end state, and Terraform figures out how to achieve it while tracking the actual state of your infrastructure.

Terraform Project Calculator

15
Complexity Score
70s
Plan Time
85%
State Efficiency
68%
Team Collaboration

Apply Time: ~175s | Risk Level: Low

Recommended Modules: 4

Terraform Core Workflow

1. Write

Define infrastructure in configuration files using HCL.

Resource Declaration
resource "aws_instance" "web" {
  ami = "ami-12345"
  instance_type = "t3.micro"
}

2. Plan

Preview changes before applying them to infrastructure.

$ terraform plan
+ aws_instance.web will be created
Plan: 1 to add, 0 to change, 0 to destroy

3. Apply

Execute the planned changes to provision infrastructure.

$ terraform apply
aws_instance.web: Creating...
Apply complete! Resources: 1 added

4. Track State

Terraform maintains state to track managed resources.

Terraform Show Output
$ terraform show
# aws_instance.web:
resource "aws_instance" "web" {
  id = "i-1234567890abcdef0"
}

Terraform Configuration Patterns

Provider Configuration

Provider Configuration
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = var.aws_region
}

Module Usage

Module Usage
module "vpc" {
  source = "terraform-aws-modules/vpc/aws"
  version = "3.0.0"

  name = "my-vpc"
  cidr = "10.0.0.0/16"
  azs = ["us-west-2a", "us-west-2b"]
  public_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
}

Remote State Configuration

Remote State Backend
terraform {
  backend "s3" {
    bucket = "my-terraform-state"
    key = "prod/terraform.tfstate"
    region = "us-west-2"
    dynamodb_table = "terraform-locks"
    encrypt = true
  }
}

Real-World Terraform Implementations

Uber

Manages thousands of resources across multiple AWS regions with Terraform.

  • • Multi-region EKS clusters
  • • RDS databases and Redis clusters
  • • Load balancers and VPC networking
  • • 10,000+ resources managed

Datadog

Uses Terraform for multi-cloud infrastructure provisioning and monitoring setup.

  • • AWS, GCP, and Azure resources
  • • Monitoring infrastructure automation
  • • CI/CD pipeline integration
  • • Self-service infrastructure for teams

Gruntwork

Builds production-ready Terraform modules for enterprise clients.

  • • Reusable infrastructure modules
  • • Security and compliance automation
  • • Multi-account AWS setups
  • • 200+ open source modules

GitLab

Manages their SaaS infrastructure and customer environments with Terraform.

  • • GCP and AWS hybrid infrastructure
  • • Kubernetes cluster provisioning
  • • Customer environment automation
  • • Disaster recovery automation

Terraform Best Practices

✅ Do

  • • Use remote state with locking for teams
  • • Structure code with modules for reusability
  • • Always run terraform plan before apply
  • • Use version constraints for providers
  • • Implement proper tagging strategies
  • • Use terraform fmt and validate regularly

❌ Don't

  • • Store state files in version control
  • • Hardcode sensitive values in configurations
  • • Make manual changes to Terraform-managed resources
  • • Skip terraform plan in CI/CD pipelines
  • • Use overly complex expressions
  • • Ignore drift detection and remediation

📝 Terraform Knowledge Quiz

1 of 6Current: 0/6

What is the primary purpose of Terraform state?