Auto Scaling
Developer Guide (API Version 2011-01-01)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Launch Auto Scaling Instances into Amazon VPC

You can create an Auto Scaling group that launches instances into the Amazon Virtual Private Cloud (Amazon VPC).

Amazon VPC is a virtual network you can design that resembles a network running out of a data center. Amazon VPC enables you to create an isolated portion of the Amazon Web Services cloud where you can launch Amazon EC2 instances that have private addresses in the range of your choice. You can create a VPC that spans multiple Availability Zones. You can define subnets within your VPC that you can use to group similar kinds of instances based on IP address range. Each subnet must reside entirely within one Availability Zone. It is possible for you to define multiple subnets within an Availability zone, but Auto Scaling requires you to define one subnet per Availability Zone per Auto Scaling group. For more information on Amazon VPC, go to Introduction to the Amazon Virtual Private Cloud in the Amazon Virtual Private Cloud User Guide.

In this section, we will walk you through the basic steps to get your started with Auto Scaling and Amazon VPC:

  1. Create an Amazon VPC.

  2. Create subnets in your Amazon VPC.

  3. Create a launch configuration.

  4. Create Auto Scaling groups in your Amazon VPC subnets.

  5. Check that instances are launched in the subnets we specified.

  6. Delete your Auto Scaling group.

Tools You Will Use

In this walkthrough exercise, when you create an Amazon VPC and subnets, you'll use Amazon EC2 command line tool commands, and we'll point you to documentation on how to use them. For the Auto Scaling tasks, you'll use the Auto Scaling CLI commands, which we'll discuss in more detail.

Amazon EC2 CLI CommandsDescription

ec2-create-vpc

Creates a VPC with a CIDR block you specify.

ec2-create-subnet

Creates a subnet in an existing VPC.

ec2-describe-subnets

Gives you information about your subnets.

ec2-describe-instances

Returns information about instances that you own.

Auto Scaling CLI CommandsDescription

as-create-launch-config

Creates a new launch configuration with specified attributes.

as-create-auto-scaling-group

Creates a new Auto Scaling group with specified name and other attributes.

as-describe-auto-scaling-groups

Describes the specified Auto Scaling group(s) if the group exists.

as-describe-scaling-activities

Describes a set of activities or all activities belonging to an Auto Scaling group.

as-delete-auto-scaling-group

Delete the specified auto scaling group if the group has no instances and no scaling activities in progress.

For common conventions the documentation uses to represent command symbols, see Document Conventions.

Create Your Amazon Virtual Private Cloud (Amazon VPC)

First, you need to create your Amazon Virtual Private Cloud (Amazon VPC) using the ec2-create-vpc command. For information about ec2-create-vpc, see ec2-create-vpc in the Amazon Elastic Compute Cloud CLI Reference. For information about Amazon VPC, see Introduction to the Amazon Virtual Private Cloud in the Amazon Virtual Private Cloud User Guide.

In the following example you use the ec2-create-vpc command to create a VPC.

ec2-create-vpc "10.0.0.0/24" --region ap-southeast-1

The response includes your VPC ID.

VPC vpc-2e00c747 available 10.0.0.0/24 dopt-2200c74b default

Create Subnets in Your Amazon VPC

You cannot create your subnet until you have created your Amazon VPC. Since you've just created your Amazon VPC, you're ready to use the ec2-create-subnet command to create a subnet; specify the VPC ID from the previous step.

ec2-create-subnet --vpc vpc-2e00c747 -cidr "10.0.0.0/28" --availability-zone ap-southeast-1a --region ap-southeast-1

For information about ec2-create-subnet, see ec2-create-subnet in the Amazon Elastic Compute Cloud CLI Reference.

The subnet will be created in your VPC in the Availability Zone you specified.

SUBNET subnet-530fc83a available vpc-2e00c747 10.0.0.0/28 11 ap-southeast-1a ap-southeast-1

Create a subnet in a different Availability Zone.

ec2-create-subnet --vpc vpc-2e00c747 -cidr "10.0.0.128/28" --availability-zone ap-southeast-1b --region ap-southeast-1

The subnet will be created in your VPC in the Availability Zone you specified.

SUBNET subnet-610acd08 available vpc-2e00c747  10.0.0.128/28 11 ap-southeast-1a

Get a list of subnets in your VPC.

ec2-describe-subnets --region ap-southeast-1

You will get information about the two subnets you just created in your VPC.

SUBNET subnet-610acd08 available vpc-2e00c747  10.0.0.128/28 11 ap-southeast-1b ap-southeast-1
SUBNET subnet-530fc83a available vpc-2e00c747 10.0.0.0/28 11 ap-southeast-1a ap-southeast-1

Create Launch Configuration

After you've created your Amazon VPC and subnets, you define the Auto Scaling scenario. Create a launch configuration, or use an existing launch configuration.

The Auto Scaling launch configuration specifies the template that Auto Scaling uses to launch Amazon EC2 instances. This template contains all the information necessary for Auto Scaling to launch instances that run your application. If you've never created a launch configuration before, go to Create a Launch Configuration.

Specify the following values for the required options:

  • Launch configuration name: myvpclc

  • Instance type: m1.small

  • Image ID: ami-b4b0cae6

    Note

    The AMI ID is provided for illustration purposes only. AMI IDs change over time. You can obtain current, valid AMI IDs by calling the ec2-describe-images command.

In addition, specify the --region general option:

  • Region: ap-southeast-1

    Note

    The AMI or image ID you specify must be supported in the region of your Amazon VPC.

Your as-create-launch-config command will look like this:

as-create-launch-config myvpclc --image-id ami-b4b0cae6 --instance-type m1.small --region ap-southeast-1

You will get a confirmation that your launch configuration was created successfully.

OK-Created launch config

Create Auto Scaling Group

An Auto Scaling group is a collection of Amazon EC2 instances. You can specify settings like the minimum, maximum, and desired number of EC2 instances for an Auto Scaling group to which you want to apply certain scaling actions. For information on how to create an Auto Scaling group, go to Create an Auto Scaling Group.

Specify these values for the following options:

  • Auto Scaling group name: myvpcasgroup

  • Launch configuration name: myvpclc

  • Availability Zone: "ap-southeast-1a,ap-southeast-1b"

  • Minimum size: 0

  • Maximum size: 10

  • Desired capacity: 10

To launch instances in the subnets you created in your Amazon VPC, you also must specify the following zone identifier and region:

  • VPC zone identifier: "subnet-610acd08,subnet-530fc83a"

    Important

    Do NOT use the VPC identifier to specify the VPCZoneIdentifier parameter. Amazon VPC has both a VPC identifier (e.g., vpc-1a2b3c4d) and a subnet identifier (e.g., subnet-9d4a7b6c). You must use the subnet identifier instead of the VPC identifier.

    Note

    The Availability Zone of the subnet must match the Availability Zone you specify. In addition, when you specify several subnets and Availability Zones separated by commas (,) make sure that there is no space between the comma and the next item.

  • Region: ap-southeast-1

Your request will look like this:

as-create-auto-scaling-group myvpcasgroup --launch-configuration myvpclc --availability-zones 
"ap-southeast-1b,ap-southeast-1a" --min-size 0 --max-size 10 --desired-capacity 10 --vpc-zone-identifier 
"subnet-610acd08,subnet-530fc83a" --region ap-southeast-1
    

The response should look something like this.

OK-Created AutoScalingGroup

Confirm that Instances Launched in Subnets

To verify that your Auto Scaling group successfully launched instances in the Availability Zone you specified in your Amazon VPC subnets, you can use the following commands:

  • as-describe-auto-scaling-groups

  • as-describe-scaling-activities

  • ec2-describe-instances

The as-describe-auto-scaling-groups command describes the Auto Scaling group, if it exists. Run this command to confirm that the Auto Scaling group myvpcasgroup was created and is launching instances according to your specifications.

as-describe-auto-scaling-groups myvpcasgroup --region ap-southeast-1 -H

Our example response confirms that the Auto Scaling group myvpcasgroup was created and that instances were launched according to specifications.

AUTO-SCALING-GROUP  GROUP-NAME    LAUNCH-CONFIG  AVAILABILITY-ZONES               MIN-SIZE  MAX-SIZE  DESIRED-CAPACITY
AUTO-SCALING-GROUP  myvpcasgroup  myvpclc        ap-southeast-1b,ap-southeast-1a  0         10        10              
INSTANCE  INSTANCE-ID  AVAILABILITY-ZONE  STATE      STATUS   LAUNCH-CONFIG
INSTANCE  i-e5f4c3b0   ap-southeast-1b    InService  Healthy  myvpclc
INSTANCE  i-e7f4c3b2   ap-southeast-1b    InService  Healthy  myvpclc
INSTANCE  i-eff4c3ba   ap-southeast-1b    InService  Healthy  myvpclc
INSTANCE  i-e9f4c3bc   ap-southeast-1b    InService  Healthy  myvpclc
INSTANCE  i-ebf4c3be   ap-southeast-1b    InService  Healthy  myvpclc
INSTANCE  i-95f4c3c0   ap-southeast-1a    InService  Healthy  myvpclc
INSTANCE  i-97f4c3c2   ap-southeast-1a    InService  Healthy  myvpclc
INSTANCE  i-9df4c3c8   ap-southeast-1a    InService  Healthy  myvpclc
INSTANCE  i-93f4c3c6   ap-southeast-1a    InService  Healthy  myvpclc
INSTANCE  i-91f4c3c4   ap-southeast-1a    InService  Healthy  myvpclc

The as-describe-scaling-activities command lists the scaling activities for the Auto Scaling group. When you run this command, it lists the activities that took place for the myvpcasgroup.

as-describe-scaling-activities --auto-scaling-group myvpcasgroup --region ap-southeast-1 -H

The response shows 10 activities that were successfully run within the timespan of a minute for Auto Scaling group myvpcasgroup.

ACTIVITY  ACTIVITY-ID                           END-TIME              GROUP-NAME    CODE      
ACTIVITY  188b5c5f-79fc-4dcc-95a3-2762eba64702  2011-12-12T22:46:08Z  myvpcasgroup  Successful
ACTIVITY  bc35c15e-cfb1-491c-b4e5-4bc99bf1f68c  2011-12-12T22:46:24Z  myvpcasgroup  Successful
ACTIVITY  b30546fb-b4a7-47fb-bb20-3e8f977bfbdd  2011-12-12T22:46:20Z  myvpcasgroup  Successful
ACTIVITY  18be6da1-7448-4548-97f7-fe1d6a86538a  2011-12-12T22:46:19Z  myvpcasgroup  Successful
ACTIVITY  7952cf76-b3d2-4faf-b725-137d79bb5769  2011-12-12T22:46:21Z  myvpcasgroup  Successful
ACTIVITY  ad501827-5b10-4479-aadf-6b6ef6eb22bc  2011-12-12T22:46:19Z  myvpcasgroup  Successful
ACTIVITY  90cad1e5-c64a-44b9-a619-25440fa411ad  2011-12-12T22:46:08Z  myvpcasgroup  Successful
ACTIVITY  3acc0e73-17fa-4326-9a16-11ec65295456  2011-12-12T22:46:38Z  myvpcasgroup  Successful
ACTIVITY  6865beb5-98da-4377-a684-b727be1cc686  2011-12-12T22:46:38Z  myvpcasgroup  Successful
ACTIVITY  cf072f5a-021b-4c1a-8557-827e19af3a76  2011-12-12T22:46:19Z  myvpcasgroup  Successful

The ec2-describe-instances command describes instances that were launched. In this walkthrough you run the command, specifying the instance ID of one of the 10 instances launched by the myvpcasgroup, with the verbose flag general option (-v or --verbose) to see if the private IP address of the instance is in the subnet subnet-610acd08.

ec2-describe-instances i-e5f4c3b0 --region ap-southeast-1 --verbose

Here is part of the response.

RESERVATION	r-ad623af8	629715795501	
INSTANCE	i-e5f4c3b0	ami-b4b0cae6			running		0		m1.small	2011-12-12T22:45:32+0000	ap-southeast-1b	aki-a4225af6			monitoring-enabled		10.0.0.136	vpc-2e00c747	subnet-610acd08	ebs					paravirtual	xen	cf072f5a-021b-4c1a-8557-827e19af3a76	sg-a19f8ccd	default
BLOCKDEVICE	/dev/sda1	vol-2eeea540	2011-12-12T22:46:08.000Z	
TAG	instance	i-e5f4c3b0	aws:autoscaling:groupName	myvpcasgroup
REQUEST ID	30e8d688-652f-4a1b-b772-ede8e4a4e72c

Delete the Auto Scaling Group

After you're done using your instances and Auto Scaling group, it is good practice to clean up. An efficient way to perform this task is by running the as-delete-auto-scaling-group command with the optional --force-delete parameter. Force delete specifies that instances of the Auto Scaling group will be deleted together with the Auto Scaling group even if the instances are still running. If you don't specify --force-delete parameter when you cannot delete your Auto Scaling group until you've manually stopped and terminated all instances of that Auto Scaling group.

Run the command with the following values:

  • Auto Scaling group name: myvpcasgroup

  • Region (general option): --region ap-southeast-1

  • Force delete (optional parameter): --force-delete

This is how your command will look.

as-delete-auto-scaling-group myvpcasgroup --force-delete --region ap-southeast-1

Confirm that you want to delete the Auto Scaling group and it will be deleted.

Are you sure you want to delete this AutoScalingGroup? [Ny]
OK-Deleted AutoScalingGroup    

Tasks Completed

You just performed the following tasks:

  • Created an Amazon VPC.

  • Created two subnets in the Amazon VPC.

  • Created a launch configuration.

  • Created an Auto Scaling group.

  • Confirmed that your Auto Scaling group exists.

  • Checked that your Auto Scaling group contains running instances in the subnet you created.

  • Deleted your Auto Scaling group.

Following is the complete snippet used to perform these tasks. You can copy the snippet, replace the values with your own, and use the code to get started.

ec2-create-vpc "10.0.0.0/24" --region ap-southeast-1 
ec2-create-subnet --vpc vpc-2e00c747 -cidr "10.0.0.0/28" --availability-zone ap-southeast-1a --region ap-southeast-1
ec2-create-subnet --vpc vpc-2e00c747 -cidr "10.0.0.128/28" --availability-zone ap-southeast-1b --region ap-southeast-1
as-create-launch-config myvpclc --image-id ami-b4b0cae6 --instance-type m1.small --region ap-southeast-1
as-create-auto-scaling-group myvpcasgroup --launch-configuration myvpclc --availability-zones 
"ap-southeast-1b,ap-southeast-1a" --min-size 0 --max-size 10 --desired-capacity 10 --vpc-zone-identifier 
"subnet-610acd08,subnet-530fc83a" --region ap-southeast-1
as-describe-auto-scaling-groups myvpcasgroup --region ap-southeast-1 -H
as-describe-scaling-activities --auto-scaling-group myvpcasgroup --region ap-southeast-1 -H
ec2-describe-instances i-e5f4c3b0 --region ap-southeast-1 --verbose
as-delete-auto-scaling-group myvpcasgroup --force-delete --region ap-southeast-1