SadServers
  • Scenarios
  • Labs
    All Labs Linux & Bash Web Servers Databases Data Processing Docker Kubernetes CI/CD Infrastructure as Code Tooling / Applications
  • Dashboard
  • Solutions
    For Individuals For Businesses
  • Ranking
  • Newsletter
  • Documentation
    FAQ Support Pro Accounts Pro+ Accounts Business Accounts Gift API CLI/TUI Privacy Troubleshooting Interviews
  • Blog
  • Pricing
  • Gift
    Gift Purchase Gift Redeem
  • About
Log In - Sign Up
  1. Labs
  2. Terraform
  3. Cheatsheet

Guide

Concepts and learning path

Troubleshooting

Failure modes and fixes

Cheatsheet

Commands to keep handy

Terraform cheatsheet

CLI essentials

CommandDescription
terraform initInitialize providers and backend
terraform fmt -recursiveFormat HCL files
terraform validateValidate configuration
terraform planPreview changes
terraform plan -out=plan.tfplanSave plan for apply
terraform applyApply changes
terraform apply plan.tfplanApply saved plan only
terraform destroyDestroy managed resources

Inspect and debug

CommandDescription
terraform showCurrent state or saved plan
terraform state listResources in state
terraform state show ADDROne resource details
terraform outputPrint outputs
terraform consoleEvaluate expressions interactively
TF_LOG=DEBUG terraform planVerbose provider logs

Variables and outputs

variable "region" { type = string default = "us-east-1" } output "bucket_id" { value = aws_s3_bucket.logs.id } # CLI: terraform plan -var="region=eu-west-1" # Or: terraform plan -var-file=prod.tfvars

Module call

module "vpc" { source = "./modules/vpc" version = "1.2.0" # if registry module cidr_block = var.vpc_cidr tags = var.tags }

Remote backend (S3 example)

terraform { backend "s3" { bucket = "my-tf-state" key = "prod/network/terraform.tfstate" region = "us-east-1" dynamodb_table = "tf-locks" encrypt = true } }

State and import

terraform import aws_instance.web i-0abc123 terraform state mv aws_instance.old aws_instance.new terraform state rm aws_instance.orphan terraform workspace list terraform workspace select staging

Targeted operations

terraform plan -target=aws_instance.web terraform apply -target=module.vpc terraform taint aws_instance.web # force recreate on next apply

Common HCL references

ExpressionDescription
var.nameInput variable
local.nameLocal value
module.vpc.vpc_idModule output
data.aws_ami.latest.idData source attribute
aws_instance.web.idResource attribute
count / for_eachMultiple resource instances

Pro tips

  • Commit .terraform.lock.hcl — reproducible provider versions across the team
  • Run plan in CI on every PR; require human review before apply to prod
  • Use lifecycle { prevent_destroy = true } on critical resources
  • Prefer for_each over count when resource identity matters
  • Never edit state by hand — use state mv/rm and import
SadServersSadServers

Real-world Linux and DevOps scenarios for hands-on learning and technical assessment.

Uptime Robot ratio (30 days)
Product
  • Scenarios
  • For Individuals
  • For Businesses
  • Pricing
Resources
  • FAQ
  • Blog
  • Newsletter
Company
  • About Us
  • Support
  • Privacy Policy
  • Terms of Service
  • Contact
Connect With Us
info@sadservers.com

Made in Canada 🇨🇦
Updated: 2026-06-13 16:06 UTC – 2d2950a