Skip to main content

Compute

Networking gave us the roads and boundaries of our Lego city. Now it’s time to place the buildings the compute layer. In Terraform, this means deploying EC2 instances for the web and app tiers, configuring auto‑scaling groups for resilience, and using launch templates for consistency. This ensures our application can handle traffic spikes while staying cost‑efficient.


Key Concepts

1. EC2 Instances

  • Web Tier: Public EC2 instances in public subnets.
  • App Tier: Private EC2 instances in private subnets.
  • Instance Types:
    • Web → smaller (t2.micro).
    • App → medium (t3.medium).

2. Launch Templates

  • Define reusable configuration for EC2 instances.
resource "aws_launch_template" "app_template" {
  name_prefix   = "app-template"
  image_id      = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.medium"

  user_data = base64encode("#!/bin/bash\nyum install -y nginx")
}

3. Auto‑Scaling Groups (ASG)

  • Automatically adjust number of instances based on demand.
resource "aws_autoscaling_group" "app_asg" {
  desired_capacity     = 2
  max_size             = 4
  min_size             = 1
  launch_template {
    id      = aws_launch_template.app_template.id
    version = "$Latest"
  }
  vpc_zone_identifier = [aws_subnet.private.id]
}

4. User Data Scripts

  • Bootstrap instances with software/configuration.

Example: Install web server:

user_data = base64encode("#!/bin/bash\nyum install -y httpd\nsystemctl start httpd")

Hands‑On Lab / Demo

Lab: Deploying Web & App Tiers

  1. Create launch template for web tier.
  2. Create auto‑scaling group for app tier.
  3. Apply Terraform → EC2 instances deployed in correct subnets.
  4. Verify scaling by simulating traffic load.

Pro Tips & Best Practices

  • Use launch templates for consistency across environments.
  • Configure health checks for auto‑scaling groups.
  • Keep user data scripts lightweight to use configuration management tools for complex setups.
  • Spread instances across multiple AZs for high availability.
  • Tag instances for easy identification.

Summary & Cheatsheet

  • EC2 = Buildings of the city.
  • Launch templates = Blueprints.
  • Auto‑scaling groups = Dynamic expansion.
  • User data = Initial setup instructions.
Quick mnemonic: Instances → Templates → Scaling → Bootstrap

The Hackers Notebook

Compute is the engine of your 3‑tier infrastructure. By deploying EC2 instances with launch templates and auto‑scaling, you ensure scalability and resilience.


Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 31, 2025