If you are looking for the terraform commands, then you are in the right place; this Terraform Cheat Sheet and Terraform commands will simply help you to understand all Terraform commands that you need to run daily; why not learn all terraform commands from basics to becoming a terraform pro.
Terraform is an infrastructure as a code tool to build and change the infrastructure effectively and simpler way. With Terraform, you can work with various cloud providers such as Amazon AWS, Oracle, Microsoft Azure, Google Cloud, and many more.
Let’s dive into this Terraform Cheatsheet and Terraform commands.
Table of Content
- Prerequisites
- Terraform Commands
- Quick Glance of Terraform CLI Commands
- Terraform commands walkthrough
- Conclusion
Prerequisites
- Windows Machine or Ubuntu Machine. This tutorial will use Windows Machine.
- Terraform Installed on Windows Machine or Ubuntu Machine. If you don’t have it already see here Terraform on Windows Machine / Terraform on Ubuntu Machine
Terraform Commands
Let’s kick off this tutorial by learning all the Terraform commands you need to use. The Terraform command-line interface or Terraform CLI can be used via terraform command, which accepts a variety of subcommands such as terraform init or terraform plan.
- terraform init: It initializes the provider, module version requirements, and backend configurations.
- terraform init -input=true ➔ You can need to provide the inputs on the command line else terraform will fail.
- terraform init -lock=false ➔ Disable lock of terraform state file but this is not recommended
- terraform init -upgrade ➔ Upgrades Terraform modules and Terraform plugins
- terraform get: terraform get command downloads and updates modules mentioned in the root module.
- terraform plan: terraform plan command determines the state of all resources and compares them with real or existing infrastructure. It uses terraform state file data to compare and provider API to check.
- terraform plan -compact-warnings ➔ Provides the summary of warnings
- terraform plan -out=path ➔ Saves the execution plan on the specific directory.
- terraform plan -var-file= abc.tfvars ➔ To use specfic terraform.tfvars specified in the directory.
- terraform apply: To apply the changes in a specific cloud such as AWS or Azure.
- terraform apply -backup=path ➔ To backup the Terraform state file
- terraform apply -lock=true ➔ Locks the state file
- terraform apply -state=path ➔ prompts to provide the path to save the state file or use it for later runs.
- terraform apply -var-file= abc.tfvars ➔ Enter the specific terraform.tfvars which contains environment-wise variables.
- terraform apply -auto-approve ➔ This command will not prompt to approve the apply command.
- terraform destroy: It will destroy terraform-managed infrastructure or the existing enviornment created by Terraform.
- terraform destroy -auto-approve ➔ This command will not prompt to approve the destroy command.
- terraform console: Provides interactive console to evaluate the expressions such as join command or split command.
- terraform console -state=path ➔ Path to local state file
- terraform fmt: terraform fmt command formats the configuration files in the proper format.
- terraform fmt -check ➔ Checks the input format
- terraform fmt – recursive ➔ It formats Terraform configuration files stored in subdirectories.
- terraform fmt – diff ➔ displays the difference between the current and previous formats.
- terraform validates: It validates the Terraform configuration files.
- terraform validate -json ➔ Output is in json format
- terraform graph: terraform graph generates a visual representation of the execution plan in graph form.
- terraform graph -draw-cycles
- terraform graph -type=plan
- terraform output: terraform output command extracts the values of an output variable from the state file.
- terraform output -json
- terraform output -state=path
- terraform state list: It lists all the resources present in the state file created or imported by Terraform.
- terraform state list – id=id ➔ This command will search for a particular resource using resource id in Terraform state file.
- terraform state list -state=path ➔ This command will prompt you to provide the path of the state file and then provides the list of all resources in terraform state file.
- terraform state show: It shows attributes of specific resources.
- terraform state show -state=path ➔ This command will prompt you to provide the path and then provide the attributes of specific resources.
- terraform import: This command will import existing resources from infrastructure which was not created using terraform but will be imported in terraform state file and will be included in Terraform next time we run it.
- terraform refresh: It will reconcile the Terraform state file. Whatever resource you created using terraform and if they are manually or by any means modified, the refresh will sync them in the state file.
- terraform state rm: This command will remove the resources from the Terraform state file without actually removing the existing resources.
- terraform state mv: This command moves the resources within the Terraform state file from one location to another
- terraform state pull: This command will manually download the Terraform state file from a remote state in your local machine.
- terraform state push: This command will manually upload the local state file to the remote state.
Quick Glance of Terraform CLI Commands
Previously, you learned all the Terraform commands individually, but let’s check out the below Terraform CLI commands table to help you group each Terraform command category-wise.
Initialize | Provision | Modify Config | Check infra | Manipulate State |
---|---|---|---|---|
terraform init | terraform plan | terraform fmt | terraform graph | terraform state list |
terraform get | terraform apply | terraform validate | terraform output | terraform state show |
terraform destroy | terraform console | terraform state show | terraform state mv/rm | |
terraform state list | terraform state pull/push | |||
Terraform commands walkthrough
Now that you have a sound idea about each of the terraform commands let’s walk through some of the Terraform commands with the practicals.
- To find out the Terraform version run
terraform -version
command.
terraform -version # It gives Terraform Version information

- Now Initialize the terraform by running the
terraform init
command in same working directory where you have all the above terraform configuration files.
terraform init

- Next run the
terraform plan
command. This command provides the blueprint of what all resources will be deployed before deploying actually.
terraform plan

- Validate the terraform configuration files by running the
terraform validate
command.
terraform validate

- Now run the
Terraform show
command provides the human readable output of state file that gets generated only after terraform plan command.
terraform show

- To list all resources within terraform state file run the
terraform state list
command.
terraform state list

- Finally use
terraform apply
command to provision the resources.
terraform apply

- To provide graphical view of all resources in configuration files run terraform graph command.
terraform graph

- To Destroy the resources that are provisioned using Terraform run Terraform destroy command.
terraform destroy # Destroys all your resources or the one which you specified

Conclusion
In this tutorial, you covered all the terraform commands that are useful for beginners and experienced professionals and will be of massive use.
So which Terraform command do you use the most?