Get started with Amazon EC2 Auto Scaling – Amazon EC2 Auto Scaling
Mục Lục
Get started with Amazon EC2 Auto Scaling
When you use Amazon EC2 Auto Scaling, you must use certain building blocks to get started. This tutorial
walks you through the process for setting up building blocks to create a basic
infrastructure for Amazon EC2 Auto Scaling.
Before you create an Auto Scaling group for use with your application, review your application
thoroughly as it runs in the AWS Cloud. Consider the following:
-
How many Availability Zones the Auto Scaling group should span.
-
What existing resources can be used, such as security groups or Amazon Machine
Images (AMIs). -
Whether you want to scale to increase or decrease capacity, or if you just want to
ensure that a specific number of servers are always running. Keep in mind that
Amazon EC2 Auto Scaling can do both simultaneously. -
What metrics have the most relevance to your application’s performance.
-
How long it takes to launch and configure a server.
The better you understand your application, the more effective you can make your Auto Scaling
architecture.
Note
For an introduction video, see AWS re:Invent 2018: Capacity
Management Made Easy with Amazon EC2 Auto Scaling on
YouTube.
Walkthrough summary
In this walkthrough, you:
-
Create a configuration template that defines your EC2 instances. You can
choose either the launch template or the launch configuration instructions.
Although you can use a launch configuration, we recommend a launch template so
that you can use the latest features of Amazon EC2 and Amazon EC2 Auto Scaling. -
Create an Auto Scaling group with a single instance in it.
-
Terminate the instance and verify that the instance was removed from service
and replaced. To maintain a constant number of instances, Amazon EC2 Auto Scaling detects and
responds to Amazon EC2 health and reachability checks automatically.
If you created your AWS account less than 12 months ago, and
have not already exceeded the free tier
benefits for Amazon EC2, it will not cost you anything to complete this tutorial, because we
help you select an instance type that is within the free tier benefits. Otherwise, when
you follow this tutorial, you incur the standard Amazon EC2 usage fees from the time that the
instance launches until you delete the Auto Scaling group (which is the final task of this
tutorial) and the instance status changes to terminated
.
Prepare for the walkthrough
This walkthrough assumes that you are familiar with launching EC2 instances and that
you have already created a key pair and a security group. For more information, see
Setting up with
Amazon EC2 in the Amazon EC2 User Guide for Linux Instances.
If you are new to Amazon EC2 Auto Scaling and want to get started using the service, you can use the
default VPC for your AWS account. The default VPC includes a
default public subnet in each Availability Zone and an internet gateway that is attached
to your VPC. You can view your VPCs on the Your VPCs page of the Amazon Virtual Private Cloud
(Amazon VPC) console.
Step 1: Create a launch template
In this step, you sign in to the Amazon EC2 console with your AWS account credentials and
create a launch template that specifies the type of EC2 instance that Amazon EC2 Auto Scaling creates
for you. Include information such as the ID of the Amazon Machine Image (AMI) to use,
the instance type, the key pair, and security groups.
Note
To use a launch configuration instead, see Create a launch configuration.
To create a launch template
-
Open the Launch templates
page of the Amazon EC2 console. -
On the navigation bar at the top of the screen, select an AWS Region. The
launch template and Auto Scaling group that you create are tied to the Region that you
specify. -
Choose Create launch template.
-
For Launch template name, enter
my-template-for-auto-scaling
. -
Under Auto Scaling guidance, select the check box.
-
For Application and OS Images (Amazon Machine Image),
choose a version of Amazon Linux 2 (HVM) from the Quick
Start list. The AMI serves as a basic configuration template for
your instances. -
For Instance type, choose a hardware configuration that
is compatible with the AMI that you specified.Note
If your account is less than 12 months old, you can use a
t2.micro
instance for free within certain usage limits. For
more information, see AWS free
tier. -
(Optional) For Key pair (login), choose an existing key
pair. You use key pairs to connect to an Amazon EC2 instance with SSH. Connecting to
an instance is not included as part of this tutorial. Therefore, you don’t need
to specify a key pair unless you intend to connect to your instance using SSH. -
For Network settings, Security
groups, choose a security group in the same VPC that you plan to
use as the VPC for your Auto Scaling group. If you don’t specify a security group, your
instance is automatically associated with the default security group for the
VPC. -
You can leave Advanced network configuration empty.
Leaving the setting empty creates a primary network interface with IP addresses
that we select for your instance based on the subnet to which the network
interface is established. If instead you choose to configure a network
interface, the security group must be a part of it. -
Choose Create launch template.
-
On the confirmation page, choose Create Auto Scaling
group.
If you are not
currently using launch templates and prefer not to create one now, you can create a
launch configuration instead.
A launch configuration is similar to a launch template, in that it specifies the type
of EC2 instance that Amazon EC2 Auto Scaling creates for you. You create the launch configuration by
including information such as the ID of the Amazon Machine Image (AMI) to use, the
instance type, the key pair, and security groups.
To create a launch configuration
-
Open the Launch configurations
page of the Amazon EC2 console. -
On the navigation bar, select an AWS Region. The launch configuration and
Auto Scaling group that you create are tied to the Region that you specify. -
Choose Create launch configuration, and then enter
my-first-launch-configuration
in the
Name field. -
For Amazon machine image (AMI), choose an AMI. To find a
specific AMI, you can find a
suitable AMI, make note of its ID, and enter the ID as search
criteria.To get the ID of the Amazon Linux 2 AMI:
-
Open the Amazon EC2
console. -
In the navigation pane, under Instances, choose
Instances, and then choose Launch
instances. -
On the Quick Start tab of the Choose an
Amazon Machine Image page, note the ID of the AMI next to
Amazon Linux 2 AMI (HVM). Notice that this AMI is marked “Free tier
eligible.”
-
-
For Instance type, select a hardware configuration for
your instance.Note
If your account is less than 12 months old, you can use a
t2.micro
instance for free within certain usage limits. For
more information, see AWS free
tier. -
Under Additional configuration, for Advanced
details, IP address type, make a selection.
To provide internet connectivity to instances in a VPC, choose an option that
assigns a public IP address. If an instance is launched into a default VPC, the
default is to assign a public IP address. If you want to provide internet
connectivity to your instance but aren’t sure whether you have a default VPC,
choose Assign a public IP address to every instance. -
For Security groups, choose an existing security group.
If you leave the Create a new security group option
selected, a default SSH rule is configured for Amazon EC2 instances running Linux. A
default RDP rule is configured for Amazon EC2 instances running Windows. -
For Key pair (login), choose an option under
Key pair options as instructed. Connecting to an
instance is not included as part of this tutorial. Therefore, you can select
Proceed without a key pair unless you intend to connect
to your instance using SSH. -
Choose Create launch configuration.
-
Select the check box next to the name of your new launch configuration and
choose Actions, Create Auto Scaling
group.
Step 2: Create a single-instance Auto Scaling group
Now use Amazon EC2 Auto Scaling to create an Auto Scaling group and add the launch template or launch
configuration to the group. Also include information such as the VPC subnets for the
instances.
Use the following procedure to continue where you left off after creating either a
launch template or a launch configuration.
To create an Auto Scaling group
-
On the Choose launch template or configuration page, for
Auto Scaling group name, enter
my-first-asg
. -
Choose Next.
The Choose instance launch options page appears, allowing
you to choose the VPC network settings you want the Auto Scaling group to use and giving
you options for launching On-Demand and Spot Instances (if you chose a launch
template). -
In the Network section, keep VPC set
to the default VPC for your chosen AWS Region, or select your own VPC. The
default VPC is automatically configured to provide internet connectivity to your
instance. This VPC includes a public subnet in each Availability Zone in the
Region. -
For Availability Zones and subnets, choose a subnet from
each Availability Zone that you want to include. Use subnets in multiple
Availability Zones for high availability. For more information, see Considerations when choosing VPC
subnets. -
[Launch template only] In the Instance type requirements
section, use the default setting to simplify this step. (Do not override the
launch template.) For this tutorial, you will launch only one On-Demand Instance
using the instance type specified in your launch template. -
Keep the rest of the defaults for this tutorial and choose Skip to
review.Note
The initial size of the group is determined by its desired capacity. The
default value is1
instance. -
On the Review page, review the information for the group,
and then choose Create Auto Scaling group.
Step 3: Verify your Auto Scaling group
Now that you have created an Auto Scaling group, you are ready to verify that the group has
launched an EC2 instance.
Tip
In the following procedure, you look at the Activity history
and Instances sections for the Auto Scaling group. In both, the named
columns should already be displayed. To display hidden columns or change the number
of rows shown, choose the gear icon on the top right corner of each section to open
the preferences modal, update the settings as needed, and choose
Confirm.
To verify that your Auto Scaling group has launched an EC2 instance
-
Open the Auto Scaling groups
page of the Amazon EC2 console. -
Select the check box next to the Auto Scaling group that you just created.
A split pane opens up in the bottom of the Auto Scaling
groups page. The first tab available is the
Details tab, showing information about the Auto Scaling
group. -
Choose the second tab, Activity. Under Activity
history, you can view the progress of activities that are
associated with the Auto Scaling group. The Status column shows the
current status of your instance. While your instance is launching, the status
column showsPreInService
. The status changes to
Successful
after the instance is launched. You can also use the
refresh button to see the current status of your instance. -
On the Instance management tab, under
Instances, you can view the status of the
instance. -
Verify that your instance launched successfully. It takes a short time for an
instance to launch.-
The Lifecycle column shows the state of your
instance. Initially, your instance is in thePending
state.
After an instance is ready to receive traffic, its state is
InService
. -
The Health status column shows the result of the
EC2 instance health check on your instance.
-
Step 4: Terminate an instance in your Auto Scaling
group
Use these steps to learn more about how Amazon EC2 Auto Scaling works, specifically, how it launches
new instances when necessary. The minimum size for the Auto Scaling group that you created in
this tutorial is one instance. Therefore, if you terminate that running instance,
Amazon EC2 Auto Scaling must launch a new instance to replace it.
-
Open the Auto Scaling groups
page of the Amazon EC2 console. -
Select the check box next to your Auto Scaling group.
-
On the Instance management tab, under
Instances, select the ID of the instance.This takes you to the Instances page of the Amazon EC2
console, where you can terminate the instance. -
Choose Actions, Instance State,
Terminate. When prompted for confirmation, choose
Yes, Terminate. -
On the navigation pane, under Auto Scaling, choose
Auto Scaling Groups. Select your Auto Scaling group and choose the
Activity tab.The default cooldown for the Auto Scaling group is 300 seconds (5 minutes), so it
takes about 5 minutes until you see the scaling activity. In the activity
history, when the scaling activity starts, you see an entry for the termination
of the first instance and an entry for the launch of a new instance. -
On the Instance management tab, the
Instances section shows the new instance only. -
On the navigation pane, under Instances, choose
Instances. This page shows both the terminated instance
and the new running instance.
Step 5: Next steps
Go to the next step if you would like to delete the basic infrastructure for automatic
scaling that you just created. Otherwise, you can use this infrastructure as your base
and try one or more of the following:
You can also start familiarizing yourself with auto scaling concepts by reading about
Target tracking scaling
policies. If the load on your application
changes, your Auto Scaling group can scale out (add instances) and scale in (run fewer
instances) automatically by adjusting the desired capacity of the group between the
minimum and maximum capacity limits. For more information about setting these limits,
see Set capacity limits on your Auto Scaling group.
If you plan to attach a load balancer to your Auto Scaling group, you can learn how to create
one quickly using the Amazon EC2 Auto Scaling console. For more information, see Configure an Application Load Balancer or Network Load Balancer from the
Amazon EC2 Auto Scaling console.
Step 6: Clean up
You can either delete your scaling infrastructure or delete just your Auto Scaling group and
keep your launch template or launch configuration to use later.
If you launched an instance that is not within the AWS Free Tier, you should terminate your
instance to prevent additional charges. When you terminate the instance, the data
associated with it will also be deleted.
To delete your Auto Scaling group
-
Open the Auto Scaling groups
page of the Amazon EC2 console. -
Select the check box next to your Auto Scaling group
(my-first-asg
). -
Choose Delete.
-
When prompted for confirmation, type
delete
to confirm
deleting the specified Auto Scaling group and then choose
Delete.A loading icon in the Name column indicates that the Auto Scaling
group is being deleted. When the deletion has occurred, the
Desired, Min, and
Max columns show0
instances for the Auto Scaling
group. It takes a few minutes to terminate the instance and delete the group.
Refresh the list to see the current state.
Skip the following procedure if you would like to keep your launch template.
To delete your launch template
-
Open the Launch templates
page of the Amazon EC2 console. -
Select your launch template
(my-template-for-auto-scaling
). -
Choose Actions, Delete
template. -
When prompted for confirmation, type
Delete
to confirm
deleting the specified launch template and then choose
Delete.
Skip the following procedure if you would like to keep your launch
configuration.
To delete your launch configuration
-
Open the Launch configurations
page of the Amazon EC2 console. -
Select your launch configuration
(my-first-launch-configuration
). -
Choose Actions, Delete launch
configuration. -
When prompted for confirmation, choose Delete.