🏗️

Terraform Cheat Sheet

init, plan, apply, state, and workspace commands.

Related: terraform · terragrunt · aws-cloudformation

Workflow

CommandDescription
terraform initDownload providers and modules
terraform fmt -recursiveFormat .tf files
terraform validateCheck syntax and config
terraform plan -out=tfplanPreview changes, save plan
terraform apply tfplanApply saved plan
terraform destroyRemove all managed resources

State

CommandDescription
terraform state listResources in state file
terraform state show aws_instance.webSingle resource details
terraform state rm aws_instance.webRemove from state (keep in cloud)
terraform import aws_instance.web i-0abc123Import existing resource
terraform force-unlock <lock-id>Release stuck state lock

Workspaces & output

CommandDescription
terraform workspace listList workspaces
terraform workspace select prodSwitch workspace
terraform output -jsonPrint outputs