DEV Community

Cover image for Terraform Commands Cheat Sheet
sam-nash
sam-nash

Posted on

Terraform Commands Cheat Sheet

This post serves as a comprehensive cheat sheet for commonly used Terraform commands, offering detailed explanations and where possible with some practical examples. Whether you're initializing a workspace, creating execution plans, managing state files, or destroying resources, I think this guide has you covered. Perfect for beginners and seasoned practitioners alike, it ensures you can leverage Terraform to its full potential.

Terraform General Commands

terraform version : Show the Terraform version.
terraform init : Initialize the working directory and download the required providers and plugins.
terraform init -reconfigure : Reconfigure the backend and update the configuration.
terraform init -get-plugins=false : Initialize without downloading plugins (deprecated, as plugins are automatically managed as providers).
terraform init -verify-plugins=false : Initialize without verifying plugin signatures (deprecated, use with caution).
terraform fmt : Format the Terraform configuration files to canonical format.
terraform fmt -recursive : Format all files in the directory and subdirectories recursively.
terraform fmt -check : Check the format of the Terraform configuration files. Does not modify files.
terraform validate : Validate the syntax and consistency of Terraform configuration files.
terraform validate -backend=false : Validate configuration without initializing the backend.
terraform validate -json : Validate configuration and output results in JSON format.

Terraform Plan Commands

terraform plan : Create an execution plan showing the changes Terraform will make.
terraform plan -out=tf_plan.out : Save the generated execution plan to a file.
terraform plan -destroy : Create a plan to destroy all infrastructure.
terraform plan -var-file=my_vars.tfvars : Include variables from the specified file in the plan.
terraform plan -var=my_variable=value : Override variables with specific values for the plan.
terraform plan -parallelism=4 : Limit the number of parallel resource operations (default is 10).

Terraform Apply Commands

terraform apply : Apply changes based on the current configuration.
terraform apply tf_plan.out : Apply changes based on a saved execution plan file.
terraform apply -auto-approve : Apply changes without prompting for approval.
terraform apply -target=resource_address : Apply changes only to the specified resource(s).
terraform apply -var=my_variable=value : Override variables with specific values during apply.
terraform apply -var-file=my_vars.tfvars : Use variables from the specified file during apply.
terraform apply -lock=true : Lock the statefile before applying changes (default behavior).
terraform apply -lock-timeout=20s : Set a timeout for acquiring a state lock.
terraform apply -input=false : Suppress interactive input prompts.
terraform apply -replace=resource_address : Force replacement of a specific resource.
terraform apply -parallelism=4 : Limit the number of concurrent resource operations during apply.
terraform apply -refresh-only : Refresh the state without applying changes (available from Terraform 1.0).

Terraform Destroy Commands

terraform destroy : Destroy all managed infrastructure.
terraform destroy -auto-approve : Destroy resources without prompting for confirmation.
terraform destroy -target=resource_address : Destroy specific resource(s).
terraform destroy -var=my_variable=value : Override variables during destroy.
terraform destroy -var-file=my_vars.tfvars : Use variables from the specified file during destroy.

Terraform State Commands

terraform state list : List all resources in the state file.
terraform state show resource_address : Show the details of a specific resource.
terraform state mv resource_address new_resource_address : Move a resource to a new address.
terraform state rm resource_address : Remove a resource from the state file.
terraform state pull > terraform.tfstate : Download the current state file.
terraform state push terraform.tfstate : Upload a state file to a remote backend.
terraform state replace-provider source_provider target_provider : Replace a provider in the state file.

Terraform Import Commands

terraform import resource_address external_id : Import existing infrastructure into the Terraform state.

Terraform Workspace Commands

terraform workspace list : List all available workspaces.
terraform workspace show : Show the currently selected workspace.
terraform workspace new workspace_name : Create a new workspace.
terraform workspace select workspace_name : Switch to a specific workspace.
Terraform Graph Commands
terraform graph : Generate a visual representation of the dependency graph.
terraform graph -type=plan : Generate a graph based on the execution plan.
terraform graph -type=apply : Generate a graph based on the apply plan.
terraform graph -draw-cycles : Highlight cyclic dependencies in the graph.

Terraform Output Commands

terraform output : Display all output variables.
terraform output -json : Display output variables in JSON format.
terraform output variable_name : Show the value of a specific output variable.

Terraform Show Commands

terraform show : Show the current state or plan.
terraform show -json : Output the state or plan in JSON format.

Other Commands

terraform refresh : Update the state file to reflect the current state of resources.
terraform taint resource_address : Mark a resource for recreation on the next apply.
terraform untaint resource_address : Remove the taint from a resource.
terraform providers : List providers used in the configuration.
terraform providers schema : Display provider schema details (available in recent versions).

Top comments (0)