Auto Scaling with ELB integration in AWS - Commands & Configuration
provider "aws" { region = "us-east-1" } resource "aws_launch_configuration" "example" { name_prefix = "example-launch-config-" image_id = "ami-0c94855ba95c71c99" instance_type = "t2.micro" security_groups = ["sg-0123456789abcdef0"] lifecycle { create_before_destroy = true } } resource "aws_autoscaling_group" "example" { name = "example-asg" max_size = 3 min_size = 1 desired_capacity = 1 launch_configuration = aws_launch_configuration.example.name vpc_zone_identifier = ["subnet-0123456789abcdef0"] health_check_type = "ELB" health_check_grace_period = 300 target_group_arns = [aws_lb_target_group.example.arn] tag { key = "Name" value = "example-instance" propagate_at_launch = true } } resource "aws_lb" "example" { name = "example-lb" internal = false load_balancer_type = "application" security_groups = ["sg-0123456789abcdef0"] subnets = ["subnet-0123456789abcdef0"] } resource "aws_lb_target_group" "example" { name = "example-tg" port = 80 protocol = "HTTP" vpc_id = "vpc-0123456789abcdef0" health_check { path = "/" interval = 30 timeout = 5 healthy_threshold = 5 unhealthy_threshold = 2 matcher = "200-299" } } resource "aws_lb_listener" "example" { load_balancer_arn = aws_lb.example.arn port = 80 protocol = "HTTP" default_action { type = "forward" target_group_arn = aws_lb_target_group.example.arn } }
This Terraform file creates an AWS Auto Scaling Group linked to an Application Load Balancer (ALB).
aws_launch_configuration: Defines the server setup like image and type.
aws_autoscaling_group: Manages the number of servers, links to the load balancer target group, and sets health checks.
aws_lb: Creates the Application Load Balancer.
aws_lb_target_group: Groups servers for the load balancer to send traffic to, with health checks.
aws_lb_listener: Listens on port 80 and forwards traffic to the target group.
terraform init
terraform plan
terraform apply -auto-approve
-auto-approve - Skip confirmation prompt to apply changes immediatelyaws autoscaling describe-auto-scaling-groups --auto-scaling-group-names example-asg
--auto-scaling-group-names - Specify the name of the Auto Scaling Group to describeIf you remember nothing else from this pattern, remember: Auto Scaling automatically adjusts server count based on demand, and ELB ensures traffic goes only to healthy servers.
Practice
Solution
Step 1: Understand Auto Scaling and ELB roles
Auto Scaling adjusts the number of servers based on demand. ELB distributes user traffic across these servers.Step 2: Identify integration benefit
Linking Auto Scaling with ELB ensures traffic is balanced only to healthy, running instances, improving availability.Final Answer:
To distribute incoming traffic evenly across all running instances -> Option AQuick Check:
Auto Scaling + ELB = balanced traffic [OK]
- Confusing ELB with storage or backup services
- Thinking Auto Scaling manages data persistence
- Assuming ELB monitors databases
Solution
Step 1: Identify correct AWS CLI command
The command to attach an Auto Scaling group to an ELB target group is 'aws autoscaling attach-load-balancer-target-groups'.Step 2: Verify command parameters
It requires the Auto Scaling group name and the target group ARN, matching aws autoscaling attach-load-balancer-target-groups --auto-scaling-group-name my-asg --target-group-arns arn:aws:elasticloadbalancing:region:123456789012:targetgroup/my-target-group/abcdef123456.Final Answer:
aws autoscaling attach-load-balancer-target-groups --auto-scaling-group-name my-asg --target-group-arns arn:aws:elasticloadbalancing:region:123456789012:targetgroup/my-target-group/abcdef123456 -> Option DQuick Check:
Attach ASG to ELB target group uses attach-load-balancer-target-groups [OK]
- Using ELB commands instead of Auto Scaling commands
- Confusing target group ARN with target group name
- Trying to create target groups via Auto Scaling CLI
{
"AutoScalingGroupName": "web-asg",
"TargetGroupARNs": ["arn:aws:elasticloadbalancing:region:123456789012:targetgroup/web-tg/abc123"],
"MinSize": 2,
"MaxSize": 4,
"DesiredCapacity": 3
}What happens when one instance fails health checks in the ELB target group?
Solution
Step 1: Understand health check integration
When an instance fails ELB health checks, Auto Scaling detects it as unhealthy.Step 2: Auto Scaling response
Auto Scaling terminates the unhealthy instance and launches a new one to keep the desired capacity at 3.Final Answer:
Auto Scaling terminates the unhealthy instance and launches a new one to maintain desired capacity -> Option AQuick Check:
Unhealthy instance triggers replacement by Auto Scaling [OK]
- Thinking ELB alone replaces instances
- Assuming MaxSize changes automatically
- Believing manual steps are needed
Solution
Step 1: Check ELB and Auto Scaling integration
If instances are not registered with the ELB target group, ELB cannot send traffic to them.Step 2: Evaluate other options
MinSize zero means no instances; region mismatch prevents communication; public IP not required for ELB internal routing.Final Answer:
The instances are not registered with the ELB target group -> Option CQuick Check:
Instances must be registered with ELB target group to get traffic [OK]
- Assuming public IP is required for ELB traffic
- Ignoring instance registration status
- Confusing region mismatch with availability zone issues
Solution
Step 1: Define scaling trigger
Scaling based on average CPU > 70% ensures scale out matches demand.Step 2: Ensure instance health before traffic
Enabling ELB health checks with a grace period lets new instances become healthy before receiving traffic.Step 3: Combine both for smooth scaling
This combination prevents traffic to unhealthy instances and scales only when needed.Final Answer:
Create a scaling policy based on CPU > 70%, enable ELB health checks with grace period -> Option BQuick Check:
CPU scaling + ELB health checks = safe, demand-based scaling [OK]
- Disabling ELB health checks causing traffic to unhealthy instances
- Using wrong metric like network traffic for CPU-based scaling
- Not setting grace period causing premature traffic routing
Ready to prove it?
Pass the test and this pattern moves unknown
