πŸ”§ Week 7: Infrastructure as Code

Day 1: Introduction to Infrastructure as Code

⏱ Duration: 5 Hours

πŸ“š Learning Objectives

  • Understand what Infrastructure as Code is
  • Learn the benefits of IaC over manual provisioning
  • Introduction to Terraform and its ecosystem
  • Install and configure Terraform

πŸ“– Core Concepts (2 Hours)

What is Infrastructure as Code?

Infrastructure as Code (IaC) is the practice of managing and provisioning infrastructure through machine-readable configuration files rather than manual processes.

  • Declarative: Define what you want, not how to create it
  • Version Controlled: Track changes like application code
  • Reproducible: Same config = same infrastructure
  • Automated: No manual clicking in consoles

Manual vs IaC Approach

Manual Process: β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ 1. Login to AWS Console β”‚ β”‚ 2. Click EC2 β†’ Launch Instance β”‚ β”‚ 3. Select AMI, instance type β”‚ β”‚ 4. Configure security groups β”‚ β”‚ 5. Add storage, tags β”‚ β”‚ 6. Launch and wait... β”‚ β”‚ [Time: 10-15 min, Error-prone] β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ IaC Process (Terraform): β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ 1. Write configuration file β”‚ β”‚ 2. Run: terraform apply β”‚ β”‚ 3. Done! β”‚ β”‚ [Time: 2 min, Reproducible] β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Benefits of IaC

βœ“ Consistency - Same environment every time βœ“ Speed - Deploy infrastructure in minutes βœ“ Documentation - Config files document your infra βœ“ Version Control- Git history of all changes βœ“ Reusability - Use modules for common patterns βœ“ Cost Control - Easy to destroy and recreate βœ“ Collaboration - Team reviews infrastructure changes

IaC Tools Comparison

Tool Language Cloud Support ───────────────────────────────────────────── Terraform HCL Multi-cloud CloudFormation YAML/JSON AWS only Pulumi Python/JS/etc Multi-cloud Ansible YAML Multi-cloud ARM Templates JSON Azure only Why Terraform? β€’ Cloud agnostic (AWS, Azure, GCP, etc.) β€’ Large community and modules β€’ Declarative and readable syntax β€’ State management built-in

Terraform Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Terraform Core β”‚ β”‚ (Parses config, builds resource graph) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β–Ό β–Ό β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β” β”‚ AWS β”‚ β”‚ Azure β”‚ β”‚ GCP β”‚ β”‚Providerβ”‚ β”‚Providerβ”‚ β”‚Providerβ”‚ β””β”€β”€β”€β”¬β”€β”€β”€β”˜ β””β”€β”€β”€β”¬β”€β”€β”€β”˜ β””β”€β”€β”€β”¬β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β–Ό β–Ό β–Ό AWS API Azure API GCP API

πŸ”¬ Hands-on Lab (2.5 Hours)

Lab 1: Install Terraform

# On Ubuntu/Debian wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg 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 sudo apt update && sudo apt install terraform # On macOS brew tap hashicorp/tap brew install hashicorp/tap/terraform # Verify installation terraform version terraform -help

Lab 2: Configure AWS Credentials

# Install AWS CLI if not installed curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" unzip awscliv2.zip sudo ./aws/install # Configure credentials aws configure # Enter: # - AWS Access Key ID # - AWS Secret Access Key # - Default region (e.g., us-east-1) # - Default output format (json) # Verify configuration aws sts get-caller-identity

Lab 3: First Terraform File

# Create project directory mkdir -p ~/terraform-labs/lab1 cd ~/terraform-labs/lab1 # Create main.tf cat > main.tf << 'EOF' terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } } provider "aws" { region = "us-east-1" } # This just outputs a message - no resources created yet output "hello" { value = "Hello from Terraform!" } EOF # Initialize Terraform terraform init # See what Terraform will do terraform plan # Apply (creates nothing yet, just outputs) terraform apply

βœ… Day 1 Checklist

  • Understand what IaC is and its benefits
  • Know why Terraform is popular
  • Terraform installed and working
  • AWS CLI configured with credentials
  • Created first Terraform configuration