Terraform by HashiCorp: Infrastructure as Code from First Principles

What Terraform is, how it works, and why it should be the foundation of every cloud engineering practice.

By Ali Bashir · DevOps · Jan 26, 2022 · 2 min read

Terraform is an infrastructure provisioning tool used for Infrastructure as Code (IaC) - meaning you create infrastructure on cloud platforms by writing code rather than clicking through consoles. Terraform can automate and manage infrastructure, platforms, and the services that run on them.

Why Terraform?

Terraform uses declarative language - you define the end result you want, not every step to get there. This means your configuration files describe infrastructure state, and Terraform figures out how to reach it.

Free to use (source-available under the Business Source License since 2023; OpenTofu is the open-source fork) and with a consistent CLI workflow, Terraform can manage hundreds of cloud services across any provider - AWS, Azure, GCP, and beyond.

Terraform vs Ansible

Both tools automate infrastructure, but they have different strengths:

Ansible

  • Primarily a configuration management tool
  • Agentless, runs over SSH
  • Better for configuring software on existing servers

Terraform

  • Primarily an infrastructure provisioning tool
  • Tracks what it created in a state file
  • Better for orchestrating cloud resources from scratch

In practice, many teams use both: Terraform to provision the infrastructure, Ansible to configure what runs on it.

How Terraform Works

Terraform reads configuration files written in HCL (HashiCorp Configuration Language), generates an execution plan, and applies changes to reach the desired state. It tracks what it has created using a state file - this is what lets it know what to update or destroy.

The Core Commands

# Initialize working directory and download providers
terraform init

# Preview changes before applying
terraform plan

# Apply the configuration
terraform apply

# Destroy all managed resources
terraform destroy

A Simple AWS Example

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

# Latest Amazon Linux 2023 AMI for the provider's region, published by AWS in SSM
data "aws_ssm_parameter" "al2023" {
  name = "/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64"
}

resource "aws_instance" "web" {
  ami           = data.aws_ssm_parameter.al2023.value
  instance_type = "t3.micro"

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

Running terraform apply against this configuration provisions the EC2 instance. Running terraform destroy removes it. The entire infrastructure lifecycle is version-controlled and reproducible.

Conclusion

Terraform is the foundation of modern cloud engineering. When your infrastructure is code, it can be reviewed, tested, versioned, and automated - the same discipline we apply to application code. That's what turns cloud platforms into reliable, scalable systems.