Wednesday, May 29, 2013

AWS Auto Scaling Part 2 - Auto Scaling Based on Fixed Number of Instances

In part 1 - AWS Auto Scaling Part 1 - Configuring Auto Scaling Command Line Tools, we have spinned a new ubuntu machine and installed the auto scaling command line tools.

We will create two things
  • a launch configuration (defines what AMI to be launched)
  • an auto scaling group (defines the number of instances to be launched, etc)

Creating a Launch Configuration

We will create a launch configuration.

as-create-launch-config --image-id --instance-type

Choose an AMI of your choice:
as-create-launch-config NodeJS --image-id ami-111111 --instance-type t1.micro
Check the launch configuration
as-describe-launch-configs --headers
Note: In the AWS EC2 Console, you can create an AMI by right-clicking one of your EC2 instances and click Create Image.


Creating an Auto Scaling Group

The auto scaling group takes the following as its parameters:
  • name for the group
  • a launch configuration
  • one or more availability zones
  • a minimum group size
  • a maximum group size

We will create a group called NodeJSGroup. It will launch the NodeJS configuration we created above. We will use us-east-1d as the region and we want to spin 1 instance.
as-create-auto-scaling-group NodeJSGroup --launch-configuration NodeJS --availability-zones us-east-1d --min-size 1 --max-size 1
Check the status of the group by:
as-describe-auto-scaling-groups --headers
Check the health of the auto scaling instances:
as-describe-auto-scaling-instances --headers 
You should see the health of the launched instances. If you don't see any, check the activity log
as-describe-scaling-activities

Deleting launch configurations and auto scaling groups

We will first remove all the instances from the Auto Scaling Group NodeJSGroup. Then we will delete the launch config and the group.

First update the group setting to terminate all the instances.
as-update-auto-scaling-group NodeJSGroup --min-size 0 --max-size 0
Now delete the group and the launch config.
as-delete-auto-scaling-group NodeJSGroup
as-delete-launch-config NodeJS

A more complicated example (with device mapping, security group

The launch configuration above doesn't take into account of the security groups and device-block-mappings. We will create a more complicated example below.

To check the device-block-mappings of an AMI, you will need to install the EC2 API Tools and use the ec2-describe-images command.

ssh connect to the machine you want to use auto-scaling on.

Run either of the following commands.
ec2-describe-images -o self
ec2-describe-images
You will get something like the following

IMAGE ami-17acc4ee 140591131275/nodejs-production-20130522 240591131275 available private x86_64 machine aki-125ea7eb ebs paravirtual xen
BLOCKDEVICEMAPPING EBS /dev/sda1 snap-1f356ee2 8 true standard
BLOCKDEVICEMAPPING EBS /dev/sdf snap-18356ee5 20 false standard
BLOCKDEVICEMAPPING EBS /dev/sdg snap-15356ee8 20 false standard
BLOCKDEVICEMAPPING EPHEMERAL /dev/sdb ephemeral0
For the above, the block-device-mapping will be
block-device-mapping=/dev/sda1=snap-1f356ee2, /dev/sdf=snap-b8356ee5, /dev/sdg=snap-b5356ee8, /dev/sdb=ephemeral0
Find the instance's security group in the AWS EC2 console
--group security_group
You will need to specify a key pair to ssh into this instance as well
--key key_pair
The whole command will be:
as-create-launch-config NodeJS --image-id --instance-type m1.large --block-device-mapping="/dev/sda1=snap- 1f356ee2, /dev/sdf=snap-18356ee5, /dev/sdg=snap-15356ee8, /dev/sdb=ephemeral0" --group security_group  --key key_pair
Now create the auto scaling group.
as-create-auto-scaling-group NodeJSGroup --launch-configuration NodeJS --availability-zones us-east-1d --min-size 1 --max-size 5 --tag "k=Name, v=AsNodeJSProd, p=true"
The tag name will propagate to all the instances. If you don't specify a tag, your instances will have no human readable names. Read more about tags here. k=Name must have Name capitalized, else you won't see the human readable names in your AWS EC2 console.

Check the status by using the following commands
as-describe-launch-configs
as-describe-auto-scaling-groups
as-describe-auto-scaling-instances
You should have one machine launched.

Keep in mind that in some newer instances, the volume names are different. For example,
/xvda1 = /sda1
/xvdb = /sdb
/xvdf = /sdf
/xvdg = /sdg
When using the as-create-launch-config command, use the names returned from as-describe-images even if the actual volumes are different names.

For device-block-mapping, you can specify different kinds of volumes with different sizes. Read Block Device Mapping for more information.

Now let's stop the auto scaling group, else you will need to pay.
as-update-auto-scaling-group NodeJSGroup --min-size 0 --max-size 0
as-delete-auto-scaling-group NodeJSGroup
as-delete-launch-config NodeJS
In the next post, we will go into auto scaling based on metrics like CPU utilizations via the CloudWatch API.

1 comment:

  1. Nice blog! very easy to understand thanks for providing your information AWS Online Training

    ReplyDelete