CLI Commands Cheat Sheet
Terraform Command-Line Interface (CLI) offers a variety of commands to help you manage and manipulate your Terraform projects. This cheat sheet provides a quick reference to the most commonly used commands.
Key Terraform Commands
-
terraform init:- Initializes a Terraform working directory by installing necessary plugins.
- Usage:
terraform init
-
terraform plan:- Creates an execution plan, showing what Terraform will do when you run
terraform apply. - Usage:
terraform plan
- Creates an execution plan, showing what Terraform will do when you run
-
terraform apply:- Applies the changes required to reach the desired state of the configuration.
- Usage:
terraform apply
-
terraform destroy:- Destroys the Terraform-managed infrastructure.
- Usage:
terraform destroy
-
terraform fmt:- Automatically formats the Terraform configuration files to a canonical format and style.
- Usage:
terraform fmt
-
terraform validate:- Validates the configuration files for syntax correctness.
- Usage:
terraform validate
-
terraform output:- Displays the outputs defined in your Terraform project.
- Usage:
terraform output
-
terraform refresh:- Updates the state file of your Terraform configuration with the actual state of the configured resources.
- Usage:
terraform refresh
-
terraform show:- Displays human-readable output from a state or plan file.
- Usage:
terraform show
-
terraform state:- Offers several subcommands for advanced state management, like
list,mv,pull,push,rm. - Usage:
terraform state [subcommand]
- Offers several subcommands for advanced state management, like
-
terraform workspace:- Manages workspaces, allowing you to use the same configuration for multiple sets of resources.
- Usage:
terraform workspace [subcommand]
Notes
- Always run
terraform planbefore applying changes to understand the impact. - Use
terraform fmtandterraform validateto ensure your code is clean and error-free. - Workspaces can be helpful to manage different environments (development, staging, production) with the same configuration.