Terraform – Commands – Cheat Sheet

Terraform is very powerful. It can do what CloudFormation or Bicep could not. Yes. We can create a Spotify playlist with Terraform. That’s a serious statment and not a joke 🙂 Terraform architecture consists of Terraform Core and it supports providers as plugins. There are 1000s of providers including AWS, GCP, Azure, Oracle Cloud. Their documentation and tutorials are great too. HCL – Hashicorp Configuration Language is easy to understand and VS Code has great tooling to use Terraform.There are some concerns about the BSL license and that’s how OpenTofu was born.

CommandPurpose
terraform -hDisplays help information for Terraform commands.
terraform -install-autocompleteEnables tab completion for Terraform commands. Restart your terminal or source your shell profile. Supports all major shells – zsh, bash, fish etc.
terraform versionDisplays the current version of Terraform.
terraform initInitializes a Terraform configuration directory. Downloads providers/modules and configures backend
terraform init -reconfigureReconfigures the backend settings for a Terraform configuration. This will not migrate the state
terraform init -migrate-stateMigrates the Terraform state to a new backend.
terraform init -upgradeUpgrades all previously installed modules and plugins to the latest version.
terraform init -backend-config=prod.hcl -migrate-stateInitializes with a specific backend configuration file and migrates the state.
terraform getDownload or update modules specified in your configuration file. Only handles module dependencies
terraform providersLists the providers required by the configuration.
terraform fmtFormats the Terraform configuration files to a canonical format.
terraform fmt -recursiveFormats Terraform configuration files recursively in the directory structure.
terraform validateValidates the Terraform configuration for syntax and logical errors.
terraform validate -jsonOutputs the validation result in JSON format.
terraform planGenerates an execution plan showing what actions Terraform will take to achieve the desired state.
terraform plan -out my-aws-planSaves the generated execution plan to a file for later use with terraform apply.
terraform plan -var aws_region=us-east-1Overrides a variable value when creating the execution plan.
terraform plan -refresh-onlyCreates a plan to update the state file with any changes from the infrastructure without modifying it.
terraform plan -destroyCreates a plan to destroy all resources managed by the configuration.
terraform applyApplies the changes required to reach the desired state of the configuration.
terraform apply -destroyApplies the destruction of all resources managed by the configuration.
terraform apply my-aws-planApplies the changes specified in a saved execution plan file.
terraform apply -auto-approveApplies changes without prompting for user confirmation.
terraform apply -lock-timeout=60sSets a timeout for locking the state file when applying changes.
terraform apply -replace=”aws_instance.web_server”Forces Terraform to replace a specific resource during apply.
terraform apply -refresh=falseSkips state refresh so apply can be quick. Not recommended for production
terraform destroyDestroys all resources managed by the Terraform configuration.
terraform state listLists all resources in the current Terraform state.
terraform state show aws_instance.web_serverDisplays detailed information about a specific resource in the state.
terraform import aws_instance.aws_linux i-0e882c5d99743d145Imports an existing infrastructure resource into Terraform state.
terraform workspace showDisplays the name of the current workspace.
terraform workspace listLists all workspaces in the current directory.
terraform workspace select stageSwitches to a different workspace. In this case a workspace named “stage”
terraform workspace new uatCreates a new workspace name “uat”
terraform workspace delete devDeletes an existing workspace
terraform state mv <source> <destination>Moves an item in the Terraform state.
terraform state pullRetrieves and outputs the state from a remote state backend.
terraform state pushUpdates the remote state with the local state.
terraform state rm <resource>Removes resources from the Terraform state.
terraform state show <resource>Displays detailed information about a specific resource in the state.
terraform state replace-providerReplaces provider references in the Terraform state.
terraform consoleOpens an interactive console for evaluating Terraform expressions. Acquires lock for the state
terraform loginAuthenticates the Terraform CLI with a Terraform Cloud or Enterprise account.
terraform outputDisplays the outputs defined in the Terraform configuration.
terraform output public_ipDisplays the value of a specific output, such as public_ip.
terraform graphGenerates a visual graph (.DOT file) of the Terraform resources and their dependencies. Uses the state file to generate the dependencies. If state file is not available, it can use the configuration file.
terraform force-unlock LOCK_IDRemoves the lock from terraform state. Forcing an unlock can lead to state corruption, if another operation is still running.
terraform module listLists all modules used in the configuration
JSON OutputMost commands above support json output with -json .
terraform state list -json
terraform version -json
terraform providers schema -json
terraform show -json <planfile>
terraform output -json
Scroll to Top