0
0
AWScloud~10 mins

High availability design patterns in AWS - Commands & Configuration

Choose your learning style9 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.