Skip to main content
Cloud basicsLesson 16 of 17

Infrastructure as Code with Terraform

Clicking through a cloud console works once. It is hard to review, hard to repeat and easy to forget. With infrastructure as code (IaC), you write the servers and firewalls you want in files and let a tool make the cloud match. Follow code → init → plan → apply → state with Terraform. OpenTofu, an open-source fork, uses the same files and commands (tofu instead of terraform). The network being built here is explained in How a Request Travels Inside a VPC.

Command—
Plan—
State lock—
Drift—
Cloud accountEngineerLaptop or CICode (.tf)Git repositoryterraforminit · plan · applyStateRemote backendProvidere.g. aws pluginCloud APICreate · updateVM webInstanceSecurity grpPort 443ColleagueConsole clickCommit
RequestResponse / reportWatchAppears in a later step

What you will learn

  • Declarative code: you describe the result, not the clicks
  • What init, plan and apply each do
  • Why state lives in a remote backend with a lock
  • How a plan reveals drift from console changes
  • Why -/+ (replace) is more dangerous than ~ (update)

Reading plan symbols​

SymbolActionMeaning
+createThe resource is in the code but not in the state. It will be created
-destroyThe resource is in the state but no longer in the code. It will be deleted
~update in placeSome attributes change on the existing object. Its ID stays the same
-/+replaceDestroy first, then create a new object. Look for # forces replacement
+/-replace (create first)create_before_destroy is set: the new object is created before the old one is deleted
<=readA data source is read during apply because it depends on values not known yet

The last line of every plan sums it up: Plan: X to add, Y to change, Z to destroy. Check Z before anything else.

Try it yourself​

terraform init # Download providers, set up the backend
terraform fmt -check && terraform validate # Style and syntax
terraform plan -out=tfplan # Diff code vs state vs cloud, save the plan
terraform show tfplan # Review the saved plan
terraform apply tfplan # Apply exactly that plan
terraform plan -refresh-only # Only show drift from changes made outside Terraform
terraform state list # Resources Terraform manages
terraform state show aws_instance.web # Recorded attributes and real ID
terraform destroy # Delete everything in the state (asks first)
# OpenTofu: the same commands with tofu instead of terraform

Check yourself

  1. A colleague opened port 22 on the security group in the console. The next plan proposes ~ update in place to remove it. What happens if you apply it without thinking?
  2. Changing a database's attribute shows -/+ and "# forces replacement" in the plan. What does that mean?
  3. Two engineers run terraform apply at the same time against a remote backend with locking. What happens?

Further reading: What is Terraform? · terraform plan · State · State locking · The lifecycle meta-argument · OpenTofu docs · How a Request Travels Inside a VPC · Permission Checks on One API Request (IAM) · Helm and GitOps