Home Infrastructure as Code (IaC) Understanding Terraform Core Commands — Init, Validate, Plan, Apply, Show, and Destroy

Understanding Terraform Core Commands — Init, Validate, Plan, Apply, Show, and Destroy

290
0

Once we understand Terraform’s core concepts, the next step is learning its core commands — the ones we’ll use daily to build, manage, and clean up cloud infrastructure.

These commands are simple yet powerful. Let’s go step-by-step through each one with clear explanations. We may use AWS cloud examples here but the concepts are the same across any cloud. We will also have hands-on notes for each cloud later.


🧱 1. terraform init — Initialize Our Project

This command prepares your working directory for use with Terraform.
It downloads the required provider plugins (like AWS), sets up the backend if you’re using remote state, and gets your project ready to run.

Example

terraform init

If your configuration contains:

provider "aws" {
  region = "us-east-1"
}

Terraform will download the AWS plugin and show:

Terraform has been successfully initialized!

Tip: Always run this when starting a new project or after cloning a repository.


🧩 2. terraform validate — Check for Errors

This command verifies that your Terraform configuration files are syntactically valid and internally consistent.
It doesn’t connect to AWS — it just ensures your .tf files are correct.

Example

terraform validate

If everything is good:

Success! The configuration is valid.

If there’s an issue, Terraform will highlight the line and reason for the error.

Tip: Run this before applying changes to catch typos and syntax mistakes early.


🧭 3. terraform plan — Preview Changes

The plan command creates an execution plan showing what Terraform will do — what resources will be created, changed, or destroyed.

It’s like a dry run that helps you confirm changes before applying them.

Example

terraform plan

Output:

+ aws_instance.web_server will be created
+ aws_s3_bucket.logs will be created

The + symbol means Terraform will add these resources.

Tip: Always review the plan carefully before applying to avoid unexpected changes.


🚀 4. terraform apply — Deploy Infrastructure

This command executes the plan and actually creates or updates your AWS infrastructure.

Example

terraform apply

Terraform will show you the plan again and ask for confirmation:

Do you want to perform these actions?
  Only 'yes' will be accepted to approve.

Enter a value: yes

Once confirmed, Terraform connects to AWS and provisions your resources.

Tip: For automation, you can use:

terraform apply -auto-approve

to skip manual confirmation.


🔍 5. terraform show — View Current State

After applying, you can use terraform show to display details about the infrastructure Terraform manages — pulled from your state file (terraform.tfstate).

Example

terraform show

Output:

# aws_instance.web_server:
resource "aws_instance" "web_server" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

Tip: This command helps verify what’s actually deployed and tracked by Terraform.


🧹 6. terraform destroy — Clean Up

When you’re done, terraform destroy removes all resources created by Terraform.
It’s the safe and efficient way to tear down your infrastructure.

Example

terraform destroy

Terraform will confirm before deletion:

Do you really want to destroy all resources?
  Only 'yes' will be accepted to confirm.

Once confirmed, Terraform deletes the AWS resources and updates the state file.

Tip: Never manually delete Terraform-managed resources from the AWS Console — it can break your state file.


🧰 7. Other Helpful Commands

CommandDescription
terraform fmtFormats .tf files to make them neat and consistent.
terraform outputDisplays outputs (like IP addresses or resource names).
terraform versionShows the installed Terraform version.
terraform workspaceLets you manage multiple environments (dev, test, prod).

🪜 Typical Terraform Workflow

Here’s a quick reference workflow you can follow in any project:

terraform init      # Initialize project and providers
terraform validate  # Check syntax and structure
terraform plan      # Preview what will happen
terraform apply     # Deploy resources to AWS
terraform show      # View current state
terraform destroy   # Clean up resources

Tip: Store your Terraform files in version control (like GitHub) — it’s the best way to track and collaborate on infrastructure changes.


🧠 Summary

Terraform’s core commands form a simple, repeatable process for managing cloud resources.
Once you get used to this workflow, you’ll be able to build and destroy environments confidently — all with code.

Quick Recap

StepCommandPurpose
1terraform initInitialize your project
2terraform validateCheck for syntax issues
3terraform planPreview changes
4terraform applyCreate or update resources
5terraform showDisplay the current state
6terraform destroyRemove all resources

💬 “Master these few commands, and you can manage entire cloud environments from your terminal.”

Previous articleUnderstanding Terraform Core Concepts
Next articleWhy Linux Is an Essential Skill for Every DevOps Engineer
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here