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:
1 | terraform init |
2. terraform plan
- Description: Generates an execution plan showing the actions Terraform will take to apply the current configuration.
- Example:
1 | terraform plan -out=tfplan |
3. terraform apply
- Description: Applies the changes described in a Terraform execution plan to the infrastructure.
- Example:
1 | terraform apply tfplan |
4. terraform destroy
- Description: Destroys the Terraform-managed infrastructure.
- Example:
1 | terraform destroy |
5. terraform output
- Description: Displays the output values defined in the Terraform configuration.
- Example:
1 | terraform output example_output |
6. terraform fmt
- Description: Rewrites Terraform configuration files to a canonical format.
- Example:
1 | terraform fmt |
7. terraform get
- Description: Downloads and installs modules for the configuration in the current directory.
- Example:
1 | terraform get |
8. terraform graph
- Description: Creates a visual representation of the Terraform configuration or execution plan.
- Example:
1 | terraform graph | dot -Tpng > graph.png |
9. terraform import
- Description: Imports existing infrastructure into Terraform.
- Example:
1 | terraform import aws_instance.example i-abcd1234 |
10. terraform providers
- Description: Prints a tree of the providers used in the configuration.
- Example:
1 | terraform providers |
11. terraform show
- Description: Inspects Terraform state or plan.
- Example:
1 | terraform show -no-color |
12. terraform workspace
- Description: Manages workspaces for multiple environments.
- Example:
1 2 | terraform workspace new dev terraform workspace select prod |
13. terraform version
- Description: Prints the Terraform version.
- Example:
1 | terraform version |
14. terraform console
- Description: Opens an interactive console for evaluating Terraform expressions.
- Example:
1 | terraform console |
15. terraform debug
- Description: Debug output management.
- Example:
1 | terraform debug |
16. terraform state mv
- Description: Move an item within the Terraform state.
- Example:
1 | terraform state mv aws_instance.example aws_instance.new_example |