Terraform Tutorial · Terraform Basics · Lesson 1 of 25

Terraform Introduction

What Terraform is, what infrastructure as code means, and why teams use it to create and change cloud infrastructure safely.

What is Terraform?

Terraform is a tool for infrastructure as code (IaC). Instead of clicking through a cloud console to create servers, networks and databases, you describe them in text files, and Terraform creates, changes and deletes the real infrastructure to match.

resource "aws_s3_bucket" "photos" {
  bucket = "my-team-photos"
}

Run terraform apply, and the bucket exists. Change the file and apply again, and Terraform changes only what is different. Delete the block, and Terraform deletes the bucket.

Why infrastructure as code?

  • Repeatable: create identical development, staging and production environments from the same code.
  • Reviewable: infrastructure changes go through pull requests and code review like any other code.
  • Safe: terraform plan shows exactly what will change before anything happens.
  • Documented: the code is the documentation of what exists.
  • Multi-cloud: one workflow for AWS, Azure, Google Cloud, Kubernetes, GitHub, Datadog and thousands more, through providers.

How Terraform works

  1. You write configuration in .tf files, using HCL (HashiCorp Configuration Language).
  2. terraform plan compares your configuration with the state (what Terraform created before) and the real world, and shows the differences.
  3. terraform apply makes the changes through each provider's API and updates the state.

Terraform is declarative: you describe the end result ("three servers"), not the steps ("create a server, then another"). It works out the order from the dependencies between resources.

A first configuration
output "message" {
  value = "Hello from Terraform!"
}

Output

message = "Hello from Terraform!"

Run this on your computer — see “Get Started” for setup.

This tutorial's examples are real configurations: each one was run with Terraform 1.17 and the output below it is what Terraform printed. Examples that need a cloud account are shown as code you can read and adapt.

Test yourself

Which command shows what Terraform would change, without changing anything?

  1. terraform apply
  2. terraform plan
  3. terraform init
  4. terraform show
Show answer

Answer: terraform plan

plan is a dry run. apply makes the changes, init prepares the directory, show prints state.