Every application runs on infrastructure: networks, servers, databases, load balancers, DNS records, permissions. For a long time people built these by clicking through a cloud console. That works for one server. It falls apart for fifty. This lesson explains the idea that replaced clicking, called infrastructure as code, and where Terraform fits.
In simple words
- Infrastructure as code (IaC): describing servers, networks and databases in code files, so they can be created, reviewed and repeated exactly, instead of clicking in a console.
- Terraform: a popular IaC tool. You write what you want in HCL files, and it creates or changes the real cloud resources to match.
- Declarative: you describe the end result, and the tool works out the steps.
- Provisioning vs configuration: provisioning creates the infrastructure (Terraform). Configuration sets up software inside it (Ansible).
- Immutable infrastructure: replacing resources with new ones instead of changing them in place.
The problem with clicking
Imagine you build a test environment in the AWS console. It takes an afternoon. Three months later, someone asks for the same thing for a new team. Now you have questions:
- Which settings did you change? Did you remember all of them?
- Who created the security group, and why is port 8080 open?
- Production was built by a different person. Is it the same as test?
- The environment broke. Can you rebuild it quickly?
Clicking leaves no record. Two environments that should match slowly drift apart. This is called configuration drift, and it causes outages that nobody can explain.
The idea
Infrastructure as code (IaC) means you describe your infrastructure in text files and let a tool create it from those files.
Because the description is a file, you get everything you already use for software:
| You get | What it means |
|---|---|
| Version control | Every change is a Git commit with an author and a reason |
| Review | A teammate reads the change in a merge request before it happens |
| Repeatability | The same files create the same infrastructure every time |
| Speed | A full environment in minutes, not days |
| Documentation | The files are the documentation, and they cannot go out of date |
| Safe deletion | Tear down a test environment with one command |
Imperative and declarative
There are two ways to write down infrastructure.
Imperative means you list the steps:
aws ec2 create-security-group --group-name web --description "web"aws ec2 authorize-security-group-ingress --group-name web --port 443 --cidr 0.0.0.0/0aws ec2 run-instances --image-id ami-0abc --instance-type t3.microRun this twice and you get two of everything, or an error. The script does not know what already exists.
Declarative means you describe the end result and the tool works out the steps:
resource "aws_instance" "web" { ami = "ami-0abc" instance_type = "t3.micro"}This says "I want one instance like this." If it already exists, nothing happens. If it exists but is a different size, the tool changes it. If it does not exist, the tool creates it. Terraform is declarative.
What Terraform is
Terraform is a tool from HashiCorp that reads .tf files written in a language called HCL (HashiCorp Configuration Language) and creates, changes or deletes real infrastructure to match.
Three facts to remember:
- Terraform works with any platform that has an API. AWS, Azure, Google Cloud, Kubernetes, GitLab, Datadog, Cloudflare and thousands more.
- It remembers what it built in a file called the state. That is how it knows what to change next time.
- It always shows a plan before it changes anything, so you can check it first.
Terraform, OpenTofu and others
In 2023 HashiCorp changed Terraform's licence. The community made a fork called OpenTofu, now run by the Linux Foundation. It uses the same HCL, the same providers and almost the same commands (tofu instead of terraform). Everything in this course works in both. Lesson 23 covers the differences.
Other tools you may meet:
| Tool | Style | Notes |
|---|---|---|
| CloudFormation | Declarative, JSON/YAML | AWS only |
| Bicep / ARM | Declarative | Azure only |
| Pulumi | Real programming languages | Multi-cloud, like Terraform |
| Ansible | Mostly imperative | Best for configuring servers, not creating clouds |
| Crossplane | Declarative, inside Kubernetes | Manages cloud resources as Kubernetes objects |
Provisioning and configuration
Two jobs get mixed up:
- Provisioning is creating the things: a VM, a network, a database. Terraform is strong here.
- Configuration management is setting up what runs inside them: install nginx, edit a config file. Ansible is strong here.
A common setup is Terraform to make the servers, and either a ready-made image or Ansible to set them up.
Mutable and immutable
- Mutable infrastructure is changed in place. You log in and upgrade a package. Over time every server is slightly different.
- Immutable infrastructure is never changed. To update, you build a new one from a fresh image and delete the old one.
Terraform nudges you toward immutable: change the image ID, apply, and the instance is replaced. Treat servers as cattle, not pets.
Your first look
Here is a complete, tiny Terraform file. Do not worry about the details yet. Read it like English.
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } }} provider "aws" { region = "eu-west-1"} resource "aws_s3_bucket" "logs" { bucket = "acme-logs-2026"}It says: "Use the AWS provider, in Ireland. I want one S3 bucket called acme-logs-2026." The three blocks are:
| Block | Purpose |
|---|---|
terraform | Settings for Terraform itself, such as which providers it needs |
provider | How to talk to one platform (login, region) |
resource | One thing you want to exist |
The loop you will repeat
Almost all Terraform work is the same four steps:
write -> init -> plan -> apply- Write or change
.tffiles. terraform initdownloads providers. Once per project.terraform planshows what would change. Nothing happens yet.terraform applymakes the changes, after you typeyes.
Lesson 02 walks through this with real output.
Good habits from day one
- Put every
.tffile in Git. Never keep the only copy on your laptop. - Never change Terraform-managed things by hand in the console. Terraform will not know, and the next apply may undo or fight your change.
- Read every plan before applying. The plan is your safety net.
- Keep secrets (passwords, keys) out of
.tffiles. Lesson 04 and 06 show how.
Common mistakes
| Mistake | Why it hurts | Fix |
|---|---|---|
| Editing resources in the console | State and reality disagree (drift) | Change the code, then apply |
| Skipping the plan | A typo can delete a database | Always read the plan |
| One giant file for everything | Hard to read and review | Split by purpose, lesson 11 onwards |
| Committing passwords | Secrets stay in Git history forever | Use variables, secret managers |
Summary
- Infrastructure as code describes infrastructure in text files that live in Git.
- Declarative means you state the result and the tool finds the steps.
- Terraform reads HCL files, talks to platforms through providers, and remembers its work in state.
- OpenTofu is a compatible open-source fork; the skills transfer fully.
- The daily loop is write, init, plan, apply.
CHECK YOUR UNDERSTANDING
3 quick questions
1. What does 'declarative' mean in infrastructure as code?
2. A teammate changed a security group by hand in the AWS console. Your Terraform files did not change. What is this called?
3. Which job is Terraform best at?