DevOps Engineer · Step 6 of 8 · 8 min
Infrastructure as code
Describe servers, networks and services in files you can review, repeat and roll back.
Clicking does not scale
You can build infrastructure by clicking in a cloud console. It is fine for learning, but nobody can review it, repeat it exactly or see what changed last Friday. Infrastructure as code, IaC, means describing infrastructure in text files kept in Git.
Declare the end state
Most IaC tools are declarative: you describe what should exist, and the tool works out how to get there. You run a plan that shows the changes, review it, then apply it.
Always read the plan. A plan that says it will destroy and recreate a database is a warning, not a detail.
The main tools
Terraform from HashiCorp is widely used across clouds. Its licence changed in 2023, and the community created OpenTofu, an open-source fork under the Linux Foundation that works in much the same way. Pulumi lets you use general programming languages. Each cloud also has its own: AWS CloudFormation, Azure Bicep, and Google Cloud's Infrastructure Manager.
For configuring software on machines, Ansible is common. For Kubernetes, teams often keep YAML in Git and let a tool sync it to the cluster, called GitOps.
State and safety
Tools like Terraform keep a state file that records what they manage. It can contain secrets, so store it in a protected remote backend, not in Git. Make changes through the code only; manual changes in the console cause drift that surprises the next person.
Try it
Run IaC with no cloud bill
- 1.Install OpenTofu or Terraform on your machine.
- 2.Ask a chatbot the prompt below. It uses only the local provider, so nothing is created in any cloud.
- 3.Run init, plan and apply. Change the file contents in the code and run plan again to see the difference.
- 4.Run destroy and confirm the file is gone.
Write a minimal OpenTofu or Terraform configuration that uses only the hashicorp/local provider to create a text file. Explain init, plan, apply and destroy, and what the state file records.