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
Choose an AMI of your choice:
as-create-launch-config NodeJS --image-id ami-111111 --instance-type t1.microCheck the launch configuration
as-describe-launch-configs --headersNote: 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 1Check the status of the group by:
as-describe-auto-scaling-groups --headersCheck the health of the auto scaling instances:
as-describe-auto-scaling-instances --headersYou 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 0Now 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 selfYou will get something like the following
ec2-describe-images
IMAGE ami-17acc4ee 140591131275/nodejs-production-20130522 240591131275 available private x86_64 machine aki-125ea7eb ebs paravirtual xenFor the above, the block-device-mapping will be
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
block-device-mapping=/dev/sda1=snap-1f356ee2, /dev/sdf=snap-b8356ee5, /dev/sdg=snap-b5356ee8, /dev/sdb=ephemeral0Find the instance's security group in the AWS EC2 console
--group security_groupYou will need to specify a key pair to ssh into this instance as well
--key key_pairThe whole command will be:
as-create-launch-config NodeJS --image-idNow create the auto scaling group.--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
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-configsYou should have one machine launched.
as-describe-auto-scaling-groups
as-describe-auto-scaling-instances
Keep in mind that in some newer instances, the volume names are different. For example,
/xvda1 = /sda1When using the as-create-launch-config command, use the names returned from as-describe-images even if the actual volumes are different names.
/xvdb = /sdb
/xvdf = /sdf
/xvdg = /sdg
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 0In the next post, we will go into auto scaling based on metrics like CPU utilizations via the CloudWatch API.
as-delete-auto-scaling-group NodeJSGroup
as-delete-launch-config NodeJS
Nice blog! very easy to understand thanks for providing your information AWS Online Training
ReplyDelete