Bird
Raised Fist0
AWScloud~10 mins

High availability design patterns in AWS - Commands & Configuration

Choose your learning style10 modes available
Introduction
High availability design patterns help keep your applications running without interruption. They reduce downtime by using multiple resources that can take over if one fails.
When you want your website to stay online even if one server crashes
When you need your database to be accessible all the time without losing data
When you want to handle sudden traffic spikes without slowing down
When you want to deploy your app across different locations to avoid regional failures
When you want automatic recovery from hardware or software failures
Config File - main.tf
main.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "subnet1" {
  vpc_id            = aws_vpc.main.id
  cidr_block        = "10.0.1.0/24"
  availability_zone = "us-east-1a"
}

resource "aws_subnet" "subnet2" {
  vpc_id            = aws_vpc.main.id
  cidr_block        = "10.0.2.0/24"
  availability_zone = "us-east-1b"
}

resource "aws_lb" "app_lb" {
  name               = "app-load-balancer"
  internal           = false
  load_balancer_type = "application"
  subnets            = [aws_subnet.subnet1.id, aws_subnet.subnet2.id]
}

resource "aws_autoscaling_group" "app_asg" {
  name                      = "app-asg"
  max_size                  = 4
  min_size                  = 2
  desired_capacity          = 2
  vpc_zone_identifier       = [aws_subnet.subnet1.id, aws_subnet.subnet2.id]
  health_check_type         = "ELB"
  health_check_grace_period = 300

  launch_configuration = aws_launch_configuration.app_lc.name
  target_group_arns    = [aws_lb_target_group.app_tg.arn]
}

resource "aws_launch_configuration" "app_lc" {
  name_prefix   = "app-lc-"
  image_id      = "ami-0c94855ba95c71c99"
  instance_type = "t3.micro"
  security_groups = [aws_security_group.app_sg.id]
  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_lb_target_group" "app_tg" {
  name     = "app-target-group"
  port     = 80
  protocol = "HTTP"
  vpc_id   = aws_vpc.main.id
  health_check {
    path                = "/"
    interval            = 30
    timeout             = 5
    healthy_threshold   = 5
    unhealthy_threshold = 2
  }
}

resource "aws_lb_listener" "app_listener" {
  load_balancer_arn = aws_lb.app_lb.arn
  port              = 80
  protocol          = "HTTP"

  default_action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.app_tg.arn
  }
}

resource "aws_security_group" "app_sg" {
  name        = "app-sg"
  description = "Allow HTTP inbound"
  vpc_id      = aws_vpc.main.id

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

This Terraform file creates a high availability setup on AWS.

  • VPC and Subnets: Defines a private network with two subnets in different availability zones for fault tolerance.
  • Load Balancer: Distributes incoming traffic across multiple instances.
  • Auto Scaling Group: Automatically manages the number of instances to handle load and replace failed ones.
  • Launch Configuration: Defines the instance type and settings for new instances.
  • Target Group and Listener: Connects the load balancer to the instances and listens on port 80.
  • Security Group: Allows HTTP traffic to the instances.
Commands
Initializes Terraform in the current directory to download necessary plugins and prepare for deployment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes required for your infrastructure.
Applies the Terraform configuration to create the high availability infrastructure automatically without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_vpc.main: Creating... aws_vpc.main: Creation complete after 3s [id=vpc-0a1b2c3d4e5f6g7h8] aws_subnet.subnet1: Creating... aws_subnet.subnet2: Creating... aws_subnet.subnet1: Creation complete after 2s [id=subnet-0123456789abcdef0] aws_subnet.subnet2: Creation complete after 2s [id=subnet-0fedcba9876543210] aws_security_group.app_sg: Creating... aws_security_group.app_sg: Creation complete after 1s [id=sg-0a1b2c3d4e5f6g7h8] aws_launch_configuration.app_lc: Creating... aws_launch_configuration.app_lc: Creation complete after 1s [id=app-lc-20240610] aws_lb.app_lb: Creating... aws_lb.app_lb: Creation complete after 4s [id=app-load-balancer] aws_lb_target_group.app_tg: Creating... aws_lb_target_group.app_tg: Creation complete after 2s [id=app-target-group] aws_lb_listener.app_listener: Creating... aws_lb_listener.app_listener: Creation complete after 1s [id=app-listener] aws_autoscaling_group.app_asg: Creating... aws_autoscaling_group.app_asg: Creation complete after 3s [id=app-asg] Apply complete! Resources: 10 added, 0 changed, 0 destroyed.
→
-auto-approve - Skips manual approval to apply changes immediately
Checks the status and details of the created load balancer to confirm it is active and ready to route traffic.
Terminal
aws elbv2 describe-load-balancers --names app-load-balancer
Expected OutputExpected
{ "LoadBalancers": [ { "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/app-load-balancer/50dc6c495c0c9188", "DNSName": "app-load-balancer-1234567890.us-east-1.elb.amazonaws.com", "CanonicalHostedZoneId": "Z35SXDOTRQ7X7K", "CreatedTime": "2024-06-10T12:00:00.000Z", "LoadBalancerName": "app-load-balancer", "Scheme": "internet-facing", "VpcId": "vpc-0a1b2c3d4e5f6g7h8", "State": { "Code": "active" }, "Type": "application", "AvailabilityZones": [ { "ZoneName": "us-east-1a", "SubnetId": "subnet-0123456789abcdef0" }, { "ZoneName": "us-east-1b", "SubnetId": "subnet-0fedcba9876543210" } ], "SecurityGroups": [ "sg-0a1b2c3d4e5f6g7h8" ] } ] }
→
--names - Specifies the load balancer name to describe
Verifies the auto scaling group is running the desired number of instances across availability zones for high availability.
Terminal
aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names app-asg
Expected OutputExpected
{ "AutoScalingGroups": [ { "AutoScalingGroupName": "app-asg", "MinSize": 2, "MaxSize": 4, "DesiredCapacity": 2, "Instances": [ { "InstanceId": "i-0123456789abcdef0", "AvailabilityZone": "us-east-1a", "LifecycleState": "InService", "HealthStatus": "Healthy" }, { "InstanceId": "i-0fedcba9876543210", "AvailabilityZone": "us-east-1b", "LifecycleState": "InService", "HealthStatus": "Healthy" } ] } ] }
→
--auto-scaling-group-names - Specifies the auto scaling group to describe
Key Concept

If you remember nothing else from this pattern, remember: spreading resources across multiple zones with automatic failover keeps your app running even when parts fail.

Common Mistakes
Creating all resources in a single availability zone
If that zone fails, your whole app goes down, losing high availability.
Always deploy resources like subnets and instances in at least two different availability zones.
Not attaching instances to a load balancer
Traffic won't be distributed, and failed instances won't be replaced automatically.
Use a load balancer with target groups and auto scaling groups to manage traffic and health.
Setting min_size and desired_capacity to zero in auto scaling group
No instances will run, so your app will be offline.
Set min_size and desired_capacity to at least 1 to keep instances running.
Summary
Initialize Terraform to prepare the environment with 'terraform init'.
Apply the configuration to create high availability resources with 'terraform apply -auto-approve'.
Check the load balancer status to ensure it is active and ready.
Verify the auto scaling group is running instances in multiple availability zones.

Practice

(1/5)
1. What is the main goal of a high availability design pattern in AWS?
easy
A. To reduce the cost of cloud resources
B. To keep the application running even if some parts fail
C. To increase the speed of the application only
D. To limit the number of users accessing the app

Solution

  1. Step 1: Understand high availability concept

    High availability means designing systems to stay online and functional despite failures.
  2. Step 2: Match goal with options

    Only To keep the application running even if some parts fail describes keeping the app running during failures, which is the goal.
  3. Final Answer:

    To keep the application running even if some parts fail -> Option B
  4. Quick Check:

    High availability = keep running despite failures [OK]
Hint: High availability means no downtime during failures [OK]
Common Mistakes:
  • Confusing cost reduction with availability
  • Thinking speed alone defines high availability
  • Assuming limiting users improves availability
2. Which AWS service is commonly used to distribute incoming traffic across multiple instances for high availability?
easy
A. Amazon S3
B. AWS Lambda
C. Amazon RDS
D. Elastic Load Balancer (ELB)

Solution

  1. Step 1: Identify service for traffic distribution

    Elastic Load Balancer (ELB) distributes traffic to multiple instances to avoid single points of failure.
  2. Step 2: Eliminate other options

    Amazon S3 stores files, RDS is a database, Lambda runs code; none distribute traffic.
  3. Final Answer:

    Elastic Load Balancer (ELB) -> Option D
  4. Quick Check:

    Traffic distribution = ELB [OK]
Hint: Load balancers spread traffic to keep apps available [OK]
Common Mistakes:
  • Choosing storage or compute services instead of load balancer
  • Confusing Lambda with traffic distribution
  • Selecting database services for traffic handling
3. Consider this AWS setup: An application runs in two Availability Zones (AZs) behind an Elastic Load Balancer. If one AZ fails, what happens to the app availability?
medium
A. The app slows down but stays available in the failed AZ
B. The app becomes completely unavailable
C. The app continues running using the healthy AZ
D. The app automatically scales down to zero instances

Solution

  1. Step 1: Understand multi-AZ deployment

    Running in multiple AZs means if one AZ fails, others can serve traffic.
  2. Step 2: Analyze ELB behavior

    ELB routes traffic only to healthy instances, so app stays available in the healthy AZ.
  3. Final Answer:

    The app continues running using the healthy AZ -> Option C
  4. Quick Check:

    Multi-AZ + ELB = app stays available [OK]
Hint: Multi-AZ with ELB keeps app running if one AZ fails [OK]
Common Mistakes:
  • Assuming total outage if one AZ fails
  • Thinking app slows but serves from failed AZ
  • Believing app scales down automatically on failure
4. You configured an Auto Scaling group with minimum 2 instances across two AZs, but during a failure, only one instance is running. What is the likely issue?
medium
A. Auto Scaling group is not set to replace unhealthy instances
B. Load balancer is misconfigured to route traffic
C. Instances are launched in the same AZ only
D. Security groups block instance communication

Solution

  1. Step 1: Check Auto Scaling group settings

    Auto Scaling must replace unhealthy instances to maintain minimum capacity.
  2. Step 2: Understand failure impact

    If replacement is disabled, failed instance won't be replaced, causing fewer running instances.
  3. Final Answer:

    Auto Scaling group is not set to replace unhealthy instances -> Option A
  4. Quick Check:

    Auto Scaling replaces unhealthy = maintain capacity [OK]
Hint: Auto Scaling must replace failed instances to keep count [OK]
Common Mistakes:
  • Blaming load balancer for instance count
  • Assuming instances launched only in one AZ
  • Thinking security groups affect instance count
5. You want to design a highly available web app on AWS that stays online even if an entire region fails. Which combination should you use?
hard
A. Deploy instances in multiple regions with Route 53 latency-based routing and health checks
B. Deploy instances in multiple AZs within one region and use an Elastic Load Balancer
C. Use a single instance in one AZ with Auto Scaling enabled
D. Deploy instances in one AZ and use Amazon S3 for static content

Solution

  1. Step 1: Understand region failure scope

    Multi-AZ protects against AZ failure, but not entire region failure.
  2. Step 2: Choose multi-region design

    Deploying in multiple regions with Route 53 routing and health checks ensures availability if one region fails.
  3. Final Answer:

    Deploy instances in multiple regions with Route 53 latency-based routing and health checks -> Option A
  4. Quick Check:

    Multi-region + Route 53 = survive region failure [OK]
Hint: Multi-region with Route 53 handles region failures [OK]
Common Mistakes:
  • Thinking multi-AZ covers region failure
  • Relying on single instance with Auto Scaling
  • Ignoring DNS routing for multi-region failover
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard