Home

EC2 Auto Scaling

Overview

I worked with several AWS services to create a scalable web application infrastructure. I:

This project gave me practical experience with:

By the end, I had created a fully functional, automatically scaling web application infrastructure that can handle variable loads efficiently while maintaining high availability.

Task 1: Creating a new AMI for Amazon EC2 Auto Scaling

In this task, I launched a new EC2 instance and then created a new AMI based on that running instance. I used the AWS CLI on the Command Host EC2 instance to perform all of these operations.

Task 1.1: Connecting to the Command Host instance

In this task, I used EC2 Instance Connect to connect to the Command Host EC2 instance that was created when the environment was provisioned. I used this instance to run AWS CLI commands.

Note: If I preferred to use an SSH client to connect to the EC2 instance, I could see the guidance to Connect to Your Linux Instance.

Now that I was connected to the Command Host instance, I could configure and use the AWS CLI to call AWS services.

Task 1.2: Configuring the AWS CLI

The AWS CLI was preconfigured on the Command Host instance.

curl http://169.254.169.254/latest/dynamic/instance-identity/document | grep region

I used this Region information in the next steps.

aws configure

Now I was ready to access and run the scripts detailed in the following steps.

cd /home/ec2-user/

Task 1.3: Creating a new EC2 Instance

In this task, I used the AWS CLI to create a new instance that hosts a web server.

more UserData.txt

This script performed a number of initialization tasks, including updating all installed software on the box and installing a small PHP web application that I could use to simulate a high CPU load on the instance. The following lines appeared near the end of the script:

find -wholename /root/.*history -wholename /home/*/.*history -exec rm -f {} \; find / -name 'authorized_keys' -exec rm -f {} \; rm -rf /var/lib/cloud/data/scripts/*

These lines erase any history or security information that might have accidentally been left on the instance when the image was taken...

aws ec2 run-instances --key-name KEYNAME --instance-type t3.micro --image-id AMIID --user-data file:///home/ec2-user/UserData.txt --security-group-ids HTTPACCESS --subnet-id SUBNETID --associate-public-ip-address --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=WebServer}]' --output text --query 'Instances[*].InstanceId'

The output of this command provided me with an InstanceId. Subsequent steps refer to this value as NEW-INSTANCE-ID. I replaced this value as needed throughout.

aws ec2 wait instance-running --instance-ids NEW-INSTANCE-ID

I waited for the command to return to a prompt before proceeding.

My instance started a new web server. To test that the web server was installed properly, I had to obtain the public DNS name.

aws ec2 describe-instances --instance-id NEW-INSTANCE-ID --query 'Reservations[0].Instances[0].NetworkInterfaces[0].Association.PublicDnsName'

The value of this output is referred to as PUBLIC-DNS-ADDRESS in the next steps.

It could take a few minutes for the web server to be installed. I waited 5 minutes before continuing to the next steps.

I did not choose Start Stress at this stage.

http://PUBLIC-DNS-ADDRESS/index.php

If my web server did not appear to be running, I would check with my instructor.

Task 1.4: Creating a Custom AMI

In this task, I created a new AMI based on that instance that I just created.

aws ec2 create-image --name WebServerAMI --instance-id NEW-INSTANCE-ID

By default, the aws ec2 create-image command restarts the current instance before creating the AMI to ensure the integrity of the image on the file system. While my AMI was being created, I proceeded to the next task.

Task 2: Creating an auto scaling environment

In this section, I created a load balancer that pools a group of EC2 instances under a single Domain Name System (DNS) address. I used auto scaling to create a dynamically scalable pool of EC2 instances based on the image that I created in the previous task. Finally, I created a set of alarms that scale out or scale in the number of instances in my load balancer group whenever the CPU performance of any machine within the group exceeds or falls below a set of specified thresholds.

I could perform the following task by using either the AWS CLI or the AWS Management Console. For this project, I used the AWS Management Console.

Task 2.1: Creating an Application Load Balancer

In this task, I created a load balancer that can balance traffic across multiple EC2 instances and Availability Zones.

These options configured the load balancer to operate across multiple Availability Zones.

The HTTPAccess security group had already been created for me, which permits HTTP access.

Note: This link opened a new browser tab with the Create target group configuration options.

I received a message similar to the following:

Successfully created load balancer: WebServerELB

I needed this information later.

Task 2.2: Creating a launch template

In this task, I created a launch template for my Auto Scaling group. A launch template is a template that an Auto Scaling group uses to launch EC2 instances. When I created a launch template, I specified information for the instances, such as the AMI, instance type, key pair, security group, and disks.

Amazon EC2 uses public key cryptography to encrypt and decrypt login information. To log in to your instance, you must create a key pair, specify the name of the key pair when you launch the instance, and provide the private key when you connect to the instance.

Note: I did not need to connect to the instance.

When you launch an instance, you can pass user data to the instance. The data can be used to run configuration tasks and scripts.

I received a message similar to the following:

Successfully created web-app-launch-template.

Task 2.3: Creating an Auto Scaling group

In this task, I used my launch template to create an Auto Scaling group.

This change told auto scaling to maintain an average CPU utilization across all instances of 50 percent. Auto scaling automatically adds or removes capacity as required to keep the metric at or close to the specified target value. It adjusts to fluctuations in the metric due to a fluctuating load pattern.

These options launched EC2 instances in private subnets across both Availability Zones.

My Auto Scaling group initially showed an Instances count of zero, but new instances were launched to reach the desired count of two instances.

Note: If I experienced an error related to the t3.micro instance type not being available, then I would rerun this task by choosing the t2.micro instance type instead.

Task 3: Verifying the auto scaling configuration

In this task, I verified that both the auto scaling configuration and the load balancer were working by accessing a pre-installed script on one of my servers that would consume CPU cycles, which invokes the scale out alarm.

Two new instances named WebApp were being created as part of my Auto Scaling group. While these instances were being created, the Status check for these two instances was Initializing.

I might need to choose Refresh to see the updated status.

I could now test the web application by accessing it through the load balancer.

Task 4: Testing auto scaling configuration

This step called the application stress in the background, which caused the CPU utilization on the instance that serviced this request to spike to 100 percent.

After a few minutes, I saw my Auto Scaling group add a new instance. This occurred because Amazon CloudWatch detected that the average CPU utilization of my Auto Scaling group exceeded 50 percent, and my scale-up policy was invoked in response.

I could also check the new instances being launched on the EC2 Dashboard.

Troubleshooting Guide

Initial Setup Values

For reference, here are the key values I used:

Common Issues and Solutions

Authentication Failure

If I encountered the error "AWS was not able to validate the provided access credentials", I would follow these steps:

  1. Verify AWS CLI configuration using aws configure
  2. Edit credentials file with sudo permissions:
    sudo su nano /home/ec2-user/.aws/credentials
  3. Add the following structure to the credentials file:
    [default] aws_access_key_id= aws_secret_access_key= aws_session_token=
  4. Verify the credentials:
    aws sts get-caller-identity
  5. Exit superuser mode:
    exit

Instance Verification

After launching my instance, I would verify its status using:

aws ec2 wait instance-running --instance-ids

Web Server Verification

To verify the web server installation:

  1. Get the public DNS name:
    aws ec2 describe-instances --instance-ids --query 'Reservations[*].Instances[*].PublicDnsName' --output text
  2. Access the web server using the format:
    http:///index.php
  3. Wait 5 minutes after instance launch before attempting to access the web server
Note: If the web server doesn't respond after 5 minutes, verify that:

Summary

Related Topics