Terraform Cheat Sheet

Terraform Cheat Sheet

The Terraform commands cheat sheet (PDF) is an attempt to document, classify, and organize the most used Terraform commands. I will try to update this with the latest Terraform commands as updates come out, so make sure to bookmark this page or save it to your team’s documentation.

Terraform Cheat Sheet PDF

Terraform Cheat Sheet

Apply Terraform Resources

# Show proposed changes that an apply would perform
terraform plan

# Apply all terraform changes
terraform apply

# Apply a specific terraform resource
terraform apply -target=<resource>
i.e. terraform apply -target=aws_db_instance.cmdb

# Use a variables file with your terraform apply/plan
terraform apply -var-file=<path_to_vars_file>

Destroy Terraform Resources

# Destroy all terraform resources
terraform destroy

# Destroy select terraform resources
terraform destroy -target=<resource>
i.e. terraform destroy -target=aws_db_instance.cmdb

# Dry run terraform destroy
terraform plan -destroy

# Mark a resource as tainted and force a destroy/recreate
terraform taint <resource>

# Mark a tainted resource as clean
terraform untaint <resource>

Terraform Debug

# Show current terraform resources
terraform show

# Validate your terraform code
terraform validate

# Show all terraform outputs
terraform output

# Test resource interpolation (uses your state file)
echo '<resource_expression>' | terraform console
i.e. echo 'aws_db_instance.cmdb.allocated_storage == "500" ? "lots" : "little"' | terraform console

# Visualize Terraform Dependency Graph (requires graphviz)
terraform graph | dot -Tsvg > graph.svg

Terraform Modules/Providers

# Initialize Terraform backend & download specified 
terraform init

# Clear out old modules/plugins & re-initialize terraform 
rm -rf ./.terraform/ && terraform init

# Pull modules into your .terraform directory
terraform get -update=true

# Show terraform providers
terraform providers

Terraform State

# Pull the remote state
terraform state pull > terraform.tfstate

# Push local state to remote state (uses file: terraform.tfstate)
terraform state push

# Update local state file against real resources
terraform refresh

# Tell Terraform a resource has been moved into a module
terraform state mv <resource> <module>
i.e. terraform state mv aws_db_instance.cmdb module.mydb

# Import an existing resource into terraform state
# Note that this is different syntax for every resource
terraform import <resource>
i.e. terraform import aws_instance.web1 i-abcd1234

Terraform Workspaces

# Create a terraform workspace
terraform workspace new <workspace>

# Use a terraform workspace
terraform workspace select <workspace>

# List terraform workspaces
terraform workspace list

# Show current terraform workspace
terraform workspace show


If you liked the Terraform Cheat Sheet, be sure to bookmark this page, add it to your team’s documentation, share it on social media and take a look at some of the other cheat sheets we’ve created:

SwissArmyDevOps is trying to create additional content to make the barrier to entry much softer for Terraform. If you have suggestions for guides that should be created or tools that should be made I’d appreciate you dropping a comment in the comment section below.

2 thoughts on “Terraform Cheat Sheet”

  1. Pingback: Docker Cheat Sheet - Swiss Army DevOps

  2. Pingback: Kubernetes (kubectl) Cheat Sheet - Swiss Army DevOps

Leave a Reply

Your email address will not be published. Required fields are marked *