Lesson 01 beginner

Introduction to Terraform

What Terraform is, the problems infrastructure as code solves, how plan and apply work, what changed in the Terraform world up to version 1.17 and how it compares with Ansible, CloudFormation, Pulumi and OpenTofu.

Terraform is a tool that creates and changes infrastructure from text files. You describe the servers, networks, databases, DNS records or containers you want in a language called HCL, and Terraform works out which API calls turn what exists today into what you described. The same files then become the documentation, the change history and the disaster recovery plan of your platform.

What Terraform looks like

Here is a complete Terraform configuration. It runs an nginx container on your own machine through Docker. You will run it yourself in lesson 3.

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 4.0"
    }
  }
}

provider "docker" {}

resource "docker_image" "web" {
  name = "nginx:1.27-alpine"
}

resource "docker_container" "web" {
  name  = "orbit-web"
  image = docker_image.web.image_id

  ports {
    internal = 80
    external = 8080
  }
}

Read it top to bottom. You ask for the Docker provider, then for an image and a container that uses it. Nothing says “pull the image first” or “start the container”. Terraform sees that the container refers to docker_image.web and creates them in the right order. You describe what, Terraform figures out how. That is what declarative means.

The problem infrastructure as code solves

Picture the team behind Orbit two years ago. Production was built by hand in a cloud console. It worked, until it did not:

  • The wiki lies. The setup page says the database has 2 vCPUs. Someone resized it during an incident and never updated the page.
  • Staging is not production. Both were built by clicking, months apart. A bug that only happens in production costs a week.
  • Nobody knows who changed what. A firewall rule opened “temporarily” is still open a year later, and nobody remembers why.
  • The region outage. Rebuilding everything in another region means following a 40-step checklist under pressure, from memory.
  • New environments take days. A developer asks for a test environment and waits for someone with console access.

Infrastructure as code (IaC) answers every one of these with the same habit software teams already have: keep it in Git.

BenefitWhat it means for Orbit
RepeatableStaging and production come from the same files with different inputs
ReviewedEvery change is a pull request with a readable plan attached
Auditablegit log tells you who changed the firewall, when and why
RecoverableA new region is terraform apply with a different variable
Self-documentingThe code is the description of what exists, and it cannot drift silently
FastA new environment takes minutes, not a ticket queue

How Terraform works

Every Terraform change follows the same loop:

  1. Write configuration in .tf files.
  2. Plan: Terraform reads the configuration, reads its state (its record of what it created before), asks the real APIs for the current situation and prints the difference as a list of creates, updates and destroys.
  3. Apply: after you approve the plan, Terraform makes those API calls, in parallel where it can and in dependency order where it must.
  4. Record: the result is saved to the state file, so the next plan starts from an accurate picture.

The pieces that make this possible:

  • Providers are plugins that know how to talk to one API: AWS, Azure, Google Cloud, Kubernetes, Docker, GitHub, Cloudflare, Datadog and thousands more on the Terraform Registry.
  • Resources are the objects a provider manages, such as docker_container or aws_s3_bucket.
  • The dependency graph comes from references between resources. Because the container uses docker_image.web.image_id, Terraform knows the image comes first.
  • State maps each resource in your code to the real object it manages. Lesson 5 is all about it.

The plan is the safety net. terraform plan never changes anything. Reading the plan before every apply, and especially looking for the word destroy, is the single most important Terraform habit.

The vocabulary you will use every day

TermMeaningExample
ProviderPlugin for one platform’s APIkreuzwerker/docker, hashicorp/aws
ResourceSomething Terraform creates and managesresource "docker_container" "web"
Data sourceSomething Terraform only readsdata "aws_caller_identity" "me"
VariableInput to a configurationvariable "environment"
OutputValue a configuration exposesoutput "url"
ModuleA folder of .tf files you can reusemodule "cache"
StateTerraform’s record of what it managesterraform.tfstate

Terraform in 2026: what changed

If you learned Terraform a few years ago, the language looks the same but the landscape moved:

  • Licence and ownership. In 2023 HashiCorp moved Terraform from the open-source MPL licence to the Business Source Licence (BSL). Using Terraform for your own infrastructure is unaffected; building a competing hosted product is restricted. IBM completed its acquisition of HashiCorp in February 2025.
  • OpenTofu. The community forked the last open-source version as OpenTofu, run under the Linux Foundation. It reads the same HCL and most of this course applies to it, but the two projects now add different features.
  • HCP Terraform is the new name of Terraform Cloud, HashiCorp’s hosted service for remote runs, state and policies.
  • The language grew a lot. Since version 1.5:
VersionFeatureLesson
1.5import blocks and config generation7
1.6 / 1.7terraform test and mock providers6, 14
1.7removed blocks, import with for_each5, 8
1.8Provider-defined functions9
1.10 / 1.11Ephemeral values, write-only arguments, S3 native locking4, 5, 15
1.14terraform query, list blocks and actions7, 13
1.15Dynamic module sources, deprecation warnings, convert()4, 6, 9
1.16import inside modules, destroy = false, Mermaid graphs3, 5, 6
1.17Terraform Policy GA, -minimal-refresh, variables in provider requirements3, 14, 15

Terraform compared with other tools

Terraform and Ansible solve different problems and are often used together. Terraform provisions: it creates the server, the network and the load balancer and remembers them in state. Ansible configures: it connects to a server that already exists and installs packages, edits files and restarts services. A common split: Terraform builds the machine, then a baked image, cloud-init or Ansible sets up what runs on it.

TerraformAWS CloudFormation / CDKPulumiOpenTofu
CloudsAny, through providersAWS onlyAnyAny, same providers as Terraform
LanguageHCLYAML/JSON, or code with CDKPython, TypeScript, Go, C#…HCL
StateState file you store (or HCP Terraform)Managed by AWSPulumi Cloud or your own backendState file, with built-in encryption
Previewterraform planChange setspulumi previewtofu plan
LicenceBSLProprietary serviceApache 2.0MPL 2.0

Pick CloudFormation if you are all-in on AWS and want AWS to hold the state. Pick Pulumi if your team prefers loops and classes in a general-purpose language. Pick Terraform or OpenTofu if you manage more than one platform and want the largest ecosystem of providers, modules and people who already know it.

Do you need Terraform?

You do benefit from it as soon as infrastructure outlives one afternoon: more than one environment, more than one person making changes, anything you would need to rebuild after a disaster, or anything an auditor might ask about.

You can skip it for a one-off experiment you will delete tonight, or when a platform already manages the infrastructure for you (a PaaS, a hosted database you configure in two fields).

Quick reference

TaskCommand
Download providers and modulesterraform init
Format all filesterraform fmt -recursive
Check the configurationterraform validate
Preview changesterraform plan
Make the changesterraform apply
Remove everything the configuration managesterraform destroy
Show what is in stateterraform state list
Print the installed versionterraform version

Exercise

A teammate opened a firewall port in the cloud console last month. Today you run terraform plan on code that does not mention that port. What does the plan show?

  1. Nothing, because Terraform only looks at its own files
  2. A change that closes the port again, because the real rule no longer matches the code
  3. An error asking you to import the console change
  4. A new resource for the port, copied from the console
Show answer

Answer: B. Plan compares your code with the real infrastructure. A change made outside Terraform, called drift, shows up as a difference that apply would undo. Fix drift by putting the change in code or by reverting it.

Exercise

Orbit needs a new virtual machine and, on it, nginx installed with a custom config file. Which split is the usual one?

  1. Ansible creates the machine, Terraform installs nginx
  2. Terraform does both with remote-exec provisioners
  3. Terraform creates the machine; an image, cloud-init or Ansible configures what runs on it
  4. CloudFormation creates the machine, Terraform installs nginx
Show answer

Answer: C. Terraform is built to provision and track infrastructure. Software inside a machine is better handled by images, cloud-init or a configuration tool. Lesson 13 explains why provisioners are a last resort.

Key takeaways

  • Terraform turns HCL files into API calls: you describe the result, it works out the steps.
  • Infrastructure as code brings Git habits (review, history, rollback) to infrastructure.
  • Every change is write, plan, apply, record. Read the plan before you approve it.
  • Providers talk to APIs, resources are what you manage, state links the two.
  • Terraform now ships under the BSL; OpenTofu is the open-source fork with the same language.
  • Terraform provisions, Ansible configures, and the two work well together.

FAQ

What is Terraform used for?

Creating and changing infrastructure from code: cloud networks, servers, databases, Kubernetes clusters, DNS, CDN settings, monitoring dashboards, GitHub repositories and anything else with an API and a provider.

Is Terraform still free?

Yes, the Terraform CLI is free to download and use for your own infrastructure. The BSL licence only restricts offering Terraform itself as a competing commercial service. HCP Terraform has a free tier and paid plans for larger teams.

Should I learn Terraform or OpenTofu?

Learn Terraform’s language and workflow; it transfers almost completely. Use whichever one your company runs. The commands differ only in name (terraform plan versus tofu plan) for everything in the first eleven lessons of this course.

Do I need to know a cloud before learning Terraform?

Not for this course: lessons 1 to 11 use Docker on your own machine. To use Terraform at work you also need to understand the platform you manage, because Terraform does not hide what a VPC or an IAM role is.

What is the hardest part for beginners?

State. Most scary moments (lost changes, lock errors, a plan that wants to recreate everything) come from not knowing what the state file is and where it lives. Lesson 5 covers it in detail.

↑ ↓ navigate Enter open Esc close