Yash Verma / Cloud runbook

Terraform commands

15 commands from the cloud runbook. Fill in a placeholder once and every command uses it.

Generate and save a plan

Generates an execution plan to a file.

terraform plan -out=tfplan

plan, review

Apply without confirmation

Applies changes automatically.

Caution. Applies without showing a plan for approval. Use it only in CI, ideally on a reviewed saved plan: terraform apply tfplan.

terraform apply -auto-approve

apply, ci-cd

Import existing resource

Brings existing cloud resource under TF state.

terraform import <resource_address> <cloud-resource-id>

Terraform 1.5 and later also support import blocks in configuration, which show up in the plan.

import, state

Remove resource from state

Removes a resource from state tracking without destroying the actual cloud resource.

terraform state rm <resource_address>

Terraform stops managing the resource; it keeps running. Back up the state first.

state, remove

Force unlock state file

Manually removes a lock on the state file if a previous pipeline crashed.

Caution. Only when you are sure no other run holds the lock. Two runs writing the same state can corrupt it.

terraform force-unlock <lock-id>

state, lock, debug

Init and upgrade providers

Initializes terraform and upgrades all providers to their latest allowed versions.

terraform init -upgrade

init, providers

List Terraform workspaces

Shows all available state workspaces.

terraform workspace list

workspace, state

Validate configuration

Validates the syntax and internal consistency of terraform files.

terraform validate

validate, syntax

Format terraform files

Rewrites all .tf files to a canonical format.

terraform fmt -recursive

format, style

Show outputs

Displays the output values from your terraform state.

terraform output -json

output, state

List all resources in state

Shows all resources currently tracked by terraform.

terraform state list

state, inventory

Show resource in state

Displays attributes of a single resource in the state.

terraform state show <resource_address>

state, inspect

Generate dependency graph

Generates a visual dependency graph of resources in DOT format.

terraform graph | dot -Tpng > graph.png

Needs Graphviz for the dot command.

graph, visualization

Force recreation of a resource

Plans the destroy and re-create of one resource. Replaces the deprecated terraform taint.

terraform apply -replace=<resource_address>

replace, taint, recreate

Create new workspace

Creates and switches to a new terraform workspace.

terraform workspace new <workspace-name>

workspace, create