Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 37 ยท Infrastructure as Code Basics

What Is Terraform? Infrastructure as Code Explained

Learn what Terraform is, why teams describe servers, networks and databases as code, how declarative IaC differs from scripts, and how Terraform compares to other tools.

Terraform is a tool that creates, changes and deletes infrastructure from text files. Instead of clicking through a cloud console to create a server, a network and a database, you describe the things you want in configuration files. Terraform reads them, compares them to what already exists, and makes the real world match. The language of those files is HCL, the HashiCorp Configuration Language, which you will learn in the next chapter.

The idea behind this is called Infrastructure as Code (IaC): treat infrastructure like software. Files live in Git, changes go through review, and the same files can build a test environment on Monday and a production environment on Friday. This course teaches Terraform 1.x and points out where OpenTofu, the open-source fork, behaves the same or differently.

What is real and what is a stand-in in this course. Terraform is a program that talks to cloud APIs, so it cannot run inside your browser. Every hcl block is therefore a read-only example. Any terraform plan or apply output is labelled "example output": it is what the tool prints, not something produced here. To teach the concepts, many lessons add small runnable stand-ins written in Python. They are not Terraform; they imitate one idea (dependency ordering, type evaluation, count/for_each expansion, state diffing) so you can poke at it. When you install Terraform yourself in the next lesson, everything read-only here becomes runnable on your machine.

The problem Terraform solves

Imagine a small web application: a virtual network, two web servers, a database and a load balancer. Created by hand, you will make dozens of clicks. Next month someone asks for the same setup for a staging environment. You try to remember the clicks, you forget a firewall rule, and the two environments quietly differ. This is called configuration drift, and it is the root of many outages.

Infrastructure as Code fixes four problems at once:

  • Repeatability. The same files produce the same infrastructure every time.
  • Reviewability. A change is a diff in a pull request, not an undocumented click.
  • History. Git records who changed what and why, and you can roll back.
  • Speed. Creating an environment takes minutes, and tearing it down is one command.

Declarative versus imperative

There are two ways to automate infrastructure. An imperative script lists steps: "create a server, then attach a disk." A declarative description lists the end result: "there is a server with a 50 GB disk." Terraform is declarative.

The difference matters when you run the thing twice. An imperative script that says "create a server" creates a second server on the second run, unless you wrote extra checks. A declarative tool looks at what exists and only does the difference. Running it twice is safe, and the second run does nothing. This property is called idempotence.

Python Imperative versus declarative (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The imperative function keeps adding servers. The declarative one says what to do the first time and then reports an empty list, because nothing needs to change. Terraform's whole workflow is a much bigger version of declarative_run.

A first look at Terraform code

Here is a taste of HCL. You do not need to understand every line yet.

HCL
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
  }
}

provider "aws" {
  region = "eu-central-1"
}

resource "aws_s3_bucket" "logs" {
  bucket = "example-app-logs-2026"
}

Read it as a description. A provider block says "talk to AWS in Frankfurt." A resource block says "there must be an S3 bucket with this name." The words aws_s3_bucket and logs are the resource type and your own local name for it. Nothing here says how to create a bucket; the AWS provider knows how.

Providers: one tool, many platforms

Terraform itself knows nothing about AWS, Azure, Google Cloud or GitHub. Plugins called providers translate resource blocks into API calls. There are thousands of them in the public Terraform Registry: cloud platforms, but also Kubernetes, Cloudflare, Datadog, GitHub, PostgreSQL and even DNS. That is the main reason Terraform is popular: one language and one workflow for everything with an API. See Providers for details.

How Terraform compares

Tool Style Best at
Terraform / OpenTofu Declarative, provider plugins, keeps state Creating and changing infrastructure across many platforms
AWS CloudFormation, Azure Bicep Declarative, one cloud only Deep integration with a single vendor
Pulumi, CDK for Terraform Declarative engine, general-purpose languages Teams that prefer Python, TypeScript or Go over HCL
Ansible Mostly procedural, agentless Configuring what runs on servers (packages, files, services)
Kubernetes manifests Declarative, controllers reconcile Running containers once a cluster exists

These tools overlap and are often combined. A typical pipeline uses Terraform to create the servers or the cluster, then Ansible or Kubernetes to configure and run software on them. A rule of thumb: Terraform builds the house; configuration tools arrange the furniture.

Terraform and OpenTofu

In 2023 HashiCorp changed Terraform's license from the open-source MPL to the Business Source License. The community responded with OpenTofu, a fork hosted by the Linux Foundation that stays open source. For 1.x features the two are almost interchangeable: the same HCL, the same providers, a tofu command instead of terraform. Where OpenTofu has extras (for example built-in state encryption), later lessons mention them. This course writes terraform in every command; swap in tofu if you prefer.

The core idea: desired state versus real state

Terraform compares three things:

  1. Configuration: the desired state you wrote in .tf files.
  2. State: Terraform's record of what it created before.
  3. Reality: what actually exists in the cloud, which it reads from the provider.

It plans the actions that turn reality into the configuration, shows them to you, and only changes things after you approve. The picture looks like this:

flowchart LR
    A["Configuration<br/>(.tf files)"] --> P{"terraform plan"}
    S["State<br/>(what Terraform knows)"] --> P
    R["Real infrastructure<br/>(via provider APIs)"] --> P
    P --> D["Proposed changes:<br/>create / update / destroy"]
    D --> AP["terraform apply"]
    AP --> R
    AP --> S

Plan reads three inputs and produces a list of changes; apply performs them and updates the state.

Why Infrastructure as Code

Repeatable Same files, same result.
Reviewable Changes are diffs in Git.
Fast Build and destroy in minutes.
1 / 2

Who uses Terraform and what for

You will see Terraform in platform and DevOps teams. Typical jobs include provisioning networks and virtual machines, creating managed databases and queues, setting up Kubernetes clusters, managing DNS records and TLS certificates, configuring identity and permissions, and even creating GitHub repositories and Datadog dashboards. It is less suited to things that change every second (use autoscaling for that) and to installing software on a machine (use Ansible or container images).

Common misunderstandings

  • "Terraform is a script." It is a planner. Order in the file does not matter; dependencies decide the order. See dependencies.
  • "The state file is optional." It is not. Without state Terraform cannot tell which real resource belongs to which block. State gets its own chapter.
  • "Terraform will manage everything in my account." It manages only what is in its configuration and state. Resources someone created by hand are invisible until you import them.
  • "Apply is always safe." Always read the plan. A renamed resource can look like "destroy one, create another."

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does it mean that Terraform is declarative?

    In a declarative tool you state the target. Terraform compares it with what exists and computes the create, update and destroy actions.
  2. 2. Which component knows how to call the AWS API?

    Terraform core is platform-neutral. Providers such as hashicorp/aws translate resource blocks into API calls.
  3. 3. You run an applied configuration a second time without changing anything. What should happen?

    This is idempotence: reality already matches the configuration, so the plan is empty.
  4. 4. Which tool is best described as configuring software on servers rather than building the servers?

    Terraform, OpenTofu and CloudFormation create infrastructure. Ansible mainly configures what runs on machines that already exist.

Summary

  • Terraform describes infrastructure in HCL files and makes real infrastructure match them.
  • Infrastructure as Code gives repeatability, review, history and speed.
  • Declarative descriptions are idempotent: a second run with no changes does nothing.
  • Providers are plugins that talk to platform APIs; core is platform-neutral.
  • Terraform compares configuration, state and reality, then plans create, update or destroy actions.
  • OpenTofu is the open-source fork; for 1.x features the HCL and workflow match.
  • In this course hcl blocks are read-only and Python stand-ins imitate the concepts.

Next: Installing Terraform and Your First Configuration