๐Ÿ“˜ Terraform CLI: The Command-Line Tool for Managing Infrastructure

When working with Terraform, everything starts with the Terraform CLI (Command-Line Interface).

The CLI is the primary way to interact with Terraform. Whether you want to initialize a project, validate configurations, plan changes, apply infrastructure, or destroy resources, the CLI provides all the commands you need.

Think of Terraform CLI as the steering wheel of the Terraform engine. Without it, your .tf configuration files are just text. With it, those files come alive and turn into real infrastructure across AWS, Azure, GCP, and many other providers.

This article covers:

  • โœ… What Terraform CLI is.
  • โœ… The most important CLI commands.
  • โœ… 3 real-world examples of CLI usage.
  • โœ… How to remember CLI commands for exams/interviews.
  • โœ… Why Terraform CLI is essential to learn.
  • โœ… Best practices for using Terraform CLI effectively.

๐Ÿ“‚ What is Terraform CLI?

The Terraform CLI (Command-Line Interface) is a binary tool that allows you to execute Terraform commands from your terminal or shell.

It is installed locally on your computer or in a CI/CD pipeline, and it interacts with your Terraform configuration files (.tf) to:

  • Initialize Terraform projects.
  • Validate code syntax.
  • Preview execution plans.
  • Apply and provision infrastructure.
  • Destroy or modify infrastructure.
  • Manage providers, state files, and workspaces.

๐Ÿ‘‰ Without the CLI, you cannot apply your .tf files to build infrastructure.


โš™๏ธ Core Terraform CLI Commands

Here are the most important CLI commands every Terraform user must know:

  1. terraform init

    • Initializes a new or existing Terraform project.
    • Downloads provider plugins and sets up backend for state.
    Terminal window
    terraform init
  2. terraform validate

    • Checks if .tf configuration syntax is valid.
    Terminal window
    terraform validate
  3. terraform plan

    • Shows what actions Terraform will take without applying them.
    Terminal window
    terraform plan
  4. terraform apply

    • Executes the plan and provisions infrastructure.
    Terminal window
    terraform apply
  5. terraform destroy

    • Removes all resources defined in .tf files.
    Terminal window
    terraform destroy
  6. terraform fmt

    • Formats .tf files to follow standard style.
    Terminal window
    terraform fmt
  7. terraform show

    • Displays the state or plan file in a readable format.
    Terminal window
    terraform show
  8. terraform workspace

    • Manage multiple environments (dev, test, prod).
    Terminal window
    terraform workspace new dev

๐Ÿ›  3 Unique Real-World Examples Using Terraform CLI


โœ… Example 1: Deploying an AWS EC2 Instance

Step 1: Initialize Terraform project

Terminal window
terraform init

Step 2: Write main.tf

provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "Terraform-EC2"
}
}

Step 3: Validate configuration

Terminal window
terraform validate

Step 4: Preview changes

Terminal window
terraform plan

Step 5: Apply configuration

Terminal window
terraform apply -auto-approve

๐Ÿ‘‰ Result: An EC2 instance is deployed on AWS.


โœ… Example 2: Managing Workspaces (Dev, Test, Prod)

Step 1: Create a new workspace

Terminal window
terraform workspace new dev
terraform workspace new prod

Step 2: Switch workspaces

Terminal window
terraform workspace select dev

Step 3: Deploy different configurations

In variables.tf:

variable "instance_type" {
type = string
default = "t2.micro"
}

Use CLI to pass different variables:

Terminal window
terraform apply -var="instance_type=t3.micro"

๐Ÿ‘‰ Result: Dev uses t2.micro, Prod uses t3.micro.


โœ… Example 3: Destroying Resources Safely

Imagine you want to tear down your staging environment.

Step 1: Run destroy plan

Terminal window
terraform plan -destroy

Step 2: Confirm destruction

Terminal window
terraform destroy -auto-approve

๐Ÿ‘‰ Result: Terraform safely removes all resources created in .tf files.


๐ŸŽฏ Why is Terraform CLI Important?

  1. Primary Interaction Point

    • Without CLI, Terraform cannot execute .tf files.
  2. Cross-Platform Consistency

    • Same CLI commands work across AWS, Azure, GCP, Kubernetes, etc.
  3. Automation in CI/CD

    • CLI commands are used in Jenkins, GitHub Actions, GitLab CI/CD pipelines.
  4. Preview Before Apply

    • terraform plan avoids mistakes by showing changes before applying.
  5. Environment Management

    • CLI workspaces make handling dev/test/prod seamless.
  6. Disaster Recovery

    • terraform destroy ensures complete resource cleanup.

๐Ÿง  How to Remember Terraform CLI Commands (Exam & Interview Prep)

Hereโ€™s a mnemonic: I.V.P.A.D.F.S.W

  • I โ†’ init (initialize project)
  • V โ†’ validate (check syntax)
  • P โ†’ plan (preview changes)
  • A โ†’ apply (create infra)
  • D โ†’ destroy (delete infra)
  • F โ†’ fmt (format files)
  • S โ†’ show (view state)
  • W โ†’ workspace (manage environments)

๐Ÿ‘‰ Interview Answer Tip: โ€œTerraform CLI is the primary tool to manage infrastructure. It provides commands such as init for initialization, plan for previewing, apply for provisioning, and destroy for cleanup. These commands make Terraform flexible, auditable, and automation-ready.โ€


๐Ÿ“š Best Practices for Using Terraform CLI

  • Always run terraform plan before apply.
  • Use terraform fmt for clean and readable .tf code.
  • Store .tfstate in a remote backend (S3, Azure Blob, GCS).
  • Automate CLI commands in CI/CD pipelines.
  • Use -auto-approve cautiously (good for automation, risky for manual use).
  • Manage environments with workspaces instead of multiple directories.

๐Ÿ”ฎ Future of Terraform CLI

  • Integration with AI โ†’ Auto-suggest CLI commands based on .tf code.
  • Enhanced Security โ†’ Built-in checks for compliance and policies.
  • Cloud-Native Enhancements โ†’ CLI commands aligned with Kubernetes and serverless infra.
  • Remote CLI Execution โ†’ Direct CLI use inside Terraform Cloud/Enterprise.

๐Ÿ“ Summary

  • Terraform CLI is the command-line interface used to interact with .tf files.
  • Key commands include init, plan, apply, destroy, fmt, show, and workspace.
  • Real-world examples include deploying EC2, managing environments, and destroying infra.
  • Use mnemonic I.V.P.A.D.F.S.W to remember commands for interviews/exams.
  • Importance: Without CLI, Terraform cannot execute infrastructure automation.
  • Best practices include running plan before apply, formatting code, and securing state files.

โœ… Mastering Terraform CLI makes you not only comfortable with Terraform but also interview-ready and job-ready. It is the foundation of Infrastructure as Code in Terraform.