Real-world scenario
Your app goes viral overnight. A single EC2 melts under load. With an ASG behind an ALB, traffic spreads across AZs and the fleet grows from 2 to 6 instances automatically — then shrinks back when the spike passes.
By the end you can:
Follow the request/data flow through the architecture.
Work through each concept and mark it understood. This unlocks the knowledge check.
Maintains a desired number of EC2 instances, adding/removing them on demand.
Why it matters
Turns a fragile single server into a self-healing, elastic fleet.
When to use it
Use for stateless tiers behind a load balancer. Pair target-tracking policies with health checks.
Example
resource "aws_autoscaling_policy" "cpu" {
name = "cpu-target-50"
autoscaling_group_name = aws_autoscaling_group.web.name
policy_type = "TargetTrackingScaling"
target_tracking_configuration {
predefined_metric_specification { predefined_metric_type = "ASGAverageCPUUtilization" }
target_value = 50.0
}
}Gotcha (exam trap)
Scaling cannot fix a stateful app — session data must live in a shared store (ElastiCache/DynamoDB), not on the instance.
A realistic main.tf for this mission. Read it top to bottom — it mirrors the concepts above.
# Auto Scaling Groups & Load Balancers
# BleepxCloud — AWS · Intermediate
# Realistic starter — adapt names, regions and CIDRs to your environment.
terraform {
required_providers { aws = { source = "hashicorp/aws", version = "~> 5.0" } }
}
provider "aws" {
region = "eu-west-1" # pick a region close to users / data-residency rules
}
# FinOps: mandatory cost-allocation tags on every resource.
locals {
common_tags = {
Project = "bleepx-cloud"
Domain = "core"
Mission = "ec2-auto-scaling"
ManagedBy = "terraform"
CostCenter = "REPLACE_ME"
}
}
data "aws_caller_identity" "current" {}
# Self-healing, elastic fleet behind a target-tracking policy.
resource "aws_autoscaling_group" "web" {
name = "web-asg"
min_size = 2
max_size = 6
desired_capacity = 2
vpc_zone_identifier = aws_subnet.private[*].id
target_group_arns = [aws_lb_target_group.web.arn]
health_check_type = "ELB"
launch_template {
id = aws_launch_template.web.id
version = "$Latest"
}
}
resource "aws_autoscaling_policy" "cpu" {
name = "cpu-target-50"
autoscaling_group_name = aws_autoscaling_group.web.name
policy_type = "TargetTrackingScaling"
target_tracking_configuration {
predefined_metric_specification { predefined_metric_type = "ASGAverageCPUUtilization" }
target_value = 50.0
}
}
# Layer-7 load balancer spanning two AZs (a hard requirement).
resource "aws_lb" "web" {
name = "web-alb"
load_balancer_type = "application"
subnets = aws_subnet.public[*].id
security_groups = [aws_security_group.alb.id]
}
resource "aws_lb_target_group" "web" {
name = "web-tg"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.main.id
health_check { path = "/health"; matcher = "200" }
}
resource "aws_lb_listener" "https" {
load_balancer_arn = aws_lb.web.arn
port = 443
protocol = "HTTPS"
certificate_arn = var.acm_cert_arn
default_action { type = "forward"; target_group_arn = aws_lb_target_group.web.arn }
}
Finish the walkthrough above to unlock the check.
Bleepx skills build on each other. Continue your learning path across Query, Lab, and Cloud.