💻 Terraform CLI Cheat Sheet
Command | Subcommand | Explanation | Example |
---|---|---|---|
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 |
state | list | Lists resources in the Terraform state. | terraform state list |
state | show | Outputs the attributes of a resource in the Terraform state. | terraform state show aws_instance.example |
state | pull | Fetches the current state from remote storage and saves it locally. | terraform state pull |
state | push | Updates remote state from a local state file. | terraform state push |
state | rm | Removes one or more resources from the Terraform state. | terraform state rm aws_instance.example |
state | mv | Moves 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 |
workspace | new | Creates a new Terraform workspace. | terraform workspace new dev |
workspace | list | Lists available Terraform workspaces. | terraform workspace list |
workspace | select | Selects an existing Terraform workspace to use. | terraform workspace select dev |
workspace | delete | Deletes a Terraform workspace. | terraform workspace delete dev |
workspace | show | Shows the current Terraform workspace. | terraform workspace show |
workspace | rename | Renames 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 |
providers | schema | Prints the schema of a provider. | terraform providers schema aws |
providers | lock | Locks the provider versions used in the configuration. | terraform providers lock |
providers | unlock | Unlocks the provider versions used in the configuration. | terraform providers unlock |
providers | mirror | Creates a mirror of a module registry for private use. | terraform providers mirror |
providers | unmirror | Removes a mirror of a module registry. | terraform providers unmirror |
providers | upgrade | Upgrades 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.