Terraform Cheatsheet - Infrastructure as Code Command Reference
Terraform's mental model is three storage layers — code in HCL, config through variables and backend, and the live state describing what actually exists. plan versus apply is the rhythm you operate on: plan previews the diff, apply makes it real, and destroy has to be deliberate. This table pairs each command with the state/backend knowledge it depends on so the workflow clicks instead of confusing.
Core Workflow 7
terraform initInitialize the working dir; download providers and modules
terraform planPreview changes: resources to create/modify/destroy
terraform applyApply changes to infrastructure
terraform destroyDestroy all managed infrastructure
terraform fmtFormat .tf files to the standard style
terraform validateValidate config syntax and internal consistency
terraform apply -auto-approveAuto-approve (for CI/CD)
State Management 9
terraform state listList all resources in the state file
terraform state show <resource>Show detailed state of a resource
terraform state mv <src> <dst>Move a resource's address in state
terraform state rm <resource>Remove a resource from state (keeps the real one)
terraform state pullPull remote state to local stdout
terraform state pushPush local state to the remote backend
terraform import <address> <id>Import an existing resource into Terraform
terraform taint <resource>Mark a resource as 'tainted'; rebuilt on next apply
terraform untaint <resource>Clear the 'tainted' mark
Variables & Outputs 8
variable "region" {\n type = string\n default = "us-east-1"\n}Define a variable (type, default)
var.regionReference a defined variable
TF_VAR_region=us-west-2Set a variable via environment variable
terraform plan -var="region=us-west-2"Set a variable via CLI flag
output "ip" {\n value = aws_instance.web.public_ip\n}Define an output value
terraform outputShow all output values
terraform output -jsonShow outputs as JSON
locals {\n name = "${var.prefix}-web"\n}Local values to simplify expressions
Modules 6
module "vpc" {\n source = "terraform-aws-modules/vpc/aws"\n version = "5.0.0"\n}Reference a Registry module, pin version
module "vpc" {\n source = "./modules/vpc"\n}Reference a local module (relative path)
module "vpc" {\n source = "git::https://example.com/vpc.git"\n}Reference a module in a Git repo
module.vpc.vpc_idReference a module's output
terraform init -upgradeUpgrade modules to the latest allowed version
terraform getDownload and update modules
Remote Backends 6
terraform {\n backend "s3" {\n bucket = "my-tf-state"\n key = "prod/terraform.tfstate"\n region = "us-east-1"\n }\n}Store state in an S3 backend
terraform {\n backend "s3" {\n dynamodb_table = "tf-lock"\n }\n}DynamoDB state lock against concurrency
data "terraform_remote_state" "db" {\n backend = "s3"\n config = {\n bucket = "shared-state"\n key = "db/terraform.tfstate"\n }\n}Read remote state across configurations
data.terraform_remote_state.db.outputs.db_endpointReference a remote state output
terraform init -reconfigureReconfigure the backend (migrate state)
terraform init -migrate-stateMigrate the state file to a new backend
Advanced Features 9
terraform workspace new stagingCreate a new workspace
terraform workspace listList all workspaces
terraform workspace select stagingSwitch to a workspace
resource "aws_instance" "web" {\n for_each = var.subnets\n}for_each creates resources from a set
resource "aws_instance" "web" {\n count = var.instance_count\n}count creates resources by number
depends_on = [aws_s3_bucket.data]Explicit dependency to fix creation order
lifecycle {\n create_before_destroy = true\n}Lifecycle: create before destroy
lifecycle {\n prevent_destroy = true\n}Lifecycle: prevent accidental destroy
lifecycle {\n ignore_changes = [tags]\n}Lifecycle: ignore changes to specific attributes
Debugging 8
terraform consoleInteractive console to test expressions
terraform graph | dot -Tpng > graph.pngGenerate a resource dependency graph
terraform showReadable dump of the current state
terraform show -jsonJSON state output for jq
TF_LOG=DEBUG terraform applyEnable DEBUG-level logging
TF_LOG=TRACE terraform planTRACE-level logging, most verbose
TF_LOG_PATH=./tf.log terraform applyWrite logs to a file
export TF_LOG=WARNSet log level (TRACE/DEBUG/INFO/WARN/ERROR)
Tips
- terraform plan never modifies resources — use it freely
- Keep secrets in variables; never hardcode them in .tf files
- Use terraform fmt to standardize formatting — essential for teams
- The state file holds sensitive data; never commit it to Git
- Use a remote backend (e.g. S3 + DynamoDB) for collaboration and state locking
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us