You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# install via tfenv (recommended — manages multiple versions)
git clone https://github.com/tfutils/tfenv.git ~/.tfenv
tfenv install latest
tfenv use latest
terraform version
# or via package manager (macOS)
brew install terraform
# shell completion
terraform -install-autocomplete
workspace & init
terraform init # initialize working dir, download providers
terraform init -upgrade # upgrade providers to latest allowed version
terraform init -reconfigure # reinitialize, ignore existing state
terraform init -backend=false # skip backend init
terraform workspace list # list workspaces
terraform workspace new <n># create workspace
terraform workspace select<n># switch workspace
terraform workspace show # current workspace
terraform workspace delete <n># delete workspace
plan & apply
terraform plan # show execution plan
terraform plan -out=tfplan # save plan to file
terraform plan -var="key=value"# pass variable inline
terraform plan -var-file="prod.tfvars"# use var file
terraform plan -target=resource.name # plan specific resource
terraform plan -destroy # plan a destroy
terraform apply # apply changes (prompts for confirmation)
terraform apply -auto-approve # apply without confirmation
terraform apply tfplan # apply saved plan file
terraform apply -var="key=value"# apply with inline var
terraform apply -target=resource.name # apply specific resource
terraform apply -replace=resource.name # force replace a resource
terraform destroy # destroy all managed resources
terraform destroy -auto-approve # destroy without confirmation
terraform destroy -target=resource.name # destroy specific resource
state
terraform state list # list all resources in state
terraform state show <resource># show state for a resource
terraform state mv <src><dst># move/rename resource in state
terraform state rm <resource># remove resource from state (stop managing)
terraform state pull # output current state as JSON
terraform state push <file># push a state file
terraform import <resource><id># import existing infra into state
terraform refresh # sync state with real infrastructure
output & inspect
terraform output # show all outputs
terraform output <name># show specific output
terraform output -json # outputs as JSON
terraform show # human-readable state or plan
terraform show -json tfplan # plan as JSON
terraform graph # generate dependency graph (dot format)
terraform graph | dot -Tsvg > graph.svg # render as SVG
validate & format
terraform validate # validate config syntax and logic
terraform fmt # format all .tf files in place
terraform fmt -check # check formatting (exit 1 if unformatted)
terraform fmt -diff # show formatting diff
terraform fmt -recursive # format subdirectories too
providers
terraform providers # show required providers
terraform providers lock # update dependency lock file
terraform providers mirror <dir># download providers to local dir
console & debug
terraform console # interactive expression evaluator> var.region # inspect a variable> local.name # inspect a local> aws_instance.web.id # inspect a resource attribute> cidrsubnet("10.0.0.0/16", 8, 1) # test functions
TF_LOG=DEBUG terraform apply # verbose logging
TF_LOG=TRACE terraform plan # maximum verbosity
TF_LOG_PATH=./tf.log terraform plan # log to file
output"instance_ip" {
value=aws_instance.web.public_ipdescription="Public IP of web instance"
}
output"db_password" {
value=var.db_passwordsensitive=true
}
# plan and apply in one step (CI-safe)
terraform plan -out=tfplan && terraform apply tfplan
# target a single resource for quick iteration
terraform apply -target=aws_instance.web
# check what would be destroyed before running destroy
terraform plan -destroy
# import an existing resource
terraform import aws_s3_bucket.logs my-existing-bucket-name
# unlock state if locked (use with caution)
terraform force-unlock <lock-id># list workspaces and switch
terraform workspace list
terraform workspace selectprod