Terraform Infrastructure as code

Version 1.16 · source

Start here

terraform init
Download providers and modules
terraform fmt
Format the files
terraform validate
Check the configuration
terraform plan -out=<file>
Preview and save changes
terraform apply <file>
Apply the saved plan
terraform output
Show output values
terraform state list
List tracked resources
terraform destroy
Remove everything managed

Concepts

  1. Config .tf files
  2. Working dir providers, modules
  3. Plan diff vs. state
  4. Infrastructure state updated

Workflow

terraform init
Prepare the working directory
Run again after adding a provider, module or backend.
terraform init -upgrade
Update providers and modules
terraform init -reconfigure
Ignore saved backend setup
terraform init -migrate-state
Move state to a new backend
terraform validate
Check syntax and consistency
terraform plan
Preview changes
terraform plan -out=<file>
Save the plan to a file
terraform apply
Plan, confirm, then apply
terraform apply <planfile>
Apply a saved plan
terraform apply -auto-approve
Skip the confirmation prompt
Meant for CI; never use it without a reviewed plan.
terraform destroy
Destroy all managed resources
terraform plan -destroy
Preview a destroy
terraform apply -refresh-only
Sync state with reality
terraform fmt
Rewrite files in canonical style
terraform fmt -recursive -check
Check formatting incl. subdirs

Targeting & plans

terraform plan -target=<addr>
Limit to one resource
For exceptions only; it can leave the rest out of sync.
terraform apply -replace=<addr>
Force recreating a resource
terraform plan -refresh=false
Skip reading real state
terraform plan -parallelism=<n>
Limit concurrent operations
terraform plan -detailed-exitcode
Exit 2 when changes exist
terraform show <planfile>
Display a saved plan
terraform show -json <planfile>
Plan as JSON
terraform graph
Dependency graph (DOT)

Variables & outputs

terraform plan -var '<name>=<value>'
Set one variable
terraform plan -var-file=<file>
Load variables from a file
TF_VAR_<name>=<value>
Set a variable via environment
terraform.tfvars
Loaded automatically
*.auto.tfvars files are also loaded automatically.
terraform output
Show all outputs
terraform output <name>
Show a single output
terraform output -raw <name>
Plain value, no quotes
terraform output -json
Outputs as JSON
terraform console
Try expressions interactively

State

terraform state list
List tracked resources
terraform state show <addr>
Show one resource's attributes
terraform state mv <src> <dst>
Rename or move in state
terraform state rm <addr>
Forget a resource, keep it real
terraform state pull
Print the state as JSON
terraform state push <file>
Upload a state file
terraform import <addr> <id>
Adopt an existing resource
The resource block must exist in the config first; an import block is the declarative alternative.
terraform taint <addr>
Mark for replacement
Superseded by apply -replace.
terraform untaint <addr>
Clear the taint mark
terraform force-unlock <id>
Release a stuck state lock
terraform refresh
Update state from reality
Deprecated; use apply -refresh-only.

Workspaces

terraform workspace list
List workspaces
terraform workspace show
Show the current one
terraform workspace new <name>
Create and switch
terraform workspace select <name>
Switch workspace
terraform workspace delete <name>
Delete a workspace

Providers & modules

terraform providers
List required providers
terraform providers lock
Write the dependency lock file
terraform get
Download modules
terraform modules
List declared modules
terraform version
Show Terraform version
terraform login
Save a remote host token
terraform -chdir=<dir> <cmd>
Run in another directory

Recipes

Reviewed deploy
terraform initterraform fmt -checkterraform validateterraform plan -out=tfplanterraform apply tfplan
Per-environment variables
terraform workspace select prodterraform plan -var-file=prod.tfvars -out=tfplanterraform apply tfplan
Rename without recreating
terraform state mv aws_instance.old aws_instance.new# update the resource name in the .tf fileterraform plan   # expect no changes
Replace one broken resource
terraform plan -replace=aws_instance.webterraform apply -replace=aws_instance.web