Medium java docker

What is infrastructure as code, and what are Terraform basics?

The 30 second answer

Infrastructure as code means the network, clusters and managed services are declared in files, reviewed and applied, instead of clicked in a console. Terraform is a common tool for that: I write .tf files, terraform plan shows the diff against state, and terraform apply makes the change. State records what Terraform already created, so it must be stored remotely and locked. I keep secrets out of state when I can, and I split stacks so a typo in one file does not recreate the database.

Could you say this in an interview?

Infrastructure as code (IaC) is the practice of describing infrastructure in versioned files so a change is reviewable and repeatable. Terraform, from HashiCorp, is one implementation: providers talk to APIs (AWS, GCP, Kubernetes), resources are declared, and a state file maps those declarations to real IDs.

The loop is:

  1. Write or change .tf files.
  2. terraform plan compares configuration with state and shows what would be added, changed or destroyed.
  3. terraform apply performs that plan.

State is sensitive and must not live only on a laptop. HashiCorp documents remote backends and state locking so two applies cannot race. I also treat a database as a carefully protected resource (prevent_destroy, a separate state) so an accidental apply cannot replace it.

terraform {
  required_version = ">= 1.5.0"
  backend "s3" {
    bucket = "shop-terraform"
    key    = "checkout/prod/terraform.tfstate"
    region = "eu-central-1"
  }
}

resource "aws_s3_bucket" "invoices" {
  bucket = "shop-invoices-prod"
}

Terraform is not a substitute for application CI, and it is not the only IaC tool (CloudFormation, Pulumi, Kubernetes manifests are others). I would say "IaC" first, then name Terraform as the tool I use for cloud resources.

Sources (3)
  1. Terraform: What is Terraformdeveloper.hashicorp.com
  2. Terraform: The plan and apply workflowdeveloper.hashicorp.com
  3. Terraform: Statedeveloper.hashicorp.com