Infrastructure as Code and Terraform
Infrastructure as Code and Terraform
Treating Infrastructure Like Software
Infrastructure as Code (IaC) manages servers, networks, and databases the way applications manage code: in version-controlled files, reviewable and repeatable. Instead of clicking through a cloud console, you describe the desired infrastructure in text, and a tool makes it real. Anyone who runs the files gets the same environment, every time.
Why IaC Wins
- Repeatability: the same files rebuild identical environments forever.
- Review: infrastructure changes go through pull requests like code.
- Speed: a full environment spins up in minutes, not days of clicking.
- Disaster recovery: an environment lost is an environment rebuilt from files.
Terraform in One Idea
Terraform is the leading IaC tool. It connects to providers such as AWS, Azure, Google Cloud, and even Docker. You write resources in declarative HCL, and Terraform creates, updates, or removes exactly what requires change to reach the desired state.
A Minimal Example
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
tags = {
Name = "web-server"
}
}
This declares one server. The block states what the resource is (aws_instance), what it is called locally (web), and its configuration, including the machine image and size.
The Plan-Apply Loop
terraform init
terraform plan
terraform apply
terraform destroy
init downloads providers, plan prints a dry run of changes, apply executes them, and destroy tears everything down. Separating plan from apply is Terraform's safety valve: you see the exact diff before touching anything.
State matters: Terraform stores the relationship between your files and real resources in a state file, so treat it as precious and store it remotely for teams.
Key Points
- IaC manages infrastructure through version-controlled files.
- Terraform is declarative: files describe desired state, and Terraform converges to it.
- Providers connect Terraform to AWS, Azure, GCP, Docker, and more.
- plan previews the diff; apply executes it; destroy removes everything.
- Store state remotely and treat it as a critical asset.