Terraform
Basics & Fundamentals
- Infrastructure as Code (IaC)
- Declarative Syntax in IaC
- Terraform Configuration Files
- Terraform CLI
- Terraform Init
- Terraform Plan
- Terraform Apply
- Terraform Destroy
Providers & Resources
๐ 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:
-
terraform init
- Initializes a new or existing Terraform project.
- Downloads provider plugins and sets up backend for state.
Terminal window terraform init -
terraform validate
- Checks if
.tf
configuration syntax is valid.
Terminal window terraform validate - Checks if
-
terraform plan
- Shows what actions Terraform will take without applying them.
Terminal window terraform plan -
terraform apply
- Executes the plan and provisions infrastructure.
Terminal window terraform apply -
terraform destroy
- Removes all resources defined in
.tf
files.
Terminal window terraform destroy - Removes all resources defined in
-
terraform fmt
- Formats
.tf
files to follow standard style.
Terminal window terraform fmt - Formats
-
terraform show
- Displays the state or plan file in a readable format.
Terminal window terraform show -
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
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
terraform validate
Step 4: Preview changes
terraform plan
Step 5: Apply configuration
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
terraform workspace new devterraform workspace new prod
Step 2: Switch workspaces
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:
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
terraform plan -destroy
Step 2: Confirm destruction
terraform destroy -auto-approve
๐ Result: Terraform safely removes all resources created in .tf
files.
๐ฏ Why is Terraform CLI Important?
-
Primary Interaction Point
- Without CLI, Terraform cannot execute
.tf
files.
- Without CLI, Terraform cannot execute
-
Cross-Platform Consistency
- Same CLI commands work across AWS, Azure, GCP, Kubernetes, etc.
-
Automation in CI/CD
- CLI commands are used in Jenkins, GitHub Actions, GitLab CI/CD pipelines.
-
Preview Before Apply
terraform plan
avoids mistakes by showing changes before applying.
-
Environment Management
- CLI workspaces make handling dev/test/prod seamless.
-
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
beforeapply
. - 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
, andworkspace
. - 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
beforeapply
, 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.