What is Infrastructure as Code?

Infrastructure as Code (IaC) means describing servers, networks and databases in text files instead of clicking through a console.

Infrastructure as Code (IaC) is the practice of defining your cloud resources — servers, networks, databases, load balancers — in plain text files that live alongside your application code.

Why not just click in the console?

Manually creating resources in a web console works for one server, but it does not scale. Clicks are not repeatable, not reviewable, and impossible to audit six months later.

Benefits of IaC

  • Repeatable — run the same code to build identical environments (dev, staging, prod).
  • Versioned — store it in Git, review changes in pull requests, roll back mistakes.
  • Documented — the code is the documentation of what exists.
  • Automated — machines apply the changes, not error-prone humans.

Example

Example · hcl
# The whole idea of IaC: this small file describes a real cloud server.
resource "aws_instance" "web" {
  ami           = "ami-0c02fb55956c7d316"
  instance_type = "t3.micro"

  tags = {
    Name = "my-first-server"
  }
}

When to use it

  • A DevOps team stores AWS VPC and subnet definitions in Git so every infrastructure change is reviewed in a pull request.
  • A startup provisions identical staging and production environments by running the same IaC scripts against different variable files.
  • An SRE rebuilds a disaster-recovery environment in minutes by re-applying the checked-in infrastructure manifests after a region failure.

More examples

Simple IaC file concept

Shows how a text file declares an S3 bucket as infrastructure, replacing a manual console click.

Example · hcl
# This HCL file IS the infrastructure definition
resource "aws_s3_bucket" "logs" {
  bucket = "my-app-logs-2024"
  tags = {
    Environment = "production"
  }
}

Version-controlled infra change

Demonstrates committing an infrastructure file to Git, enabling code-review workflows for infra changes.

Example · bash
# Treat infra like application code
git init infra
cd infra
cat > main.tf <<'EOF'
resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.micro"
}
EOF
git add main.tf && git commit -m "Add web server resource"

Reproduce environment idempotently

IaC is idempotent: running it twice on a matching environment produces no changes, ensuring consistency across environments.

Example · bash
# Apply the same config to staging then production
export TF_VAR_env=staging
terraform apply -auto-approve

export TF_VAR_env=production
terraform apply -auto-approve

# Running apply again on an already-correct env changes nothing

Discussion

  • Be the first to comment on this lesson.