Terraform is a powerful Infrastructure as Code (IaC) tool developed by HashiCorp that allows you to define, provision, and manage infrastructure using declarative configuration files. Here’s a detailed overview of commonly used Terraform commands, along with explanations and examples to help you understand how they work.
Basic Terraform Commands
1. terraform init
Description: Initializes a Terraform working directory. This command is used to set up the backend, download required provider plugins, and set up the working environment for Terraform.
Usage: Run this command first before using any other Terraform commands in a new or existing configuration.
Example:
terraform init
Explanation: This command scans the configuration files, checks the required providers (e.g., AWS, Azure), and installs them into the .terraform directory. It also sets up the backend configuration if specified.
2. terraform plan
Description: Creates an execution plan, showing what actions Terraform will take to achieve the desired state of the infrastructure.
Usage: Use this command to preview changes without actually applying them.
Example:
terraform plan
Explanation: Terraform compares the current state of the infrastructure with the desired state specified in the configuration files and displays what will be created, modified, or destroyed.
3. terraform apply
Description: Applies the changes required to reach the desired state of the configuration, as shown in the execution plan.
Usage: After reviewing the plan, run this command to make changes.
Example:
terraform apply
Explanation: Terraform will execute the steps needed to create, update, or destroy infrastructure resources as defined. You can also pass the -auto-approve flag to skip the approval step.
4. terraform destroy
Description: Destroys the infrastructure managed by Terraform. This command is used to clean up resources created by a particular configuration.
Usage: Use this command when you want to remove all resources defined in your configuration.
Example:
terraform destroy
Explanation: Terraform reads the current state and removes all resources that were created. You can use flags like -target to destroy specific resources.
5. terraform validate
Description: Validates the syntax and configuration files without creating an execution plan or applying changes.
Usage: Use this command to ensure your configuration files are correct.
Example:
terraform validate
Explanation: This checks for syntax errors and validates all required arguments for the resources specified.
6. terraform fmt
Description: Formats the Terraform configuration files to a canonical format and style.
Usage: Use this command to clean up your configuration files to make them more readable.
Example:
terraform fmt
Explanation: Terraform applies consistent formatting to your files, making them easier to read and share with others.
7. terraform state
Description: Commands under state allow you to manipulate the state file, which is used by Terraform to keep track of resources.
Usage: Commands include list, show, rm, etc., to manage resources.
Example:
terraform state list
Explanation: This command lists all resources in the state file, showing what is currently being tracked by Terraform.
8. terraform output
Description: Extracts the output values of a configuration, which can be used in other parts of your infrastructure.
Usage: Use this to view the output variables defined in your configuration.
Example:
terraform output
Explanation: Displays the output variables defined in your Terraform configuration, such as IP addresses or URLs of created resources.
9. terraform import
Description: Imports existing infrastructure into your Terraform state.
Usage: Use this command to bring manually created resources under Terraform management.
Example:
terraform import aws_instance.example i-12345678
Explanation: This command adds an existing resource to the Terraform state, but you must manually update your configuration files to reflect the imported resource.
10. terraform refresh
Description: Updates the state file with the real-time state of resources without applying changes to the infrastructure.
Usage: Use this command to sync the state file with actual infrastructure.
Example:
terraform refresh
Explanation: Refreshes the state file with the current state of all resources without modifying them.
Advanced Terraform Commands
11. terraform taint
Description: Marks a resource as tainted, forcing Terraform to recreate it during the next apply.
Usage: Use when you want to replace a resource that might be faulty or requires updating.
Example:
terraform taint aws_instance.example
Explanation: The resource marked as tainted will be destroyed and recreated the next time terraform apply is run.
12. terraform untaint
Description: Removes the taint from a resource, preventing it from being replaced during the next apply.
Usage: Use if you mistakenly marked a resource as tainted.
Example:
terraform untaint aws_instance.example
Explanation: This command reverts a previously tainted resource, preventing its destruction.
13. terraform graph
Description: Generates a visual representation of the dependency graph of Terraform resources.
Usage: Useful for understanding the relationships and dependencies between resources.
Example:
terraform graph | dot -Tpng > graph.png
Explanation: This command outputs a graph description that can be converted into an image showing how resources depend on each other.
14. terraform workspace
Description: Manages workspaces, allowing you to create multiple instances of your infrastructure.
Usage: Use to manage separate environments like dev, test, and prod.
Example:
terraform workspace new dev
terraform workspace select dev
Explanation: Workspaces allow you to use the same configuration to manage multiple environments or versions of your infrastructure.
Summary
Terraform commands allow you to manage your infrastructure declaratively. You start with initializing your environment (init), planning changes (plan), applying those changes (apply), and cleaning up (destroy). Advanced commands such as import, state, and workspace provide greater control over managing complex infrastructure.