Terraform Commands Cheat Sheet - Infrastructure as Code

Comprehensive Terraform commands cheat sheet for infrastructure as code. Learn to format, validate, initialize, deploy, destroy, manage state, and work with Terraform Cloud.

Terraform Commands Cheat Sheet

This cheat sheet provides a quick reference for essential Terraform commands used in managing infrastructure as code. Terraform is a powerful tool for building, changing, and versioning infrastructure safely and efficiently.

Terraform Command Reference

Format and Validate Configuration

Command Description
terraform fmt Reformat your Terraform configuration files to a standard style. This ensures consistency across your codebase.
terraform validate Check whether the Terraform configuration files are syntactically valid and internally consistent.

Initialize Working Directory

Command Description
terraform init Initialize a working directory containing Terraform configuration files. This downloads provider plugins and modules.

Plan, Deploy, and Cleanup Infrastructure

Command Description
terraform apply --auto-approve Create or update infrastructure defined in your configuration without requiring manual confirmation. Use with caution.
terraform destroy --auto-approve Destroy all infrastructure managed by the current Terraform configuration without a confirmation prompt. Use with extreme caution.
terraform plan -out plan.out Generate an execution plan describing what actions Terraform will take to achieve the desired state. Saves the plan to a file.
terraform apply plan.out Apply the saved execution plan to create or update infrastructure. This ensures that the actions taken match the planned actions.
terraform plan -destroy Generate an execution plan for destroying resources. This shows what will be removed before executing the destroy command.
terraform apply -target=aws_instance.myinstance Apply changes only to a specific resource or module. Useful for targeted updates or debugging.
terraform apply -var myregion=us-east-1 Pass variable values directly on the command line during the apply phase.
terraform apply -lock=true Explicitly enable state locking during an apply operation to prevent concurrent modifications.
terraform apply refresh=false Apply changes without first refreshing the state of existing resources. This can speed up applies but may lead to drift if not used carefully.
terraform apply --parallelism=5 Control the number of concurrent operations Terraform can perform during an apply. Adjust based on your system's capabilities and network.
terraform refresh Update Terraform state to match the real-world infrastructure. This command detects any manual changes made outside of Terraform.
terraform providers Show information about the providers used in the current Terraform configuration, including their versions.

Manage Workspaces

Command Description
terraform workspace new <workspace_name> Create a new, empty workspace. Workspaces allow you to manage multiple distinct environments (e.g., dev, staging, prod) with the same configuration.
terraform workspace select <workspace_name> Switch the current working directory to a different workspace. The default workspace is named 'default'.
terraform workspace list List all available workspaces in the current project.

State Manipulation and Management

Command Description
terraform state show <resource_address> Display detailed information about a specific resource as it is recorded in the Terraform state file.
terraform state pull > terraform.tfstate Output the current Terraform state to a local file. Useful for backups or manual inspection.
terraform state mv <source_address> <destination_address> Move a resource within the Terraform state file, for example, when refactoring modules.
terraform state replace-provider <source_provider_addr> <destination_provider_addr> Replace an existing provider in the state file with a new one. Useful when migrating providers or changing registry locations.
terraform state list List all resource addresses currently tracked in the Terraform state file.
terraform state rm <resource_address> Remove a resource from the Terraform state file without destroying the actual infrastructure. Use with caution.

Import and Outputs

Command Description
terraform import <resource_type>.<resource_name> <resource_id> Import existing infrastructure into Terraform management. This associates pre-existing resources with your Terraform configuration.
terraform output Display all defined output values from your Terraform configuration. Outputs expose information about your infrastructure.
terraform output <output_name> Display the value of a specific output variable.
terraform output -json Display all output values in JSON format, which is useful for scripting and automation.

Terraform Cloud Integration

Command Description
terraform login Log in to Terraform Cloud or Terraform Enterprise using an API token. This enables remote state management and collaboration features.
terraform logout Log out from Terraform Cloud or Terraform Enterprise.

For more detailed information, refer to the official Terraform CLI documentation.