Infrastructure as Code (IaC) has revolutionized the way cloud environments are managed, and Terraform stands out as one of the most popular IaC tools today. Whether you're provisioning compute resources, managing storage, or automating networking setups, Terraform provides a consistent and powerful CLI experience.
In this article, I'll walk you through essential Terraform commands every Cloud Engineer should know—especially if you're just getting started or want a solid foundation for managing infrastructure efficiently.
1. terraform init
terraform init
This is the first command you run when starting a new Terraform configuration. It initializes the working directory, downloads the required provider plugins, and prepares your project for further actions.
Use it when:
- You clone a new Terraform repo
- You add/change providers
- You're setting up a new module
2. terraform validate
terraform validate
This command checks your configuration files for syntax errors and ensures they're internally consistent.
Think of it as your syntax checker.
3. terraform plan
terraform plan
This is your preview command. It shows what actions Terraform will take to reach the desired state, without actually making any changes.
Why it's important:
- Avoid unintended resource changes
- Review planned changes with your team
4. terraform apply
terraform apply
This command executes the changes outlined in the plan. You’ll be prompted to approve before any infrastructure is provisioned or updated.
You can auto-approve using:
terraform apply -auto-approve
Pro tip: Avoid using -auto-approve
in production unless you really know what you're doing.
5. terraform destroy
terraform destroy
Need to tear down everything? This command removes all infrastructure created by Terraform. Use with caution!
6. terraform output
terraform output
Displays the values of output variables from your configuration. This is useful for retrieving endpoints, resource IDs, and other important data post-deployment.
7. terraform fmt
terraform fmt
Automatically formats your Terraform code according to standard conventions. This keeps your code clean, readable, and consistent across teams.
8. terraform show
terraform show
Want to inspect your Terraform state? This command gives you a human-readable view of what's currently managed in your state file.
9. terraform state
terraform state list
Manage the current state of your resources. You can list, pull, move, or remove resources directly from the state file—but this command is powerful and risky, so use with understanding.
10. terraform taint
terraform taint
Marks a specific resource for destruction and recreation during the next apply
. Useful when a resource is in a bad state and you want to force a rebuild.
Bonus: terraform import
terraform import
This command lets you bring existing infrastructure under Terraform management. It doesn’t generate the code for you, but it links the resource to your state file.
Final Thoughts
Mastering these Terraform commands is a game-changer for any cloud engineer. They form the core toolkit for managing, updating, and troubleshooting infrastructure as code. Whether you're deploying your first EC2 instance or managing multi-cloud environments, these commands will make your work efficient and reliable.
Got more Terraform tips or favorite commands?
Drop them in the comments—I’d love to hear from fellow DevOps and Cloud Engineering enthusiasts!