10th Week :- Terraform in DevOps: Complete Guide with AWS, Kubernetes & CI/CD Examples

Table of contents
- π 1. What is Terraform?
- π 2. Why Terraform in DevOps?
- π 3. Key Benefits of Terraform π
- π 4. Terraform Core Concepts π§
- π 5. Terraform Use Cases π
- π 6. Sample Terraform AWS Deployment β
- π 7. Deploy to AWS with Terraform π
- π 8. Terraform in CI/CD DevOps Workflow π
- π 9. Advanced Terraform Features π‘
- π 10. Real-Life Example: AWS VPC + EC2 with Terraform π
- π 11. AWS Example β Deploy EC2 & VPC β
- π 12. Kubernetes Example β Deploy an NGINX Pod π³
- π 13. Terraform + CI/CD Pipeline (GitHub Actions Example) π
- π 14. Summary β¨
π 1. What is Terraform?
Terraform is an Infrastructure as Code (IaC) tool developed by HashiCorp.
It allows DevOps engineers, cloud architects, and developers to define, provision, and manage infrastructure using a declarative configuration language (HCL - HashiCorp Configuration Language).
Instead of manually creating resources in a cloud providerβs console, you write code that describes the desired infrastructure, and Terraform takes care of creating, updating, and managing it.
Think of Terraform like a blueprint for your cloud. You describe what you want, and Terraform builds it for you.
π 2. Why Terraform in DevOps?
In DevOps, speed, consistency, and automation are key. Terraform helps achieve these by:
Automating infrastructure setup and tear-down
Version-controlling infrastructure changes like software code
Ensuring consistency across environments (Dev, Staging, Prod)
Integrating easily with CI/CD pipelines
π 3. Key Benefits of Terraform π
Benefit | Description |
Cloud Agnostic β | Works with AWS, Azure, GCP, Kubernetes, VMware, etc. |
Declarative Syntax π | You describe what you want, not how to do it |
Immutable Infrastructure π‘ | Updates replace resources safely, avoiding config drift |
State Management π | Tracks infrastructure state for accuracy |
Collaboration Ready π€ | Works with Git for team-based infra management |
Scalable π | Handles large, complex deployments easily |
π 4. Terraform Core Concepts π§
Provider β Defines the cloud or platform (e.g., AWS, Azure, GCP, Kubernetes).
Resource β The infrastructure component (EC2 instance, S3 bucket, VPC, etc.).
Variables β Parameters for dynamic configurations.
State File (
terraform.tfstate
) β Keeps track of created resources.Modules β Reusable sets of Terraform files.
Plan & Apply β Plan previews changes, Apply executes them.
π 5. Terraform Use Cases π
a) Multi-Cloud Deployments
Easily deploy resources to AWS + Azure + GCP simultaneously.
b) Immutable Infrastructure
Easily replace outdated infrastructure instead of patching manually.
c) Infrastructure Version Control
Keep track of infrastructure changes in GitHub.
d) Automated Environment Provisioning
Spin up staging/prod environments in minutes.
π 6. Sample Terraform AWS Deployment β
Hereβs an example AWS EC2 Instance creation with Terraform.
Step 1: Install Terraform
# For Linux / Mac
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
# For Windows
choco install terraform
Step 2: Create Terraform Files
π Folder structure:
terraform-aws-demo/
βββ main.tf
βββ variables.tf
βββ outputs.tf
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "web" {
ami = "ami-0c02fb55956c7d316" # Amazon Linux 2
instance_type = "t2.micro"
tags = {
Name = "Terraform-EC2"
}
}
variables.tf (optional for customization):
variable "region" {
default = "us-east-1"
}
output "instance_ip" {
value = aws_instance.web.public_ip
}
π 7. Deploy to AWS with Terraform π
# Step 1: Initialize Terraform
terraform init
# Step 2: Preview what will be created
terraform plan
# Step 3: Apply changes (create resources)
terraform apply
# Step 4: Destroy infrastructure (when no longer needed)
terraform destroy
π 8. Terraform in CI/CD DevOps Workflow π
Terraform fits perfectly into a DevOps pipeline:
Write β Create
.tf
files describing infra.Plan β Run
terraform plan
in CI to preview changes.Review β Code review via Pull Requests.
Apply β Auto-deploy on merge to main branch.
Monitor β Track infra drift with Terraform Cloud.
π 9. Advanced Terraform Features π‘
Feature | Why itβs Useful |
Remote State Storage | Share state between team members via S3, GCS, Azure Blob, etc. |
Workspaces | Manage multiple environments (dev, staging, prod) |
Modules | Reusable infrastructure blueprints |
Data Sources | Use existing resources without recreating |
Provisioners | Run scripts on created instances |
π 10. Real-Life Example: AWS VPC + EC2 with Terraform π
provider "aws" {
region = "us-east-1"
}
# Create VPC
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
# Create Subnet
resource "aws_subnet" "subnet1" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a"
}
# Create EC2 Instance
resource "aws_instance" "web" {
ami = "ami-0c02fb55956c7d316"
instance_type = "t2.micro"
subnet_id = aws_subnet.subnet1.id
tags = {
Name = "Web-Server"
}
}
π 11. AWS Example β Deploy EC2 & VPC β
Folder Structure
terraform-aws/
βββ main.tf
βββ variables.tf
βββ outputs.tf
provider "aws" {
region = var.aws_region
}
# Create VPC
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
# Create Subnet
resource "aws_subnet" "subnet1" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a"
}
# Create EC2 Instance
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = "t2.micro"
subnet_id = aws_subnet.subnet1.id
tags = {
Name = "Terraform-Web-Server"
}
}
variable "aws_region" {
default = "us-east-1"
}
variable "ami_id" {
default = "ami-0c02fb55956c7d316" # Amazon Linux 2
}
output "instance_ip" {
value = aws_instance.web.public_ip
}
Deployment
terraform init
terraform plan
terraform apply
terraform destroy # To clean up
π 12. Kubernetes Example β Deploy an NGINX Pod π³
Terraform can also manage Kubernetes clusters and workloads.
Pre-requisites
A running Kubernetes cluster (EKS, AKS, GKE, or Minikube)
kubectl
configured
provider "kubernetes" {
config_path = "~/.kube/config"
}
resource "kubernetes_namespace" "demo" {
metadata {
name = "terraform-demo"
}
}
resource "kubernetes_deployment" "nginx" {
metadata {
name = "nginx-deploy"
namespace = kubernetes_namespace.demo.metadata[0].name
}
spec {
replicas = 2
selector {
match_labels = {
app = "nginx"
}
}
template {
metadata {
labels = {
app = "nginx"
}
}
spec {
container {
image = "nginx:latest"
name = "nginx"
port {
container_port = 80
}
}
}
}
}
}
Deploy to Kubernetes
terraform init
terraform apply
π 13. Terraform + CI/CD Pipeline (GitHub Actions Example) π
You can automate Terraform deployments using GitHub Actions.
.github/workflows/terraform.yml
name: Terraform Deploy
on:
push:
branches: [ "main" ]
jobs:
terraform:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
- name: Terraform Init
run: terraform init
- name: Terraform Plan
run: terraform plan
- name: Terraform Apply
run: terraform apply -auto-approve
Now, every push to main
will auto-deploy infrastructure
π 14. Summary β¨
Terraform is a must-have tool in DevOps for managing cloud infrastructure in a consistent, automated, and scalable way. Whether youβre building a small project or a multi-cloud enterprise system, Terraform helps you:
β
Save time
β
Reduce human errors
β
Improve collaboration
β
Scale infrastructure efficiently
π‘ Pro Tip: Use Terraform Cloud for remote state, team collaboration, and secure secret management.
Subscribe to my newsletter
Read articles from Lav kushwaha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
