Skip to content
$ lynuxpro
← Terraform

LESSON 1 OF 23 · BEGINNER · 16 MIN

Infrastructure as code

Free previewLesson 1 of the 2 free lessons in this Pro course.Unlock all 23

HOW IT WORKSClicking versus code

Console clickshard to repeat
1/5

Clicking in a console is slow, and nobody can say later what was clicked.

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 getWhat it means
Version controlEvery change is a Git commit with an author and a reason
ReviewA teammate reads the change in a merge request before it happens
RepeatabilityThe same files create the same infrastructure every time
SpeedA full environment in minutes, not days
DocumentationThe files are the documentation, and they cannot go out of date
Safe deletionTear 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.micro
SHELLbashutf-8 · 3L

Run 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"}
NORMALhclutf-8 · 4L

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:

  1. Terraform works with any platform that has an API. AWS, Azure, Google Cloud, Kubernetes, GitLab, Datadog, Cloudflare and thousands more.
  2. It remembers what it built in a file called the state. That is how it knows what to change next time.
  3. 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:

ToolStyleNotes
CloudFormationDeclarative, JSON/YAMLAWS only
Bicep / ARMDeclarativeAzure only
PulumiReal programming languagesMulti-cloud, like Terraform
AnsibleMostly imperativeBest for configuring servers, not creating clouds
CrossplaneDeclarative, inside KubernetesManages 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"}
NORMALhclutf-8 · 16L

It says: "Use the AWS provider, in Ireland. I want one S3 bucket called acme-logs-2026." The three blocks are:

BlockPurpose
terraformSettings for Terraform itself, such as which providers it needs
providerHow to talk to one platform (login, region)
resourceOne thing you want to exist

The loop you will repeat

Almost all Terraform work is the same four steps:

write  ->  init  ->  plan  ->  apply
OUTPUTterminalutf-8 · 1L
  1. Write or change .tf files.
  2. terraform init downloads providers. Once per project.
  3. terraform plan shows what would change. Nothing happens yet.
  4. terraform apply makes the changes, after you type yes.

Lesson 02 walks through this with real output.

Good habits from day one

  • Put every .tf file 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 .tf files. Lesson 04 and 06 show how.

Common mistakes

MistakeWhy it hurtsFix
Editing resources in the consoleState and reality disagree (drift)Change the code, then apply
Skipping the planA typo can delete a databaseAlways read the plan
One giant file for everythingHard to read and reviewSplit by purpose, lesson 11 onwards
Committing passwordsSecrets stay in Git history foreverUse 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. 1. What does 'declarative' mean in infrastructure as code?

  2. 2. A teammate changed a security group by hand in the AWS console. Your Terraform files did not change. What is this called?

  3. 3. Which job is Terraform best at?