Terraform
Everyday plan/apply, workspaces, working with state, imports and targeted runs.
Everyday workflow
Initialize providers and backend
terraform initFormat and validate
terraform fmt -recursive && terraform validateShow planned changes
terraform planSave a plan and apply exactly it
terraform plan -out=tfplan && terraform apply tfplanPlan with a variables file
terraform plan -var-file=<env>.tfvars
Show outputs
terraform outputDestroy everything (with confirmation)
terraform destroyWorkspaces
List workspaces
terraform workspace listCreate and switch to a workspace
terraform workspace new <workspace>
Switch workspace
terraform workspace select <workspace>
State
List resources in state
terraform state listShow a resource from state
terraform state show '<resource>'
Rename or move a resource in stateadvanced
terraform state mv '<resource>' '<new_resource>'
In Terraform 1.1+ prefer a moved {} block in code.
Forget a resource without destroying itadvanced
terraform state rm '<resource>'
Import an existing resourceadvanced
terraform import '<resource>' <cloud_id>
Release a stuck state lockadvanced
terraform force-unlock <lock_id>
Targeted and advanced runs
Plan a single resourceadvanced
terraform plan -target='<resource>'
Recreate a resourceadvanced
terraform apply -replace='<resource>'
Sync state with reality without changing infraadvanced
terraform apply -refresh-onlyUpgrade providers within constraintsadvanced
terraform init -upgradeDetailed logsadvanced
TF_LOG=DEBUG terraform plan