Essential Terraform Commands for Infrastructure Management

In this post, let's walk through the essential Terraform commands. These commands help us manage our infrastructure as code. From setting up our working environment to applying changes and even tearing down resources, we'll cover the key commands every Terraform user needs to know.
What is Terraform init?
The first thing we need to do after writing our Terraform code or cloning an existing project from source control is run the Terraform init command.
Terraform init sets up our working environment by installing the necessary provider plugins and modules. It gets everything ready so that we can run other Terraform commands like Terraform plan and Terraform apply, which are needed to actually make changes to our infrastructure.
What is Terraform plan?
The Terraform plan command is a key part of the Terraform workflow. It shows us a preview of what changes will be made to our infrastructure based on our code.
Before making any changes, Terraform plan lets us review what will be created, modified, or deleted. This helps ensure the changes align with our goals and prevents any unintended changes. We’ll see a summary of resources that will be affected, so we can understand the impact before applying the changes.
What is Terraform validate?
The Terraform validate command checks if our Terraform code is written correctly. It looks for any syntax errors, misconfigurations, or unsupported arguments.
Running this command helps us catch mistakes early, before we try to apply any changes. It ensures our code is structured properly and follows best practices, which also makes our code easier to read and maintain.
Some common reasons we might use Terraform validate include:
Checking our code for errors before relying on other resources.
Using the results in other applications.
Avoiding mistakes before deploying anything.
What is Terraform apply?
The Terraform apply command is where the changes actually happen. When we run it, Terraform compares our infrastructure with the configuration we've written and updates everything accordingly.
This includes creating, updating, or deleting resources as necessary to match the desired state in our code. It’s always a good idea to run Terraform plan before Terraform apply to make sure everything looks correct before applying the changes.
What is Terraform destroy?
The Terraform destroy command is used to remove the infrastructure that Terraform is managing. When we run this command, it deletes the resources we've defined in our Terraform configuration and updates the state accordingly.
Before running Terraform destroy, it's a good idea to preview what will be deleted by running Terraform plan -destroy. This gives us a final check to make sure we’re not accidentally removing anything important. Once we're confident, running Terraform destroy will permanently delete those resources.
Terraform ensures resources are destroyed in the right order by building a dependency graph.
By using these key Terraform commands—init, plan, validate, apply, and destroy—we can efficiently manage our infrastructure. Each command plays an important role, helping us apply changes in a safe and controlled way.
Subscribe to my newsletter
Read articles from Siddharth M directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
