Infrastructure as Code covers why declarative, state-tracked infrastructure beats a pile of one-off cloud console clicks or ad hoc scripts. This page is the command reference for actually driving Terraform.
The core loop
terraform init # download providers/modules, set up the backend -- run this first, and # again any time providers/backend config changeterraform validate # check syntax and internal consistency, no cloud callsterraform fmt # reformat .tf files to the canonical styleterraform fmt -recursive # ...across every subdirectoryterraform plan # compute and show the diff between config and real infrastructure, # without changing anythingterraform plan -out=tfplan # save the computed plan to apply later, exactly as reviewedterraform apply # plan, then prompt to confirm, then executeterraform apply tfplan # apply a previously-saved plan, no re-prompt (the safe CI pattern -- # what got reviewed is exactly what gets applied)terraform apply -auto-approve # skip the confirmation prompt (CI only, never by habit)terraform destroy # plan and apply the removal of everything this config manages
State inspection and surgery
Terraform tracks what it manages in a state file — these commands are for when that state has drifted from reality, or needs to be reorganized.
terraform state list # every resource address currently in stateterraform state show aws_instance.web # full attributes of one resource, as Terraform knows themterraform state mv aws_instance.web aws_instance.web_server # rename a resource without destroying/recreating itterraform state rm aws_instance.web # stop tracking a resource (does NOT destroy it in the cloud)terraform show # human-readable dump of the entire current stateterraform import aws_instance.web i-0123456789abcdef0 # bring an already-existing, manually-created resource under Terraform's management
Workspaces: separating environments in one config
terraform workspace list # all workspaces, current one marked with *terraform workspace new staging # create and switch to a new workspaceterraform workspace select prod # switch to an existing oneterraform workspace show # which one is active right now
Everything else worth knowing
terraform output # show every declared output valueterraform output vpc_id # show one specific outputterraform providers # list providers required by this configurationterraform console # interactive REPL for testing expressions against the current stateterraform apply -target=aws_instance.web # apply changes to one resource only (escape hatch, not a habit -- # it can leave dependent resources out of sync with config)terraform apply -replace="aws_instance.web" # force-recreate one resource on the next apply # (the current replacement for the deprecated `terraform taint`)terraform plan -var="instance_type=t3.large" # override a variable for one runterraform plan -var-file="prod.tfvars" # load variables from a file