Guide

Infrastructure as code (IaC) stores cloud resources as declarations in a repository. They are reviewed in pull requests, applied by a pipeline, and checked against reality. Terraform is the most common tool for this across AWS, GCP and Azure. OpenTofu is an open-source fork with the same workflow.

You write the desired state in HCL. Terraform compares it with a state file that records what it created, prints a plan, and applies the difference through cloud APIs after approval.

Concepts in reading order

StepConceptWhat it answers
1Why infrastructure as codeWhy not click through the console?
2Plan and applyWhat will Terraform change, and how do you check it?
3State and lockingHow does Terraform know what it owns?
4Modules and environmentsHow do you reuse standards and keep dev apart from prod?
5Safe changesHow do you stop a bad apply from deleting a database?

Read step 1 first. Steps 2 to 5 assume you know what a plan and a state file are.

Alternatives

ToolStyleWhen it fits
Terraform or OpenTofuHCL, declarative, large provider ecosystemThe default for cloud and SaaS provisioning
PulumiTypeScript, Python or GoTeams that want real abstractions, tests and IDE support
AWS CDK or CDKTFCode that generates CloudFormation or TerraformDeep in one cloud, and a preference for TypeScript
CrossplaneKubernetes control plane for cloud resourcesPlatform teams that want kubectl apply as the only API

OpenTofu follows Terraform's language and providers under an open license, so the HCL skills transfer. Do not rewrite working Terraform only for fashion.

Common mistakes

  • Putting state in git. Rotate anything that leaked, and move to a remote backend the same day.
  • Drifting out of band. Console changes must be imported or reverted, or the next apply surprises someone.
  • Putting raw passwords in .tf or tfvars files. Use secret references instead. See secrets and encryption.
  • Confusing workspaces. One wrong select can aim an apply at the wrong environment.

In practice

Interviews often ask you to explain state and locking, the difference between an update and a replacement, how you would split prod and staging, and what prevent_destroy does. Being able to answer those with an example is the goal.

Key takeaways

  • IaC makes infrastructure reviewable, repeatable and diffable.
  • Read the plan for destroys and replacements before every apply.
  • Use a remote backend with locking. Treat state changes as data migrations.
  • Give each long-lived environment its own directory and state.
  • Plan in CI, apply the saved plan, and guard databases.

Further learning

HashiCorp's tutorials are the best hands-on path after this overview.

Terraform tutorials (HashiCorp)Explore Terraform product documentation, tutorials, and examples.Tutorials | Terraform | HashiCorp Developer OpenTofu documentationDocumentation | OpenTofuopentofu.org Terraform style guideLearn recommended style conventions for Terraform configuration and workflows.Style Guide - Configuration Language | Terraform | HashiCorp Developer