🎓 A1Learner
🔍
0 of 50 lessons completed

What Is Infrastructure as Code?

Infrastructure as Code is the practice of describing infrastructure in files instead of building it by hand. The files are the record of what should exist.

Desired state in one file

The file says what should exist. It does not list every click needed to build it.

resource "local_file" "note" {
  filename = "note.txt"
  content  = "This file should exist."
}

Repeatability

A person clicking through a console may click differently each time. A file runs the same way each time.

terraform apply

Review before running

A teammate can read the file and spot a mistake. A finished console setup cannot be read back in the same way.

terraform plan

Example

# main.tf
terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "2.9.1"
    }
  }
}

resource "local_file" "note" {
  filename = "note.txt"
  content  = "This file should exist."
}

# Commands:
# terraform init
# terraform plan
# terraform apply

Key points

  • Desired state means the result you want, written down in advance.
  • Terraform compares the desired state with what exists today.
  • It then makes only the changes needed to reach the desired state.
  • A file can be reviewed before it runs, so mistakes are caught early.
  • The same file can build a test setup and a live setup.
  • Clicks in a console leave no record. Files leave a clear record.

The plan shows the desired state before anything changes. The file is the record, and the plan is the check. Apply then creates the file.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What is the main benefit of describing infrastructure in files?