Mastering Terraform: A Comprehensive Guide to 30 Essential Commands for Daily Infrastructure Automation
Mastering Terraform: A Comprehensive Guide to 30 Essential Commands for Daily Infrastructure Automation
Terraform Workflow Commands
terraform init
: Initialize a Terraform directory.terraform plan
: Show what Terraform will do before making changes.terraform apply
: Apply the changes specified by the plan.terraform destroy
: Remove all resources defined in the Terraform configuration.
State Management Commands
terraform state list
: List resources in the state file.terraform state show [resource]
: Show the attributes of a single resource in the state.terraform state rm [resource]
: Remove items from the state.terraform state mv [source] [destination]
: Move items within the state.terraform state pull
: Manually download and output the state from remote storage.terraform state push [path]
: Manually upload a state file to remote storage.
Configuration Refinement Commands
terraform fmt
: Rewrite Terraform configuration files to a canonical format and style.terraform validate
: Validates the configuration for syntax and internal consistency.terraform refresh
: Update local state to match real-world infrastructure.
Advanced Execution Commands
terraform apply -auto-approve
: Apply changes without being prompted to enter "yes".terraform plan -out=path
: Save the execution plan to a file.terraform apply "planfile"
: Apply the execution plan from a file.
Debugging and Troubleshooting
terraform console
: Interactive console for evaluating expressions.TF_LOG=DEBUG terraform apply
: Enable detailed logs for debugging.
Modularization and Reuse
terraform get
: Download and install modules for the configuration.terraform module list
: List the modules used in the current configuration.
Workspace Management
terraform workspace list
: List all existing workspaces.terraform workspace new [name]
: Create a new workspace.terraform workspace select [name]
: Switch to another workspace.terraform workspace delete [name]
: Delete a workspace (cannot delete the current workspace).
Foundational Commands
terraform init
: Initialize a Terraform working directory.terraform plan
: Show an execution plan.terraform apply
: Apply changes to reach the desired state.terraform destroy
: Destroy Terraform-managed infrastructure.terraform fmt
: Reformat your configuration in a canonical style.terraform validate
: Validate the configuration files for errors.terraform output
: Show output values from your state file.terraform refresh
: Update local state file against real resources.terraform import
: Bring real-world infrastructure into Terraform management.terraform workspace
: Manage workspaces for managing different states of your
Miscellaneous Commands
terraform version
: Display the current Terraform version.terraform taint [resource]
: Mark a resource for recreation during the next apply.terraform untaint [resource]
: Remove the 'taint' from a resource.terraform import [resource] [id]
: Import existing infrastructure into Terraform management.terraform graph
: Generate a visual representation of either a configuration or execution plan.
Scripting and Automation
terraform output [name]
: Query specific output values from your state file.- Executing Terraform in automation: Using Terraform in CI/CD pipelines typically involves scripting around commands like
terraform init
, terraform plan
, and terraform apply
, often with flags for automation.
No comments:
Post a Comment