Getting started with Amazon EC2 Auto Scaling - Amazon EC2 Auto Scaling

Getting 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.

The following instructions:

  • Create a configuration template that defines your EC2 instances. You can choose either the launch template or the launch configuration instructions, based on your preference.

  • Create an Auto Scaling group to maintain a fixed number of instances even if an instance becomes unhealthy.

  • Optionally, delete this basic infrastructure.

This tutorial 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.

To get started, you can launch a single free tier eligible Linux instance. 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 options that are 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.

Step 1: Create a launch template

For this step, you 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

Alternatively, you can use a launch configuration to create an Auto Scaling group instead of using a launch template. For the launch configuration instructions, see Create a launch configuration.

To create a launch template

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. On the navigation bar at the top of the screen, select an AWS Region. The Amazon EC2 Auto Scaling resources that you create are tied to the Region that you specify.

  3. On the navigation pane, under INSTANCES, choose Launch Templates.

  4. Choose Create launch template.

  5. Enter a name (for example, my_template) and provide a description for the initial version of the launch template.

  6. For Amazon machine image (AMI), choose a version of Amazon Linux 2 (HVM) from the Quick Start list. The Amazon Machine Image (AMI) serves as a basic configuration template for your instances.

  7. For Instance type, choose a hardware configuration that is compatible with the AMI that you specified. Note that the free tier Linux server is a t2.micro 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.

  8. (Optional) For Key pair name, 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.

  9. Leave Networking platform set to VPC.

  10. For 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.

  11. You can leave Network interfaces 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 specify a network interface, the security group must be a part of it.

  12. Choose Create launch template.

  13. 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

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. On the navigation bar, select an AWS Region. The Auto Scaling resources that you create are tied to the Region that you specify.

  3. On the navigation pane, under AUTO SCALING, choose Launch Configurations.

  4. Choose Create launch configuration, and enter a name for your launch configuration (for example, my-first-lc).

  5. 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:

    1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

    2. In the navigation pane, under Instances, choose Instances, and then choose Launch instances.

    3. 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."

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. Choose Create launch configuration.

  11. Select the check box next to the name of your new launch configuration and choose Actions, Create Auto Scaling group.

Step 2: Create an Auto Scaling group

An Auto Scaling group is a collection of EC2 instances, and is the core of Amazon EC2 Auto Scaling. When you create an Auto Scaling group, you include information such as the subnets for the instances and the initial number of instances to start with.

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 (console)

  1. On the Choose launch template or configuration page, for Auto Scaling group name, enter a name for your Auto Scaling group.

  2. Choose Next.

    The Configure settings page appears, allowing you to configure network settings and giving you options for launching On-Demand and Spot Instances across multiple instance types (if you chose a launch template).

  3. [Launch template only] Keep Purchase options and instance types set to Adhere to the launch template to quickly create and configure an Auto Scaling group.

  4. Keep Network 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.

  5. For Subnet, choose a subnet from each Availability Zone that you want to include. Use subnets in multiple Availability Zones for high availability.

  6. 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 is 1 instance.

  7. 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.

To verify that your Auto Scaling group has launched an EC2 instance

  1. On the Auto Scaling groups page, select the check box next to the Auto Scaling group that you just created.

    A split pane opens up in the bottom part of the Auto Scaling groups page, showing information about the group. The first tab available is the Details tab, showing information about the Auto Scaling group.

  2. 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 shows PreInService. 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.

  3. On the Instance management tab, under Instances, you can view the status of the instance.

  4. 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 the Pending 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.

(Optional) Terminate an instance in your Auto Scaling group

You can 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.

  1. On the Instance management tab, under Instances, select the ID of the instance.

    This takes you to the Instances page in the Amazon EC2 console, where you can terminate the instance.

  2. Choose Actions, Instance State, Terminate. When prompted for confirmation, choose Yes, Terminate.

  3. 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.

  4. On the Instance management tab, the Instances section shows the new instance only.

  5. On the navigation pane, under INSTANCES, choose Instances. This page shows both the terminated instance and the new running instance.

Step 4: 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:

Step 5: (Optional) Delete your scaling infrastructure

You can either delete your scaling infrastructure or delete just your Auto Scaling group and keep your launch template or configuration to use later.

If you launched an instance that is not within the AWS Free Tier, you should terminate your instances to prevent additional charges. When you terminate the instance, the data associated with it will also be deleted.

To delete your Auto Scaling group

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. On the navigation pane, under AUTO SCALING, choose Auto Scaling Groups.

  3. Select the check box next to your Auto Scaling group.

  4. Choose Delete. When prompted for confirmation, 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 show 0 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

  1. On the navigation pane, under INSTANCES, choose Launch Templates.

  2. Select your launch template.

  3. Choose Actions, Delete template. When prompted for confirmation, choose Delete launch template.

Skip the following procedure if you would like to keep your launch configuration.

To delete your launch configuration

  1. On the navigation pane, under AUTO SCALING, choose Launch Configurations.

  2. Select your launch configuration.

  3. Choose Actions, Delete launch configuration. When prompted for confirmation, choose Yes, Delete.