Monday, May 9, 2011

Amazon Auto Scaling

This script is enough for me :)

ZONE="us-east-1d" <=== Availability Zone
<=== Instance Type (micro, small, Large, ...)
LC_IMAGE_ID="ami-xxxxxxxx" <=== Enter your AMI ID here

# Set up load balancer
elb-create-lb $LB_NAME --headers --listener "lb-port=80,instance-port=80,protocol=http" --availability-zones $ZONE --region $REGION

#Setup Load Balancer Health Check
elb-configure-healthcheck $LB_NAME --headers --target "HTTP:80/index.php" --interval 5 --timeout 2 --unhealthy-threshold 2 --healthy-threshold 2 --region $REGION

# Setup Auto Scaling
as-create-launch-config $LC_NAME --image-id $LC_IMAGE_ID --instance-type $INSTANCE_SIZE --group $SECURITY_GROUP --region $REGION

#Setup Auto Scaling Group
as-create-auto-scaling-group $SG_NAME --availability-zones $ZONE --launch-configuration $LC_NAME --min-size 1 --max-size 6 --load-balancers $LB_NAME --health-check-type ELB --grace-period 60 --region $REGION

# Setup Scaling Policies (to add instance)
SCALE_UP_POLICY=`as-put-scaling-policy MyScaleUpPolicy1 --auto-scaling-group $SG_NAME --adjustment=1 --type ChangeInCapacity --cooldown 300 --region $REGION`

#Setup Alarm (Add instance when CPU Load is more then 70%)
mon-put-metric-alarm MyHighCPUAlarm1 --region $REGION --comparison-operator GreaterThanThreshold --evaluation-periods 1 --metric-name CPUUtilization --namespace "AWS/EC2" --period 60 --statistic Average --threshold 70 --alarm-actions $SCALE_UP_POLICY --dimensions "AutoScalingGroupName=$SG_NAME"

# Setup Scaling Policies (to reduce instance)
SCALE_DOWN_POLICY=`as-put-scaling-policy MyScaleDownPolicy1 --auto-scaling-group $SG_NAME --adjustment=-1 --type ChangeInCapacity --cooldown 300 --region $REGION`

#Setup Alarm (Reduce instance when CPU Load is less then 30%)
mon-put-metric-alarm MyLowCPUAlarm1 --region $REGION --comparison-operator LessThanThreshold --evaluation-periods 1 --metric-name CPUUtilization --namespace "AWS/EC2" --period 60 --statistic Average --threshold 30 --alarm-actions $SCALE_DOWN_POLICY --dimensions "AutoScalingGroupName=$SG_NAME"


Anonymous said...

great stuff, but you forgot the --region flag on most calls (also --show-xml was very helpful on as-create-auto-scaling-group)

grosser said...

I made all this into a nice and clean repo, with proper setup instructions :)

AWS Autoscaling tools

Tehrani said...

Thanks grosser ;)

Dr.Phosphorus said...

Hey Thanks Grosser!
One thing I dont get is, if the load balancer decides to create a new instance does it mean a clone is created complete with all the application logic and databases (LAMP stack?). And when I use your script I get a message saying Shutting Down... and new instance is created. But the load balancer must just add new instances instead of removing unhealthy instances right?



Anonymous said...

After a couple hours of searching, you hit the nail on the head! And so simply.

Much thanks!