Day 5: Scaling Infrastructure and Managing Terraform State

Today in my #30DaysOfTerraform challenge, I explored two key areas:

📈 Scaling Infrastructure

I scaled a web server cluster using count in Terraform to spin up multiple EC2 instances based on demand.

resource "aws_instance" "web" {
  count         = var.instance_count
  ami           = var.ami_id
  instance_type = var.instance_type
  ...
}

🌐 Load Balancer Deployment

I deployed an Application Load Balancer (ALB) to distribute traffic across my cluster, increasing availability and resilience.

🔍 Understanding Terraform State

I learned how Terraform uses a terraform.tfstate file to track infrastructure. When I deleted an instance manually from AWS, Terraform tried to recreate it — showing the importance of not manually changing cloud resources outside of Terraform.

📊 Comparison Table of Terraform Blocks

Block Description Example
resource Declares infrastructure (e.g., EC2, VPC) resource "aws_instance" "web" {...}
variable Inputs to parametrize configurations variable "instance_type" {...}
output Returns useful values output "web_ip" {...}
provider Specifies the provider (e.g., AWS) provider "aws" {...}
module Reusable group of resources module "web_cluster" {...}

💡 Key Takeaway

Always let Terraform manage your infrastructure to avoid state drift. Manual changes to resources should be avoided!

#30DaysOfTerraform #IaC #DevOps #AWS #Terraform