🌍 1.What is Terraform?
Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It allows you to define, provision, and manage infrastructure (servers, networks, databases, etc.) across multiple cloud providers (AWS, Azure, GCP, etc.) using declarative configuration files.
❓ 2.Why do we need Terraform?
Automates infrastructure provisioning.
Maintains consistency across environments.
Supports multi-cloud deployments.
Version-controlled (works with Git).
Easy rollback using state files.
⚙️ 3.Where do we use Terraform?
Provisioning cloud infrastructure (AWS EC2, Azure VM, GCP Compute).
Setting up Kubernetes clusters (EKS, AKS, GKE).
Managing VPCs, subnets, load balancers, databases.
Infrastructure automation in DevOps CI/CD pipelines.
🏗️ 4.Terraform Architecture
1.Configuration files (.tf) → written by the user.
2.Terraform Core → processes the configuration, handles planning and apply.
3.Providers → plugins for cloud providers (AWS, Azure, GCP, etc.).
4.State File → tracks real infrastructure.
5.Execution Plan → preview of changes before applying.
💻 5.How to Install Terraform (Linux Example)
Download Terraform
wget https://releases.hashicorp.com/terraform/1.9.5/terraform_1.9.5_linux_amd64.zip
Unzip
unzip terraform_1.9.5_linux_amd64.zip
Move binary to path
sudo mv terraform /usr/local/bin/
Verify installation
terraform -v
✨ 6.Terraform Features
Infrastructure as Code (IaC)
Multi-cloud support
Immutable infrastructure
Execution plan before apply
State management
Modules for reusability
Open-source and extensible
🔄 7.Terraform vs Ansible (Key Differences)
Terraform: Provisioning tool (builds servers, networks, DBs).
Ansible: Configuration management tool (installs software, manages apps).
Terraform defines what infrastructure looks like.
Ansible defines how infrastructure is configured.
📜 8.Common Terraform Commands (with usage)
terraform init → Initialize working directory.
terraform validate → Validate configuration files.
terraform plan → Preview infrastructure changes.
terraform apply → Create/Update infrastructure.
terraform destroy → Delete infrastructure.
terraform show → Display current resources.
terraform state list → Show managed resources.
☁️ 9.Simple Terraform Examples (AWS EC2)
Example 1: Launch a Single EC2 Instance
main.tf
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "my_ec2" {
ami = "ami-08c40ec9ead489470" # Amazon Linux 2
instance_type = "t2.micro"
tags = {
Name = "MyFirstEC2"
}
}
Steps to Run
terraform init
terraform plan
terraform apply -auto-approve
Output
✅ EC2 instance created in AWS.
Check AWS console → EC2 → Running instance named MyFirstEC2.
Example 2: Launch EC2 with Security Group
main.tf
provider "aws" {
region = "us-east-1"
}
resource "aws_security_group" "allow_ssh" {
name = "allow_ssh"
description = "Allow SSH inbound traffic"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_instance" "my_ec2" {
ami = "ami-08c40ec9ead489470"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.allow_ssh.id]
tags = {
Name = "EC2-with-SG"
}
}
Output
✅ EC2 instance created with a security group allowing SSH access.
Example 3: Launch Multiple EC2 Instances
main.tf
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "servers" {
count = 2
ami = "ami-08c40ec9ead489470"
instance_type = "t2.micro"
tags = {
Name = "Server-${count.index + 1}"
}
}
Output
✅ Two EC2 instances created: Server-1 and Server-2.
🎯 Final Thoughts
Terraform is one of the most powerful tools for infrastructure automation in the DevOps world.
Start small (like EC2 provisioning).
Use plan before apply.
Store your .tfstate files securely (S3 backend recommended).
Combine with Ansible for provisioning + configuration management.
With these basics, you can confidently begin using Terraform in real-world projects.
Note: please have look at latest installation doc from terraform for installation as well as follow the instance types, Ids from AWS or VM in azure to launch.
Top comments (0)