Terraform Cheat Sheet
Terraform Cheat Sheet
Examples
spacelift.io/blog/terraform-commands-cheat-sheet
Sometimes you just want to get straight to the commands you need to use with a particular
tool, without having to trawl through all the documentation. In this post, I’ll highlight the
commonly used commands used on the Terraform CLI so you can get straight into the action
without the pain! You will also find here a Terraform Cheat Sheet PDF version to download.
This Terraform command reference guide was written using the latest version of Terraform
v.1.3.7. New commands and subcommands can be added and depreciated over time with
different versions, but should not change too dramatically. You can get the latest version of
Terraform here.
Note: New versions of Terraform are placed under the BUSL license, but everything created
before version 1.5.x stays open-source. OpenTofu is an open-source version of Terraform
that expands on Terraform’s existing concepts and offerings. It is a viable alternative to
HashiCorp’s Terraform, being forked from Terraform version 1.5.6.
Get Help
terraform -help — Get a list of available commands for execution with descriptions. Can
be used with any other subcommand to get more information.
terraform fmt -help — Display help options for the fmt command.
terraform fmt — Format your Terraform configuration files using the HCL language
standard.
1/10
terraform fmt --recursive — Also format files in subdirectories
terraform fmt --diff — Display differences between original configuration files and
formatting changes.
terraform fmt --check — Useful in automation CI/CD pipelines, the check flag can be
used to ensure the configuration files are formatted correctly, if not the exit status will be non-
zero. If files are formatted correctly, the exit status will be zero.
terraform init -lock=false — Initialize the working directory, don’t hold a state lock
during backend migration.
terraform init -input=false — Initialize the working directory, and disable interactive
prompts.
terraform get — Download and installs modules needed for the configuration.
terraform get -update — Check the versions of the already installed modules against the
available modules and installs the newer versions if available.
2/10
terraform validate -json — To see easier the number of errors and warnings that you
have.
terraform plan -out=<path> — Save the plan file to a given path. Can then be passed to
the terraform apply command.
terraform plan -destroy — Create a plan to destroy all objects rather than the usual
actions.
terraform apply -auto-approve — Apply changes without having to interactively type ‘yes’
to the plan. Useful in automation CI/CD pipelines.
terraform apply <planfilename> — Provide the file generated using the terraform plan
-out command. If provided, Terraform will take the actions in the plan without any
confirmation prompts.
terraform apply -lock=false — Do not hold a state lock during the Terraform apply
operation. Use with caution if other engineers might run concurrent commands against the
same workspace.
3/10
terraform destroy -target=”module.appgw.0" — Destroy only the targeted resource.
terraform show <path to statefile> — If you want to read a specific state file, you can
provide the path to it. If no path is provided, the current state file is shown.
terraform state list — Lists out all the resources that are tracked in the current state file.
terraform state mv — Move an item in the state, for example, this is useful when you need
to tell Terraform that an item has been renamed, e.g. terraform state mv vm1.oldname
vm1.newname
terraform state pull > state.tfstate — Get the current state and outputs it to a local
file.
terraform state push — Update remote state from the local state file.
4/10
terraform state replace-provider hashicorp/azurerm
customproviderregistry/azurerm — Replace a provider, useful when switching to using a
custom provider registry.
terraform state rm — Remove the specified instance from the state file. Useful when a
resource has been manually deleted outside of Terraform.
terraform state show <resourcename> — Show the specified resource in the state file.
5/10
the same infrastructure between different development stages, e.g. Dev / UAT / Production,
or different internal teams.
terraform workspace new <workspace name> — Create a new workspace with a specified
name.
terraform output -state=<path to state file> — List the outputs held in the specified
state file. -state option is ignored when the remote state is used.
terraform output -json — List the outputs held in your state file in JSON format to make
them machine-readable.
terraform output vm1_public_ip — List a specific output held in your state file.
terraform logout — Remove the credentials that are stored locally after logging in, by
default for Terraform Cloud (app.terraform.io).
6/10
terraform logout <hostname> — Remove the credentials that are stored locally after
logging in for the specified hostname.
terraform graph -plan=tfplan — Produce a dependency graph using a specified plan file
(generated using terraform plan -out=tfplan).
terraform graph -type=plan — Specify the type of graph to output, either plan, plan-
refresh-only, plan-destroy, or apply.
terraform graph -draw-cycles — You can see if there are any dependency cycles
between the resources.
🙂
terraform console — Allow testing and exploration of expressions on the interactive
console using the command line. e.g. 1+2
With the terraform console command, you have the ability to test different pieces of code.
All you have to do is write terraform console, and then you can write HCL code.
terraform console
# The below command will merge list elements into a string, separating them with
commas.
> join(",",["foo","bar"])
"foo,bar"
# You can use resource parameters to get details about them. With the below command,
we will get the public ip of an ec2 instance called my_ec2
> aws_instance.my_ec2.public_ip
3.153.2.10
7/10
terraform -chdir=”../dev” apply
Shell Tab-completion
Terraform also comes with an optional Shell Tab-completion. It can be useful if you are just
starting out with Terraform. However, Terraform CLI is pretty lightweight, and you won’t
usually reach very long commands.
terraform -install-autocomplete
After that you will need to resource your profile. This is done by either closing and opening
the terminal, or by running source path_to_your_profile.
What is Terraform?
Terraform is an infrastructure as code (IaC) tool developed by HashiCorp, which was initially
open-source but recently switched to a BSL. It uses a declarative language called HashiCorp
Configuration Language (HCL) to define infrastructure resources. Terraform supports a wide
variety of cloud providers such as AWS, Microsoft Azure, Google Cloud, or Oracle Cloud
Infrastructure, but it can also be used with Kubernetes, Helm, and many others. It is stateful,
keeping track of the deployed infrastructure using a state file.
The Terraform Command Line Interface (CLI), is a command-line tool that provides a simple
way for users to interact with the infrastructure components defined in the Terraform
configuration. It offers multiple commands, from initializing your terraform directory to
planning, applying, and destroying infrastructure resources. With the Terraform CLI, you also
have the ability to check outputs, do state-related operations, and even test different
expressions.
Key Points
8/10
This guide should help you get straight to the command you need when using the Terraform
CLI!
If you need more help with Terraform, I encourage you to check the following blog posts:
How to Automate Terraform Deployments, and 12 Terraform Best Practices.
Written by
Flavius Dinu
Flavius is a passionate Developer Advocate with an Infrastructure as Code mindset and
expertise in DevOps & Cloud Engineering. He holds ITIL Foundation Certificate in IT Service
Management and Hashicorp Terraform Associate Certification. He currently works at
Spacelift, and in his free time, he blogs at techblog.flaviusdinu.com, where he provides
tutorials, tips, and tricks for all levels of experience based on his exposure.
Jack Roper
9/10
Jack Roper is a highly experienced IT professional with close to 20 years of experience,
focused on cloud and DevOps technologies. He specializes in Terraform, Azure, Azure
DevOps, and Kubernetes and holds multiple certifications from Microsoft, Amazon, and
Hashicorp. Jack enjoys writing technical articles for well-regarded websites.
10/10