Tools › Cheat sheets › Containers & DevOps › Terraform
🏗️

Terraform cheat sheet

Terraform describes infrastructure as code. The core loop is init, plan, apply. Always read the plan before applying, especially anything that says destroy or replace. OpenTofu uses the same commands with tofu.

Click any command to copy it.

Core workflow

CommandWhat it does
terraform initDownload providers and modules, set up the backend.
terraform init -upgradeUpgrade providers within version constraints.
terraform fmt -recursiveFormat all .tf files.
terraform validateCheck the configuration for errors.
terraform planPreview the changes.
terraform plan -out=tfplanSave the plan so apply runs exactly what you reviewed.
terraform apply tfplanApply a saved plan.
terraform applyPlan and apply after confirmation.

Variables and outputs

CommandWhat it does
terraform plan -var "region=eu-west-1"Set a variable on the command line.
terraform plan -var-file=prod.tfvarsLoad variables from a file.
terraform outputShow output values.
terraform output -jsonOutputs as JSON for scripts.
terraform output -raw namePrint one output with no quotes.
export TF_VAR_name=valueSet a variable through the environment.

State

CommandWhat it does
terraform state listList resources in the state.
terraform state show aws_instance.webShow one resource's attributes.
terraform state mv old.name new.nameRename a resource in state without recreating it.
terraform import aws_instance.web i-0abc123Bring an existing resource under Terraform management.
terraform state rm aws_instance.webDestructive: Forget a resource without destroying it; Terraform may try to create a duplicate.
terraform apply -refresh-onlyUpdate state to match real infrastructure.
terraform force-unlock LOCK_IDDestructive: Release a stuck state lock. Only if you are sure no other run is active.

Targeted and destructive

CommandWhat it does
terraform apply -replace=aws_instance.webDestructive: Force a resource to be destroyed and recreated (replaces the old taint command).
terraform apply -target=aws_instance.webApply only one resource (use sparingly).
terraform apply -auto-approveDestructive: Apply without confirmation. Use only in reviewed automation.
terraform destroyDestructive: Destroy everything in the configuration.
terraform destroy -target=aws_instance.webDestructive: Destroy one resource.

Workspaces and debugging

CommandWhat it does
terraform workspace listList workspaces.
terraform workspace new stagingCreate a workspace.
terraform workspace select stagingSwitch workspace.
terraform consoleEvaluate expressions interactively.
terraform showShow the current state or a saved plan.
terraform providersList required providers.
TF_LOG=DEBUG terraform planEnable detailed logging.
terraform versionShow Terraform and provider versions.

Frequently asked questions

What is the difference between plan and apply?

plan only shows what would change. apply makes the changes. Saving a plan with -out and applying that file guarantees you apply exactly what you reviewed.

How do I recover from a corrupted or lost state?

Use a remote backend with versioning (such as an S3 bucket with versioning) so you can restore an earlier state file; terraform import can rebuild entries for existing resources.

More Containers & DevOps cheat sheets

🌿 Git🐳 Docker☸️ kubectl🅰️ AnsibleAll Containers & DevOps cheat sheets →
Copied to clipboard