Introduction to EC2 Auto Scaling – No Scaling Policy
EC2 Auto Scaling allows you to launch any number of EC2 instances to increase the availability of your applications.
This time, as an introduction to EC2 Auto Scaling, we will not set a scaling policy, but will configure the system so that two EC2 instances are always launched.
For more information on simple scaling, please refer to the following page.
For step scaling, please refer to the following page
For target tracking scaling, please refer to the following page
Environment
Create an ALB and attach EC2 Auto Scaling in the private subnet.
Configure Auto Scaling as follows
- Minimum number of instances: 2
- Maximum number: 2
- Desired number: 2
The EC2 instance to be launched within the Auto Scaling group, but it will be the latest version of Amazon Linux 2.
Install Apache from the yum repository on S3 and configure it to act as a web server.
CloudFormation template files
The above configuration is built with CloudFormation.
The CloudFormation templates are located at the following URL
https://github.com/awstut-an-r/awstut-fa/tree/main/089
Explanation of key points of the template files
This page covers the basic Auto Scaling configuration.
For information on how to attach resources in a private subnet to an ALB, please refer to the following page
For information on how to run yum on instances in a private subnet, please refer to the following page
EC2 Auto Scaling
To build EC2 Auto Scaling, create the following two resources
- Launch Template (or Launch Configuration)
- Auto Scaling Group
Launch Template
Resources:
LaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
IamInstanceProfile:
Arn: !GetAtt InstanceProfile.Arn
ImageId: !Ref ImageId
InstanceType: !Ref InstanceType
SecurityGroupIds:
- !Ref InstanceSecurityGroup
UserData: !Base64 |
#!/bin/bash -xe
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
ec2-metadata -i > /var/www/html/index.html
LaunchTemplateName: !Sub "${Prefix}-LaunchTemplate"
Code language: YAML (yaml)
A Launch Template is a resource for configuration information for EC2 instances to be launched within an Auto Scaling Group.
You must create a Launch Template or Launch Configuration to configure EC2 Auto Scaling.
However, it is currently deprecated to configure Auto Scaling using Launch Configuration.
We strongly recommend that you do not use launch configurations. They do not provide full functionality for Amazon EC2 Auto Scaling or Amazon EC2. We provide information about launch configurations for customers who have not yet migrated from launch configurations to launch templates.
Launch configurations
Basically, the configuration items are the same as for EC2 instances.
For example, specify the AMI and instance type of the instance to be launched with the ImageId and InstanceType properties.
User data can be set with the UserData property.
This time we will install and activate Apache, write the instance ID in an HTML file, and set it in the Apache root page.
Auto Scaling Group
Resources:
AutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
AutoScalingGroupName: !Sub "${Prefix}-AutoScalingGroup"
DesiredCapacity: !Ref DesiredCapacity
LaunchTemplate:
LaunchTemplateId: !Ref LaunchTemplate
Version: !GetAtt LaunchTemplate.LatestVersionNumber
MaxSize: !Ref MaxSize
MinSize: !Ref MinSize
VPCZoneIdentifier:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
TargetGroupARNs:
- !Ref ALBTargetGroup
Code language: YAML (yaml)
An Auto Scaling Group is a resource that collects EC2 instances created within EC2 Auto Scaling.
Within the Auto Scaling Group, EC2 instances generated from the Launch Template are created.
Specify the ID and version of the aforementioned Launch Template in the LaunchTemplate property.
The settings are based on the official page.
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-as-group.html
The number of instances to be created in the Auto Scaling Group can be set with the following property.
This time, specify 2 for both properties so that 2 instances will be launched at all times.
- DesiredCapacity: Desired number of instances
- MaxSize:Minimum number of instances
- MinSize:Maximum number
In the TargetGroupARNs property, specify the ARN of the target group of the ALB to be attached.
Architecting
Use CloudFormation to build this environment and check the actual behavior.
Create CloudFormation stacks and check resources in stacks
Create a CloudFormation stacks.
For information on how to create stacks and check each stack, please refer to the following page
After checking the resources in each stack, information on the main resources created this time is as follows
- ALB: fa-089-ALB
- DNS name of ALB: http://fa-089-alb-762314289.ap-northeast-1.elb.amazonaws.com/
- ALB Target Group: fa-089-ALBTargetGroup
- Launch Template: fa-089-LaunchTemplate
- EC2 Auto Scaling Group: fa-089-AutoScalingGroup
Confirm the created resource from the AWS Management Console.
Confirm the ALB.
Confirm the DNS name of the ALB.
Confirm the Launch Template.
You can see that the Launch Template was created as version 1.
Check the Auto Scaling Group.
The desired number, minimum number, and maximum number are all 2. This means that there will always be two instances launched in the Auto Scaling Group.
You can see that the aforementioned Launch Template is applied to the Auto Scaling Group.
Looking at the activity history of the Auto Scaling Group, we can see that two instances were created in the previously empty group.
Checking the instances in the group, we can see that two instances have indeed been launched.
Check Operation
Initial Construction Time
Now that we are ready, we access the ALB.
We were able to access the two instances alternately.
You can see that indeed, an Auto Scaling Group is attached to the ALB, and two instances are created in the group.
Stop Instance Manually
We intentionally stop one instance to check the behavior of Auto Scaling.
One instance is deleted.
Check the activity history of the Auto Scaling Group.
You can see that the instance was deleted and that a new instance was immediately and automatically created.
Check the instances in the Auto Scaling Group.
Two instances are in “InService” status.
This means that the number of instances in the Auto Scaling Group is maintained at two.
Access the ALB again.
We are now able to access the new instances.
In this way, we have seen that EC2 Auto Scaling can be configured by setting up a Launch Template and Auto Scaling Group.
Summary
We have confirmed how to configure the Launch Template and Auto Scaling Group that configure EC2 Auto Scaling.
We have confirmed that it is possible to launch and maintain a specified number of instances at any time by unifying the desired, minimum, and maximum number of Auto Scaling Group.