🏗️
Terraform Cheat Sheet
init, plan, apply, state, and workspace commands.
Related: terraform · terragrunt · aws-cloudformation
Workflow
| Command | Description | |
|---|---|---|
terraform init | Download providers and modules | |
terraform fmt -recursive | Format .tf files | |
terraform validate | Check syntax and config | |
terraform plan -out=tfplan | Preview changes, save plan | |
terraform apply tfplan | Apply saved plan | |
terraform destroy | Remove all managed resources |
State
| Command | Description | |
|---|---|---|
terraform state list | Resources in state file | |
terraform state show aws_instance.web | Single resource details | |
terraform state rm aws_instance.web | Remove from state (keep in cloud) | |
terraform import aws_instance.web i-0abc123 | Import existing resource | |
terraform force-unlock <lock-id> | Release stuck state lock |
Workspaces & output
| Command | Description | |
|---|---|---|
terraform workspace list | List workspaces | |
terraform workspace select prod | Switch workspace | |
terraform output -json | Print outputs |