Skip to main content

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

  1. terraform init:

    • Initializes a Terraform working directory by installing necessary plugins.
    • Usage: terraform init
  2. terraform plan:

    • Creates an execution plan, showing what Terraform will do when you run terraform apply.
    • Usage: terraform plan
  3. terraform apply:

    • Applies the changes required to reach the desired state of the configuration.
    • Usage: terraform apply
  4. terraform destroy:

    • Destroys the Terraform-managed infrastructure.
    • Usage: terraform destroy
  5. terraform fmt:

    • Automatically formats the Terraform configuration files to a canonical format and style.
    • Usage: terraform fmt
  6. terraform validate:

    • Validates the configuration files for syntax correctness.
    • Usage: terraform validate
  7. terraform output:

    • Displays the outputs defined in your Terraform project.
    • Usage: terraform output
  8. terraform refresh:

    • Updates the state file of your Terraform configuration with the actual state of the configured resources.
    • Usage: terraform refresh
  9. terraform show:

    • Displays human-readable output from a state or plan file.
    • Usage: terraform show
  10. terraform state:

    • Offers several subcommands for advanced state management, like list, mv, pull, push, rm.
    • Usage: terraform state [subcommand]
  11. terraform workspace:

    • Manages workspaces, allowing you to use the same configuration for multiple sets of resources.
    • Usage: terraform workspace [subcommand]

Notes

  • Always run terraform plan before applying changes to understand the impact.
  • Use terraform fmt and terraform validate to ensure your code is clean and error-free.
  • Workspaces can be helpful to manage different environments (development, staging, production) with the same configuration.