Infastructure As Code Essential Commands
Overview
When working with Infrastructure as Code (IaC) tools, particularly Terraform, there are several essential commands you should be familiar with. These commands allow you to manage your infrastructure effectively, from initializing your environment to applying changes and troubleshooting. Here’s a breakdown of the key Terraform commands you should know:
1. Initialization and Setup Commands
terraform init
:Purpose: Initializes the working directory containing the Terraform configuration files. This command sets up the backend, installs any required provider plugins, and prepares the environment for running Terraform commands.
When to Use: Run this command first in any new Terraform project or when you add/change providers in your configuration.
terraform workspace
:Purpose: Manages workspaces, which are separate instances of state files. Workspaces allow you to manage multiple environments (e.g., dev, staging, prod) within the same Terraform configuration.
When to Use: Use this command when you need to create, select, or delete workspaces for managing different environments.
2. Planning and Applying Changes
terraform plan
:Purpose: Generates and displays an execution plan, showing what actions Terraform will take to achieve the desired state defined in the configuration files. It’s a dry-run that helps you review the proposed changes before applying them.
When to Use: Run this command to review the changes before executing them, especially in a production environment.
terraform apply
:Purpose: Applies the changes required to reach the desired state of the configuration. This command executes the plan, creating, modifying, or destroying resources as necessary.
When to Use: After reviewing the plan, run this command to implement the changes. You can also combine
plan
andapply
by usingterraform apply
with a plan file generated byterraform plan
.
3. State Management
terraform state
:Purpose: Provides commands for advanced state management, such as inspecting the current state, listing resources, moving resources between modules, and removing resources from the state file.
When to Use: Use this command when you need to modify or troubleshoot the Terraform state file directly. Examples include
terraform state list
,terraform state show
, andterraform state mv
.
terraform refresh
:Purpose: Updates the state file with the latest information from the real infrastructure. This command queries the actual state of the resources and updates the Terraform state file accordingly.
When to Use: Use this command to ensure that the state file is in sync with the actual state of the infrastructure, especially before running
terraform plan
orterraform apply
.
terraform import
:Purpose: Imports existing infrastructure resources into your Terraform state. This is useful for bringing resources created outside of Terraform under its management.
When to Use: Use this command to import resources that were created manually or by other means into Terraform's state file.
4. Destroying Infrastructure
terraform destroy
:Purpose: Destroys the resources managed by your Terraform configuration. This command removes all resources defined in your configuration from the infrastructure.
When to Use: Use this command when you need to tear down an environment or decommission resources that are no longer needed.
5. Debugging and Troubleshooting
terraform validate
:Purpose: Validates the Terraform configuration files for syntax errors and consistency issues, ensuring that they are correctly formatted and can be executed.
When to Use: Use this command to check your configuration files before applying changes, especially after making significant edits.
terraform fmt
:Purpose: Automatically formats Terraform configuration files to follow the canonical style. This command ensures that your HCL code is clean and consistent.
When to Use: Use this command regularly to keep your Terraform files well-formatted, especially before committing code to version control.
terraform taint
:Purpose: Marks a specific resource for destruction and recreation on the next
terraform apply
. This is useful when a resource is in a problematic state and needs to be recreated.When to Use: Use this command to force a resource to be replaced, often for troubleshooting or when a resource is corrupted or misconfigured.
terraform untaint
:Purpose: Removes the "taint" from a resource, meaning it will not be destroyed and recreated during the next
terraform apply
.When to Use: Use this command to reverse the effect of a previous
terraform taint
if you decide not to recreate the resource.
6. Managing Outputs and Variables
terraform output
:Purpose: Displays the values of output variables defined in the configuration. Outputs are often used to expose useful information about your infrastructure, such as IP addresses or resource IDs.
When to Use: Use this command to retrieve and display the output values after running
terraform apply
.
terraform variables
:Purpose: Manages input variables for Terraform configurations, including setting, getting, and validating variables.
When to Use: Use this command (along with
-var
or-var-file
flags) to pass variables into Terraform configurations, especially when customizing deployments across different environments.
7. Advanced Commands
terraform graph
:Purpose: Generates a visual representation of the resource dependencies in your configuration. This command outputs a graph in DOT format, which can be converted into a graphical diagram using tools like Graphviz.
When to Use: Use this command to visualize and understand the dependency structure of your Terraform configuration, which is helpful in complex environments.
terraform providers
:Purpose: Lists the providers required by the configuration, as well as the provider versions and source addresses.
When to Use: Use this command to inspect the providers being used in your configuration, especially when troubleshooting provider-related issues.
8. Terraform Cloud and Enterprise Commands
terraform login
:Purpose: Authenticates to Terraform Cloud or Terraform Enterprise, allowing you to interact with remote workspaces and manage infrastructure from a centralized platform.
When to Use: Use this command when working in an environment that leverages Terraform Cloud or Enterprise for state management and collaboration.
terraform remote
:Purpose: Manages remote state backends in Terraform. This command configures and interacts with remote state storage, allowing you to securely store and share state files across teams.
When to Use: Use this command to set up and manage remote state backends, especially in collaborative or production environments.
Last updated