Pages

Tuesday, April 9, 2024

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

  1. terraform init: Initialize a Terraform directory.
  2. terraform plan: Show what Terraform will do before making changes.
  3. terraform apply: Apply the changes specified by the plan.
  4. terraform destroy: Remove all resources defined in the Terraform configuration.

State Management Commands

  1. terraform state list: List resources in the state file.
  2. terraform state show [resource]: Show the attributes of a single resource in the state.
  3. terraform state rm [resource]: Remove items from the state.
  4. terraform state mv [source] [destination]: Move items within the state.
  5. terraform state pull: Manually download and output the state from remote storage.
  6. terraform state push [path]: Manually upload a state file to remote storage.

Configuration Refinement Commands

  1. terraform fmt: Rewrite Terraform configuration files to a canonical format and style.
  2. terraform validate: Validates the configuration for syntax and internal consistency.
  3. terraform refresh: Update local state to match real-world infrastructure.

Advanced Execution Commands

  1. terraform apply -auto-approve: Apply changes without being prompted to enter "yes".
  2. terraform plan -out=path: Save the execution plan to a file.
  3. terraform apply "planfile": Apply the execution plan from a file.

Debugging and Troubleshooting

  1. terraform console: Interactive console for evaluating expressions.
  2. TF_LOG=DEBUG terraform apply: Enable detailed logs for debugging.

Modularization and Reuse

  1. terraform get: Download and install modules for the configuration.
  2. terraform module list: List the modules used in the current configuration.

Workspace Management

  1. terraform workspace list: List all existing workspaces.
  2. terraform workspace new [name]: Create a new workspace.
  3. terraform workspace select [name]: Switch to another workspace.
  4. terraform workspace delete [name]: Delete a workspace (cannot delete the current workspace).

Foundational Commands

  1. terraform init: Initialize a Terraform working directory.
  2. terraform plan: Show an execution plan.
  3. terraform apply: Apply changes to reach the desired state.
  4. terraform destroy: Destroy Terraform-managed infrastructure.
  5. terraform fmt: Reformat your configuration in a canonical style.
  6. terraform validate: Validate the configuration files for errors.
  7. terraform output: Show output values from your state file.
  8. terraform refresh: Update local state file against real resources.
  9. terraform import: Bring real-world infrastructure into Terraform management.
  10. terraform workspace: Manage workspaces for managing different states of your

Miscellaneous Commands

  1. terraform version: Display the current Terraform version.
  2. terraform taint [resource]: Mark a resource for recreation during the next apply.
  3. terraform untaint [resource]: Remove the 'taint' from a resource.
  4. terraform import [resource] [id]: Import existing infrastructure into Terraform management.
  5. terraform graph: Generate a visual representation of either a configuration or execution plan.

Scripting and Automation

  1. terraform output [name]: Query specific output values from your state file.
  2. 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