Skip to content

💻 Terraform CLI Cheat Sheet

CommandSubcommandExplanationExample
init-Initializes a new or existing Terraform working directory, downloads and installs the required providers and modules.terraform init
plan-Generates an execution plan, displaying the changes Terraform will make to your infrastructure without actually applying those changes.terraform plan
apply-Applies the changes required to reach the desired state of the configuration, creating, modifying, or destroying resources.terraform apply
destroy-Destroys the Terraform-managed infrastructure, removing all resources that were created.terraform destroy
validate-Validates the configuration files, checking for syntax errors and potential issues.terraform validate
fmt-Formats the Terraform configuration files to ensure consistent styling and indentation.terraform fmt
get-Retrieves and installs the latest versions of modules used in the configuration.terraform get
import-Imports existing infrastructure into Terraform, allowing you to manage resources that were not originally created using Terraform.terraform import aws_instance.example i-abcd1234
output-Reads the output variables from a state file and displays their values.terraform output
refresh-Updates the state file with the real-world infrastructure, detecting any changes that were made outside of Terraform.terraform refresh
taint-Marks a resource as tainted, forcing it to be destroyed and recreated on the next apply.terraform taint aws_instance.example
untaint-Removes the tainted state from a resource, allowing it to be managed normally.terraform untaint aws_instance.example
statelistLists resources in the Terraform state.terraform state list
stateshowOutputs the attributes of a resource in the Terraform state.terraform state show aws_instance.example
statepullFetches the current state from remote storage and saves it locally.terraform state pull
statepushUpdates remote state from a local state file.terraform state push
statermRemoves one or more resources from the Terraform state.terraform state rm aws_instance.example
statemvMoves an item in the Terraform state from one address to another.terraform state mv aws_instance.example aws_instance.new
graph-Generates a visual representation of the Terraform resource graph as a DOT graph file.terraform graph | dot -Tsvg > graph.svg
workspacenewCreates a new Terraform workspace.terraform workspace new dev
workspacelistLists available Terraform workspaces.terraform workspace list
workspaceselectSelects an existing Terraform workspace to use.terraform workspace select dev
workspacedeleteDeletes a Terraform workspace.terraform workspace delete dev
workspaceshowShows the current Terraform workspace.terraform workspace show
workspacerenameRenames a Terraform workspace.terraform workspace rename dev new_dev
import-Imports existing resources into Terraform, allowing you to manage resources that were created outside of Terraform.terraform import aws_instance.example i-abcd1234
output-Reads the output variables from a state file and displays their values.terraform output
providersschemaPrints the schema of a provider.terraform providers schema aws
providerslockLocks the provider versions used in the configuration.terraform providers lock
providersunlockUnlocks the provider versions used in the configuration.terraform providers unlock
providersmirrorCreates a mirror of a module registry for private use.terraform providers mirror
providersunmirrorRemoves a mirror of a module registry.terraform providers unmirror
providersupgradeUpgrades provider versions in the configuration.terraform providers upgrade
force-unlock-Manually unlocks the state for a given workspace.terraform force-unlock LOCK_ID
debug-Prints Terraform debug information.terraform debug
version-Prints the Terraform version.terraform version

Please note that while this list covers the majority of commonly used Terraform commands and subcommands, there may be additional commands and subcommands specific to certain providers or plugins.