Getting Started with Terraform: Complete Beginner’s Guide to Infrastructure as Code
Terraform is an open-source Infrastructure as Code (IaC) tool created by HashiCorp that lets you define, provision, and manage cloud infrastructure using a declarative configuration language. Instead of manually clicking through cloud consoles, you write code that describes your desired infrastructure state, and Terraform makes it happen. This guide covers everything you need to get started with Terraform in 2025.
📑 Table of Contents
- What is Infrastructure as Code (IaC)?
- Why Choose Terraform?
- Installing Terraform
- On Ubuntu/Debian
- On RHEL/CentOS/Fedora
- On macOS
- Terraform Basics: Key Concepts
- Core Workflow
- Key Terms
- Your First Terraform Configuration
- Step 1: Create Project Directory
- Step 2: Create Configuration File (main.tf)
- Step 3: Initialize Terraform
- Step 4: Preview Changes
- Step 5: Apply Configuration
- Step 6: Clean Up
- Understanding HCL Syntax
- Resource Block
- Variables
- Outputs
- Essential Terraform Commands
- Project Structure Best Practices
- Working with Workspaces
- Next Steps
- Conclusion
What is Infrastructure as Code (IaC)?
Infrastructure as Code means managing and provisioning infrastructure through machine-readable configuration files rather than manual processes. Benefits include:
- Version Control – Track infrastructure changes in Git
- Reproducibility – Create identical environments consistently
- Automation – Reduce manual errors and speed up deployments
- Documentation – Code serves as documentation of your infrastructure
- Collaboration – Teams can review and approve infrastructure changes
Why Choose Terraform?
- Cloud Agnostic – Works with AWS, Azure, GCP, and 3000+ providers
- Declarative Syntax – Describe what you want, not how to build it
- State Management – Tracks real infrastructure state
- Plan Before Apply – Preview changes before making them
- Modular – Reuse code with modules
- Large Community – Extensive documentation and community support
Installing Terraform
ubuntu-debian">On Ubuntu/Debian
# Add HashiCorp GPG key
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
# Add repository
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
# Install Terraform
sudo apt update && sudo apt install terraform -y
# Verify installation
terraform version
centos-fedora">On RHEL/CentOS/Fedora
# Add HashiCorp repository
sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
# Install Terraform
sudo dnf install terraform -y
# Verify installation
terraform version
On macOS
# Using Homebrew
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
# Verify
terraform version
Terraform Basics: Key Concepts
Core Workflow
- Write – Define infrastructure in .tf files
- Init – Initialize working directory and download providers
- Plan – Preview changes Terraform will make
- Apply – Create/modify infrastructure
- Destroy – Remove infrastructure when no longer needed
Key Terms
- Provider – Plugin that interacts with APIs (AWS, Azure, etc.)
- Resource – Infrastructure component (EC2 instance, S3 bucket)
- State – Terraform record of managed infrastructure
- Module – Reusable collection of resources
- Variable – Input parameters for configurations
- Output – Values exported from your configuration
Your First Terraform Configuration
Let us create a simple configuration that provisions a local file (no cloud account needed).
Step 1: Create Project Directory
mkdir terraform-demo
cd terraform-demo
Step 2: Create Configuration File (main.tf)
# Configure the Local provider
terraform {
required_providers {
local = {
source = "hashicorp/local"
version = "~> 2.4"
}
}
}
# Create a local file
resource "local_file" "hello" {
content = "Hello, Terraform!"
filename = "hello.txt"
}
# Output the file path
output "file_path" {
value = local_file.hello.filename
}
Step 3: Initialize Terraform
terraform init
Step 4: Preview Changes
terraform plan
Step 5: Apply Configuration
terraform apply
Type yes when prompted. Terraform creates the file.
Step 6: Clean Up
terraform destroy
Understanding HCL Syntax
Terraform uses HashiCorp Configuration Language (HCL):
Resource Block
resource "aws_instance" "web_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "WebServer"
}
}
Variables
# Define variable
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t2.micro"
}
# Use variable
resource "aws_instance" "example" {
instance_type = var.instance_type
}
Outputs
output "instance_ip" {
description = "Public IP of the instance"
value = aws_instance.example.public_ip
}
Essential Terraform Commands
# Initialize working directory
terraform init
# Validate configuration syntax
terraform validate
# Format code consistently
terraform fmt
# Preview changes
terraform plan
# Apply changes
terraform apply
# Apply without confirmation
terraform apply -auto-approve
# Destroy infrastructure
terraform destroy
# Show current state
terraform show
# List resources in state
terraform state list
Project Structure Best Practices
my-terraform-project/
├── main.tf # Main resource definitions
├── variables.tf # Variable declarations
├── outputs.tf # Output definitions
├── providers.tf # Provider configurations
├── terraform.tfvars # Variable values
└── modules/ # Reusable modules
Working with Workspaces
# Create workspace
terraform workspace new dev
terraform workspace new prod
# List workspaces
terraform workspace list
# Switch workspace
terraform workspace select dev
Next Steps
- Cloud Providers – Set up AWS, Azure, or GCP credentials
- Modules – Create reusable infrastructure components
- Remote State – Configure state storage for teams
- CI/CD Integration – Automate Terraform in pipelines
Conclusion
Terraform is essential for modern infrastructure management. Its declarative approach, multi-cloud support, and strong community make it the industry standard for Infrastructure as Code. Start with simple configurations and gradually build complexity as you gain confidence.
Was this article helpful?
About Ramesh Sundararamaiah
Red Hat Certified Architect
Expert in Linux system administration, DevOps automation, and cloud infrastructure. Specializing in Red Hat Enterprise Linux, CentOS, Ubuntu, Docker, Ansible, and enterprise IT solutions.