Terraform Commands with Descriptions and Examples

Here’s a detailed description for each Terraform command along with an example showcasing its usage:

1. terraform init

  • Description: Initializes a new or existing Terraform working directory, preparing it for use.
  • Example:
  terraform init

2. terraform plan

  • Description: Generates an execution plan showing the actions Terraform will take to apply the current configuration.
  • Example:
  terraform plan -out=tfplan

3. terraform apply

  • Description: Applies the changes described in a Terraform execution plan to the infrastructure.
  • Example:
  terraform apply tfplan

4. terraform destroy

  • Description: Destroys the Terraform-managed infrastructure.
  • Example:
  terraform destroy

5. terraform output

  • Description: Displays the output values defined in the Terraform configuration.
  • Example:
  terraform output example_output

6. terraform fmt

  • Description: Rewrites Terraform configuration files to a canonical format.
  • Example:
  terraform fmt

7. terraform get

  • Description: Downloads and installs modules for the configuration in the current directory.
  • Example:
  terraform get

8. terraform graph

  • Description: Creates a visual representation of the Terraform configuration or execution plan.
  • Example:
  terraform graph | dot -Tpng > graph.png

9. terraform import

  • Description: Imports existing infrastructure into Terraform.
  • Example:
  terraform import aws_instance.example i-abcd1234

10. terraform providers

  • Description: Prints a tree of the providers used in the configuration.
  • Example:
  terraform providers

11. terraform show

  • Description: Inspects Terraform state or plan.
  • Example:
  terraform show -no-color

12. terraform workspace

  • Description: Manages workspaces for multiple environments.
  • Example:
  terraform workspace new dev
  terraform workspace select prod

13. terraform version

  • Description: Prints the Terraform version.
  • Example:
  terraform version

14. terraform console

  • Description: Opens an interactive console for evaluating Terraform expressions.
  • Example:
  terraform console

15. terraform debug

  • Description: Debug output management.
  • Example:
  terraform debug

16. terraform state mv

  • Description: Move an item within the Terraform state.
  • Example:
  terraform state mv aws_instance.example aws_instance.new_example