The following procedures help you set up a scaled and load-balanced application, you will attach a load balancer to your Auto Scaling group. The load balancer automatically distributes incoming traffic across the instances in the group. And a CloudFront generates by Load balancer. This tutorial attaches a load balancer to an Auto Scaling group when you create the group, and set up a scaling policy to be triggered of target tracking scaling policy.
The workshop’s region will be in ‘N.Virginia’
Prepare VPC, if there's no VPC's,you can use lab-network to deploy
Elastic Load Balancing automatically distributes incoming application traffic across multiple targets, such as EC2 instances, containers, IP addresses, and Lambda functions. It also can handle the varying load of your application traffic in a single Availability Zone or across multiple Availability Zones. This part will walk you to create an Application Load Balancer to distributes incoming application traffic to EC2.
-
On the service menu, click EC2.
-
In the navigation pane, click Load Balancers.
-
Click Create Load Balancer.
-
Choose Application Load Balancer, and click Create.
-
Specify the following settings:
- Name:
WebServerLB
- Scheme: choose internet-facing
- VPC: select My Lab VPC, choose Subnet with name Lab Public Subnet 1 and Lab Public Subnet 2
- Name:
-
Click Next: Configure Security Settings.
-
Click Next: Configure Security Groups.
-
Select Select an existing security group, choose
ALB SG
.
-
Click Next: Configure Routing.
-
Enter the following information, and leave other as default:
- Name:
WebServerTG
- Name:
-
Click Next: Register Targets.
Because there's no EC2 yet, we'll register it later.
-
Click Next:Review.
-
Click Create.
-
Click Close.
CloudFront is a fast content delivery network (CDN), it will securely deliver data, videos, applications, and APIs to customers globally with low latency, high transfer speeds. Cloudfront will also give our Elastic Load Balancer new domain name. The ELB we use in this case is the one we created in the first step.
-
On the Service menu, choose CloudFront.
-
Choose Create Distribution.
-
Create a web distribution, click Get Started.
-
In Create Distribution, enter the following information and leave other as default:
- Origin Domain Name :
WebServerLB
- Object Caching : Customize
- Default TTL :
10
- Origin Domain Name :
- Choose Create Distribution to deploy CloudFront distribution.
It will take 20 ~ 40 minutes, do the following steps beside waiting.
Create a Launch Configuration and Auto-Scaling Group to manage the EC2 which create automatically. We can separate this step into two part:
Launch configuration is an instance configuration template that an Auto Scaling group uses to launch EC2 instances. When you create a launch configuration, you specify information for the instances. Include the ID of the Amazon Machine Image (AMI), the instance type, a key pair, one or more security groups, and a block device mapping. We're telling you how to set up the details about scaling instances.
-
On the service menu, click EC2.
-
In the navigation pane, click Auto Scaling Groups.
-
Click Create Auto Scaling group.
-
Choose Launch Configuration, choose Create a new launch configuration, then click Next Step.
-
In the navigation pane, choose Quick Start, in the row for the second Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type, click Select.
-
Select instance type t2.micro and click Next:Configure details
-
In the Configure details, enter Name:
Auto-Scaling-Launch
-
Specify the following settings:
- Purchasing option: select Request Spot Instance
- Maximum price: 0.05
- User data: copy the following
- IP Address Type: Assign a public IP address to every instance
#!/bin/bash
# Install Apache Web Server and PHP
yum install -y php72 wget httpd24
# Download Lab files
wget https://raw.githubusercontent.com/ecloudvalley/How-to-build-an-elastic-structure/master/index.php
mv index.php /var/www/html/
# Turn on web server
chkconfig httpd on
service httpd start
Because Spot Instance is cheaper than On-Demand instance, so we choose Spot Instance for Auto Scaling instances.
-
Click Next: Add Storage
-
Click Next: Configure Security Group
-
Select Select an existing security group , choose
Web SG
. -
Click Review.
-
Review the details of your launch configuration and click Create launch configuration.
-
Click Proceed without a keypair, select the acknowledgment box, and click Create launch configuration.
Auto Scaling monitors your applications and automatically adjusts capacity to maintain steady, predictable performance at the lowest possible cost. Using AWS Auto Scaling, it’s easy to setup application scaling for multiple resources across multiple services in minutes. Scaling Policy can be customized in several ways.
-
On the Create Auto Scaling Group, enter the following detail:
- Group name:
Auto-Scaling-Group
- Group size: Start with
1
instance - Network: select My Lab VPC
- Subnet: select both Lab Public Subnet 1 and Lab Public Subnet 2
- Group name:
-
Scroll down and expand Advanced Details, and select Receive traffic from one or more load balancers.
-
Click in the Target Groups textbox and then click WebServerTG.
-
Click Next: Configure scaling policies.
-
Click Use scaling policies to adjust the capacity of this group.
-
Modify the Scale between textbox to scale between 1 and 5 instances.
-
Specify the following settings:
- Name: Scale Group Size
- Metric type: Average CPU Utilization
- Target value: 70
-
Click Next: Configure Notifications.
-
Click Next: Configure Tags, enter the following details:
- Key: Name
- Value: AutoScaling Instance
-
Click Review.
-
Review the details of your Auto Scaling group, then click Create Auto Scaling group.
-
Click Close when your Auto Scaling has been created.
The CloudFront created before must successfully deploy now, we can test it to see if it works like expectation.
In the ALB Website, the web page will be changed since refresh the browser. There's no cache in ALB.
-
In the navigation pane, click Load Balancer.
-
Check the Description tag bellow, copy DNS name and paste it to the browser.
Now you can see the Port, Public IP,Instance ID and Time shown on the page.
- Press Refresh to see the difference between the website.
In the Cloudfront Website, cache can be customized. Reducing the duration allows you to serve dynamic content. Increasing the duration means your users get better performance.
-
Click On the Service menu, click CloudFront.
-
Copy the Domain name of your Cloudfront, paste it to the browser.
-
Now you can see the Port, Public IP,Instance ID and Time shown on the page.
-
Press F12 on the keyboard, click Network.
-
Press F5 to refresh the page, click the file under Name
-
View the X-Cache, now you will see Hit from CloudFront
Make sure to clean up the service we just created.
-
Cloudfront, disable and delete
-
On the Service menu, Click Cloudfront.
-
Select the CloudFront with Origin name
WebServerLB
-
Click Disable
Wait until the state become Disabled
- Select the same CloudFront and click Delete on the top
-
-
WebServer Instance
-
Click on Service menu, choose EC2.
-
Right click on WebServer and choose terminate.
-
-
Launch Configuration and Auto Scaling Group
-
In the navigation pane, choose Launch Configurations.
-
Right click on Auto Scaling Launch and choose Delete launch configuration.
-
In the navigation pane, choose Auto Scaling Groups.
-
Right click on Auto-Scaling-Group and choose Delete.
-
-
Load Balancer and Target Group
-
In the navigation pane, choose Load Balancers.
-
Right click on WebServerLB and choose Delete.
-
In the navigation pane, choose Target Groups.
-
Right click on WebServerTG and choose Delete
-
Congratulations! now you have learned:
- Create a Cloudfront
- Create Load Balancer and Target Group
- Create Launch Configuration and Auto Scaling Group
- Trigger Auto Scaling Group with Target Tracking Scaling Policy
To test the Website and Elastic Load Balancer, you can try Bees-with-Machine-Guns