Table of Contents
Introduction
Terraform by HashiCorp is a powerful tool used to create, manage, and manipulate infrastructure resources with code. The Terraform Command Line Interface (CLI) is the heart through which developers interact with Terraform to manage infrastructure. This cheat sheet is a comprehensive guide to the most useful Terraform CLI commands, from basic to advanced, serving as a quick reference for beginners and experienced users alike.
Basic Commands
Before diving into the commands, ensure you have Terraform installed. You can check your installation by running:
terraform --version
This command will output something like:
Terraform v0.15.3
Let’s start with the most basic commands.
1. Initializing a Terraform Directory
To begin any project, you need to initialize the directory containing your Terraform configuration files with:
terraform init
This prepares your directory for other Terraform commands, installs necessary plugins, and sets up the backend.
2. Formatting Terraform Code
The terraform fmt
command is used to rewrite Terraform configuration files to a canonical format and style. This not only makes your files cleaner but also helps in maintaining consistency across your codebase.
terraform fmt
3. Validating Terraform Configuration
It’s crucial to validate your Terraform configuration for any errors or misconfigurations. Running terraform validate
helps catch issues early in the development cycle:
terraform validate
This outputs either a success message if no issues are found or details of the errors if any.
4. Planning Infrastructure
Before applying your configuration to actual infrastructure, you should always run:
terraform plan
This command shows a preview of what Terraform intends to do based on your configuration. It’s an essential step for catching any unintended changes.
5. Applying Configuration Changes
To apply your Terraform configuration and create or update your infrastructure, use:
terraform apply
Terraform will show a plan and ask for confirmation before making any changes.
6. Viewing the State
The terraform state
command series is useful for inspecting and modifying the Terraform state file, which contains the current state of the infrastructure managed by Terraform.
To list resources in the state:
terraform state list
7. Destroying Infrastructure
To remove all the resources defined in your Terraform configuration, you use:
terraform destroy
This is useful for cleaning up resources that are no longer needed.
Advanced Terraform Commands
Moving beyond the basics, there are several advanced Terraform CLI commands that offer greater control and insight into your infrastructure management.
1. Workspaces
Workspaces allow you to manage different states of your infrastructure matching different environments, like staging or production. To create a new workspace:
terraform workspace new my-workspace
2. Importing Existing Resources
To bring real-world infrastructure into Terraform management without recreating it, you can use the terraform import
command. You’ll need to specify the resource type and an identifier:
terraform import aws_instance.my_instance i-1234567890abcdef0
This is complex but crucial for integrating Terraform into existing environments.
3. Debugging
When facing issues or unexpected behavior, Terraform’s debug feature can be invaluable. To enable verbose log output:
TF_LOG=DEBUG terraform apply
This can provide detailed insights needed to diagnose and resolve issues.
Conclusion
Terraform’s CLI commands are an essential toolkit for infrastructure as code (IaC). Understanding and mastering these commands can significantly improve the efficiency and reliability of your infrastructure management process. Start with the basics, then progressively adopt more advanced commands as your needs evolve. With practice, you’ll find Terraform to be an indispensable part of your DevOps toolkit.