Skip to main content

Terraform for Beginners: When and How to Use It

·409 words·2 mins· loading · loading · · ·
Table of Contents

Introduction
#

Terraform is an open-source “infrastructure as code” (IaC) tool that lets you declare cloud and on‑prem resources in human-readable files. Use it when you want repeatable, versioned, and reviewable infrastructure changes across cloud providers or environments. This guide covers when Terraform is the right choice and a minimal workflow to get you started.

When to use Terraform
#

Prefer Terraform when you need:

  • Multi-cloud or hybrid provisioning from a single configuration.
  • Declarative state: describe desired end state and let Terraform compute changes.
  • Reusable modules for team-wide standards (networks, IAM, storage).
  • Versioned infrastructure that you can review in Git and roll back.

Avoid Terraform for ephemeral, per-developer tasks better handled by short-lived scripts; prefer it for resources whose lifecycle should be tracked and audited.

Core concepts (quick)
#

  • Provider: plugin that talks to a platform (AWS, Azure, GCP, etc.).
  • Resource: the thing you manage (vm, network, bucket).
  • State: the local or remote snapshot that maps config to real-world resources.
  • Module: reusable collection of resources and variables.
  • Plan & Apply: terraform plan shows changes; terraform apply executes them.

Use remote state (e.g., S3+DynamoDB for AWS) for team collaboration to avoid state conflicts.

Minimal workflow and example
#

Typical local workflow:

  1. Write configs in HCL (.tf files).
  2. Run terraform init to install providers.
  3. Run terraform plan to inspect changes.
  4. Run terraform apply to make changes.
  5. Commit .tf files (don’t commit local state unless using remote state).

Example: create an AWS S3 bucket (simplified):

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "site" {
  bucket = "example-terraform-bucket-12345"
  acl    = "private"
}

Commands:

terraform init
terraform plan
terraform apply

Safety and operational tips
#

  • Store state remotely and lock it (prevent concurrent changes).
  • Use workspaces or separate state files per environment (dev/stage/prod).
  • Pin provider versions in required_providers to avoid surprises.
  • Keep secrets out of plain TF files—use Vault or provider-specific secret sources and environment variables.
  • Review terraform plan output in PRs; consider automated policy checks (Sentinel, Open Policy Agent).
  • Build modules for repeatable patterns and publish them to a private registry.

Conclusion
#

Terraform shines when you want declarative, version-controlled infrastructure across clouds or environments. Start small: codify one resource, use remote state, and require plan review in PRs. Gradually refactor repeated patterns into modules and add policy checks and automated scans. Next step: create a tiny module, push it to a private registry, and run a CI job that executes terraform plan for PRs.

Co-authored by Vishwakarma, Deeps 2nd Brain

Deep Jiwan
Author
Deep Jiwan
Building hacky solutions that save time and make my life easier. Not too sure about yours :)

Related