AWS EC2 ASG Hands-On | A Step-by-Step Guide

A step-by-step instructions on Auto Scaling Groups (ASG)

AWS EC2 ASG Hands-On | A Step-by-Step Guide

AWS Auto Scaling Groups (ASGs) empower you to efficiently manage the elasticity of your infrastructure, ensuring your applications remain responsive and cost-effective under varying workloads. In this comprehensive guide, we'll walk you through the process of setting up and optimizing an AWS Auto Scaling Group, step by step.


Creating a Launch Template

Step 1: Create a New Launch Template:

  1. Log in to your AWS Management Console and navigate to the EC2 Dashboard.

  2. From the EC2 Dashboard, go to "Launch Templates" under the "Instances" dropdown menu.

  3. Click "Create launch template.

Step 2: Configure Details:

  1. Provide a name and description

  2. Select your desired AMI, we are choosing Amazon Linux as the AMI.

  3. Select an instance type, we are choosing t2.micro as the instance type.

  4. Specify key pair and security groups.

Step 3: Advanced Settings - User Data:

  1. In the "Advanced settings" section, add the following User Data script:
#!/bin/bash
yum update -y
yum install -y httpd
service httpd start
systemctl enable httpd
echo "<html><head><title>Test Page</title></head><body><h1>Apache is running!</h1></body></html>" > /var/www/html/index.html

Step 4: Review and Create:

  1. Review your configurations.

  2. Click "Create launch template."

    Utilize this launch template for launching instances or configuring auto scaling groups.

Creating an Auto Scaling Group Using a Launch Template

Step 1: Choose Launch Template

  1. Access the EC2 Dashboard in your AWS Management Console.

  2. Navigate to "Auto Scaling Groups" under the "Auto Scaling" dropdown menu.

  3. Click on "Create Auto Scaling Group."

  4. Choose "Launch template" as the launch configuration type.

  5. Select the desired launch template from the dropdown menu.

  6. Click on "Next"

Step 2: Choose Instance Launch Options

  1. You can keep the same instance attributes or instance type from your launch template, or you can choose to override the launch template by specifying different instance attributes or manually adding instance types..

  2. For most applications, you can use multiple Availability Zones and let EC2 Auto Scaling balance your instances across the zones. The default VPC and default subnets are suitable for getting started quickly.

Step 3: Step 3: Configure Advanced Options (Optional)

  1. Load Balancing:
  • Choose from: No Load Balancer, Attach to an Existing Load Balancer, Attach to a New Load Balancer

  • We will Attach to an "Existing Load Balancer" we created in Guide to Create a Load Balancer (ALB)

  1. Health Checks:
  • Enable EC2 Health Checks (Always enabled).

  • Optionally, enable Elastic Load Balancing Health Checks for increased availability.

  • Set delay time for initial health checks in Health Check Grace Period.

  1. Click on Next

Step 4: Step 4: Configure Group Size and Scaling (Optional)

  1. Group Size: Specify the initial size or desired Capacity of the Auto Scaling group.

  2. Scaling: Define limits for scaling operations:

    • Min Desired Capacity (Equal or less than desired capacity)

    • Max Desired Capacity(Equal or greater than desired capacity)

  3. Automatic Scaling (Optional): Choose whether to use a target tracking scaling policy:

    • No scaling policies: Keep group at initial size.

    • Target tracking scaling policy: Adjust desired capacity based on CloudWatch metric.

  1. Instance Maintenance Policy: Control instance replacement behavior to manage availability and cost:

    • Mixed behavior: New instances launch before terminating existing ones.

    • Prioritize availability: Launch new instances before terminating existing ones.

    • Control costs: Terminate and launch instances simultaneously.

    • Flexible: Customize behavior based on capacity needs.

  2. Instance Scale-In Protection: Enable protection to prevent newly launched instances from immediate termination by scaling activities.

  3. Click on Next

Step 5: Add Notifications (Optional)

  1. Set up notifications to receive alerts about scaling activities or instance health changes if required.

Step 6: Add Tags (Optional)

  1. Assign tags to your auto scaling group for easier management and organization.

Step 7: Review

  1. Review all the configurations you have made.

  2. Ensure everything is set up according to your requirements.

  3. Click "Create Auto Scaling Group" to finalize the process.


Review and Testing

Test 1: Check for instances created ( desired number)

  1. Log in to your AWS Management Console and navigate to the EC2 Dashboard.

  2. From the EC2 Dashboard, go to "Instances" under the "Instances" tab.

  3. You will see the "desired number" of instances running, which should match the specified desired capacity set during the group configuration.

Test 2: Check for Target Groups

If your Auto Scaling Group is associated with a load balancer and target groups, you can also verify the number of instances in the target groups to ensure that they match the desired capacity set for the Auto Scaling Group. Here's how you can do it:

  1. Go to the "Target Groups" section in the Load Balancer dashboard.

  2. Select the target group associated with your Auto Scaling Group.

  3. You will see the same number of targets (instances) listed in the target group as the desired capacity set for the Auto Scaling Group.

  4. Verify that the instances listed in the target group are healthy and properly serving traffic.

  5. Confirm that any load balancer health checks are passing for the instances in the target group.

  6. Monitor the target group for any changes in the number of targets as the Auto Scaling Group dynamically adjusts the instance capacity.

Test 3: DNS Testing in Browser

  1. Go to the "Load Balancers" section in the EC2 dashboard.

  2. Locate the load balancer associated with your Auto Scaling Group.

  3. Click on the load balancer to view its details.

  4. Copy the DNS name of the load balancer from the dashboard.

  5. Paste the DNS name into a text editor or another location for future reference or testing purposes.

  6. Optionally, you can open a web browser and paste the DNS name into the address bar to access your application through the load balancer.

  7. Add load by refreshing to see the load being distributed to Different IP