Skip to content

Terraform Basics

What It Is

Terraform describes infrastructure declaratively in HCL and reconciles real resources to the desired state through plan and apply.

Key Concepts

Provider

A plugin that knows how to work with a platform such as AWS, Azure, GCP, Cloudflare, Kubernetes, and many others.

Resource

Represents a concrete infrastructure object such as aws_instance, aws_vpc, or cloudflare_record.

Data Source

Reads existing data without creating a new resource.

Variable

Lets you parameterize the configuration.

Output

Exposes values such as IPs, IDs, endpoints, and ARNs.

Module

A reusable block of Terraform code. A good module encapsulates a standard infrastructure pattern with clear inputs and outputs.

Lifecycle

  1. terraform init
  2. terraform fmt and terraform validate
  3. terraform plan
  4. terraform apply
  5. Changes are recorded in state

State

State stores the mapping between HCL and real infrastructure objects. That is how Terraform knows what to create, update, or destroy.

For team workflows, use a remote backend with locking, such as S3 plus DynamoDB or HCP Terraform.

Common Files

  • main.tf
  • variables.tf
  • outputs.tf
  • providers.tf
  • terraform.tfvars

Minimal Resource Example

resource "local_file" "example" {
  filename = "example.txt"
  content  = "hello terraform"
}

Common Mistakes

  • storing secrets in plain text
  • using local state in a team project
  • building overly large monolithic root modules
  • making manual cloud-side changes outside Terraform

Practice

  • Keep one root module per clear bounded context.
  • Extract common patterns into modules.
  • Pin providers and versions explicitly.
  • Save a plan file before production apply.