Lesson 1 of 7 in Infrastructure as Code with Terraform and OpenTofu

What Infrastructure as Code means

Core7 min

Infrastructure as Code (IaC) means you describe the infrastructure you want in text files and let a tool create or change the real thing to match. The files live in Git like any other code, so changes are reviewed, repeatable and traceable.

Draft. This lesson is written and waiting for a person to run every command and review it. It is safe to learn from, but check the official documentation for anything you depend on.

Read these first: Cloud fundamentals with AWS, Git and collaboration

The problem this solves

Building infrastructure by clicking through a cloud console works the first time. The second time you forget a setting. The third time a colleague builds it slightly differently, and now staging and production disagree in ways nobody wrote down.

Scripts help, but a typical script says how to do things: create this, then add that. Run it twice and it may fail or create duplicates, because it does not know what already exists.

IaC tools such as Terraform and OpenTofu take a different approach. You describe the end result, and the tool works out what has to change to get there. That style is called declarative. The step-by-step style is called imperative.

How it works

From files in Git to real infrastructure
From files in Git to real infrastructureYou edit configuration files, commit them to Git, and an IaC tool reads them and creates or changes the real infrastructure to match.1 commit2 readconfig3 create orchangeYouedit the filesGit reporeviewed historyTerraform/OpenTofureads and comparesReal infrastructureservers, networks, files
  • People and clients
  • Data
  • Control
  • Runs code

You edit configuration files, commit them to Git, and an IaC tool reads them and creates or changes the real infrastructure to match.

  1. You to Git repo: 1 commit
  2. Git repo to Terraform/OpenTofu: 2 read config
  3. Terraform/OpenTofu to Real infrastructure: 3 create or change

The configuration files are the single description of what should exist. Nobody has to remember which buttons were clicked, because the answer is in the repository.

The tool compares what the files say with what exists and changes only the difference. Running it again with no edits changes nothing. That property is called idempotence, and it is what makes declarative tools safe to run repeatedly.

Terraform and OpenTofu talk to many kinds of targets through plugins called providers. Cloud providers for AWS, Azure or Google Cloud are the best known, but there are providers for DNS, Kubernetes, GitHub and even local files. This course uses local ones so you need no account.

Try it yourself

Run this in a Linux or macOS terminal (or WSL). Everything happens inside a throwaway folder in your home directory.

  1. 1
    See the imperative problem
    mkdir -p ~/iac-lab/intro && cd ~/iac-lab/intro
    echo hello >> notes.txt
    echo hello >> notes.txt
    cat notes.txt

    You should see

    hello
    hello

    >> means append. The command says how to act, so running it twice leaves two lines.

  2. 2
    Describe the end result instead
    rm notes.txt
    echo hello > notes.txt
    echo hello > notes.txt
    cat notes.txt

    You should see

    hello

    > means make the file contain this. Running it twice gives the same result. A declarative tool applies that idea to whole systems.

  3. 3
    Check whether a tool is installed
    terraform version
    tofu version

    You need only one of them. Install Terraform or OpenTofu from the official install pages, then come back. If a command is not found, that tool is not installed yet.

What breaks, and where to look

  • The files say one thing, the cloud console shows another, and nobody knows which is right.

    WHY IT HAPPENS
    Someone changed the infrastructure by hand after it was created from code. The code is no longer a true description.
    CHECK FIRST
    Run the tool's plan command (covered next) to see the differences, then decide whether to revert the hand edit or copy it into the code.
  • A teammate says IaC is pointless because they can click faster.

    WHY IT HAPPENS
    For a single throwaway resource that is true. The value shows up when you need a second copy, a reviewed change, or a rebuild after a mistake.
    CHECK FIRST
    Ask whether the thing will be built more than once or changed by more than one person. If yes, write it as code.

When not to use this

  • A one-off experiment you will delete today does not need code. The console is fine for exploring a service you have never used.
  • IaC describes what exists, not what runs inside a machine. Installing and configuring software on a server is the job of a configuration tool or a container image.

Check yourself

What is the difference between declarative and imperative?

Declarative describes the end result and the tool works out the steps. Imperative lists the steps in order and does not know what already exists.

Why does idempotence matter?

You can run the tool repeatedly and it only changes what differs, so rerunning after a failure or with no edits is safe.

Why store infrastructure files in Git?

Changes get reviewed, history shows who changed what and why, and you can rebuild from any commit.

Lesson 63 of 92 in the DevOps path

Search

Search cheat sheets and projects