← Back to cheat sheets

Infrastructure as Code

Terraform cheat sheet

Terraform provisions infrastructure using declarative code.

On this page

Table of contents

Use this page for fast recall. Use Full documentation when you want the complete end-to-end path.

Quick workflow

A simple 5-step flow you can follow when using Terraform in real work.

1) Setup

Install/run the tool and confirm version. Create a minimal config.

2) Small change

Do one small action end-to-end to prove the workflow.

3) Validate

Check output, logs, and status. Catch mistakes early.

4) Automate

Convert it into a repeatable script or pipeline step.

5) Productionize

Add safety: secrets, rollback, observability, and docs.

Workflows you will actually reuse

These are practical sequences you can copy into your own checklist or runbook.

Team Terraform workflow

Goal: Keep infrastructure changes reviewable and safe.

- Use remote state with locking (S3+DynamoDB, etc.).

- Run fmt+validate on every PR.

- Always review `plan` output before apply.

- Apply from a controlled runner (CI/CD) with approvals for prod.

- Avoid `-target` except for emergency recovery.

Key Concepts

- Resources

- State

- Modules

Learning path (high-level):

- Resource basics

- Module reuse

- Remote state and workflow

Quick Start

Initialize

Command

terraform init

Plan

Command

terraform plan

Apply

Command

terraform apply

Common Commands

Short descriptions and practical intent. Search, filter, copy, and reuse.

Showing 22

Basics
terraform version

Show Terraform version.

Quality
terraform fmt -recursive

Format all Terraform files.

Quality
terraform validate

Validate configuration syntax.

Workflow
terraform init

Initialize providers and backend.

Workflow
terraform init -upgrade

Upgrade provider versions within constraints.

Workflow
terraform plan

Preview changes.

Workflow
terraform plan -out tfplan

Save the plan for later apply.

Workflow
terraform apply

Apply changes (interactive).

Workflow
terraform apply tfplan

Apply a saved plan.

Workflow
terraform destroy

Delete managed infrastructure.

State
terraform show

Show current state in human-readable form.

State
terraform output

Print outputs (useful for wiring).

State
terraform state list

List resources in state.

State
terraform state show <addr>

Show a resource from state.

State
terraform state mv <from> <to>

Move resources in state (refactor).

State
terraform state rm <addr>

Remove a resource from state (danger).

State
terraform import <addr> <id>

Bring existing resource under Terraform.

State
terraform taint <addr>

Mark resource for recreation (legacy).

Workflow
terraform plan -target=<addr>

Plan only a specific resource (use carefully).

Workspaces
terraform workspace list

List workspaces.

Workspaces
terraform workspace new dev

Create a workspace.

Workspaces
terraform workspace select dev

Switch workspaces.

Copyable snippets

Small blocks you can drop into your terminal, config, or runbook.

Safe local workflow

bash

terraform fmt -recursive
terraform validate
terraform plan -out tfplan
terraform apply tfplan

Troubleshooting checklist

When things break, follow this order to stay calm and move fast.

- If state is locked: confirm no active apply is running; unlock only when safe.

- If drift happens: re-run plan and reconcile with the smallest changes.

- If resources want replacement: read the diff; it may cause downtime.

Pitfalls

The common mistakes that slow people down when using Terraform.

- Copy-pasting commands without understanding inputs/outputs and side effects.

- Not documenting defaults (ports, paths, credentials) and then getting stuck in prod.

- Skipping logs and metrics when troubleshooting; always collect evidence first.

Mini lab (practice)

Do these tasks in order. You will feel the tool instead of just reading about it.

- Create a small module with variables and outputs, then reuse it in a root config.

- Practice `plan`, `apply`, and `destroy` safely with a remote state backend (if possible).

- Introduce drift and reconcile using `refresh`/`plan` changes.

Interview prompts

Use these to test if you truly understand the basics (and can explain them clearly).

- Explain the tool’s role in a real CI/CD pipeline from commit to production.

- Describe the most common failure you’ve seen with this tool and how you fixed it.

- What would you monitor/alert on for this tool in production?

Official Docs

https://developer.hashicorp.com/terraform/docs