Using the Amazon Web services is a modern way to have your environment in the Cloud. It supports load balancing between your instances and thanks to Auto Scaling you’ll never need to pay for more than you use.
Basically the Auto Scaling launches additional instances to meet your demand, usually with help of a CPU alarm. Later removes them when not needed any more.
First, you need to build your environment, I would recommend having an NFS (Network File System) server, to host your web-server configuration and your web-root directory. If you need a database, have a look at Amazon RDS (Relationship Database Service), it works great as a MySQL server.
Then you will need to create an Image (Amazon AMIs), install your web-server and put all your scripts so it will automatically add itself to grant access to your NFS and so on.
That will be your Auto Scaling Image, so whenever you have a traffic spike, the Auto Scaling will launch new instances from this image.
You also need to create your Amazon ELB (Elastic Load Balancer), which can be easily done from the AWS web console under the EC2 tab.
Create a launch config
First thing you need it to create a launch config, that will tell what kind of instance will be launched.
# as-create-launch-config MyFirstConfig --image-id ami-01234567 --instance-type m1.large --key MyFirst_PairKey --group MyFirst_Group OK-Created launch config
- –image-id: The ID of the AMIs you created, the image with the web-server
- –instance-type: How many virtual CPU’s and memory for the Auto Scaling images
- –key: Which KeyPair to be used, important if you need SSH access
- –group: The name of the Security Group, if you need special ports open
Create an Auto Scaling group
Next step will be to create your Auto Scaling group, here is where you tell minimum and maximum of instances.
# as-create-auto-scaling-group MyFirstGroup --launch-configuration MyFirstConfig --availability-zones eu-west-1a eu-west-1b --min-size 2 --max-size 20 --load-balancers MyFirstELB OK-Created AutoScalingGroup
- –availability-zones: Tell in which zones your instances will be launched
- –min-size: Minimum of instances
- –max-size: Maximum of instances
Create first policy Scale UP
We need two policies one for scaling up and one for down, in this case I will use CPU usage as alarm and when the average CPU usage is higher then 75% on all active instances it will launch two new instances.
# as-put-scaling-policy MyFirstScaleUpPolicy --auto-scaling-group MyFirstGroup --adjustment=2 --type ChangeInCapacity --cooldown 300 arn:aws:autoscaling:eu-west-1:123456789:scalingPolicy:0123456-789a-bcde-f012-3456789abcdef:autoScalingGroupName/MyFirstGroup:policyName/MyFirstScaleUpPolicy
Use the output string that the policy returned to add in your alarm.
- –adjustment: How many instances to create when CPU usage is higher then 75%
- –type In this case it’s capacity but percentage can also be used “PercentChangeInCapacity”
- –cooldown Time (in seconds) between a successful scaling activity and succeeding scaling activity.
# mon-put-metric-alarm MyFirstHighCPUAlarm --comparison-operator GreaterThanThreshold --evaluation-periods 1 --metric-name CPUUtilization --namespace "AWS/EC2" --period 600 --statistic Average --threshold 75 --alarm-actions arn:aws:autoscaling:eu-west-1:123456789:scalingPolicy:0123456-789a-bcde-f012-3456789abcdef:autoScalingGroupName/MyFirstGroup:policyName/MyFirstScaleUpPolicy --dimensions "AutoScalingGroupName=MyFirstGroup" OK-Created Alarm
- –comparison-operator: Tells which operator to use, in this case GreaterThanThreshold.
- –evaluation-periods: Number of consecutive periods for which the value of the metric needs to be compared to threshold.
- –metric-name: The name of the metric on which to alarm.
- –namespace: A namespace for your alarms AWS/EC2 is default.
- –period: Period of metric on which to alarm.
- –statistic: Here we use average between periods, but others like Minimum or Maximum can be used as well.
- –threshold: The threshold with which the metric value will be compared.
- –alarm-actions: What should be sent, use your output from when you put your metrics alarm in previous step.
- –dimensions: Adds a tag to all instances
Create second policy Scale Down
The down scaling policy needs to be added too. When average CPU usages goes below 20% on all instances, remove 1 instance.
# as-put-scaling-policy MyFirstScaleDownPolicy --auto-scaling-group MyFirstGroup --adjustment=-1 --type ChangeInCapacity --cooldown 300 arn:aws:autoscaling:eu-west-1:123456789:scalingPolicy:0123456-789a-bcde-f012-3456789abcdef:autoScalingGroupName/MyFirstGroup:policyName/MyFirstScaleDownPolicy
# mon-put-metric-alarm MyFirstLowCPUAlarm --comparison-operator LessThanThreshold --evaluation-periods 1 --metric-name CPUUtilization --namespace "AWS/EC2" --period 600 --statistic Average --threshold 20 --alarm-actions arn:aws:autoscaling:eu-west-1:123456789:scalingPolicy:0123456-789a-bcde-f012-3456789abcdef:autoScalingGroupName/MyFirstGroup:policyName/MyFirstScaleDownPolicy --dimensions "AutoScalingGroupName=MyFirstGroup" OK-Created Alarm
Verify your configuration
# as-describe-auto-scaling-groups MyFirstGroup --headers AUTO-SCALING-GROUP GROUP-NAME LAUNCH-CONFIG AVAILABILITY-ZONES LOAD-BALANCERS MIN-SIZE MAX-SIZE DESIRED-CAPACITY AUTO-SCALING-GROUP MyFirstGroup MyFirstConfig eu-west-1a,eu-west-1b MyFirstELB 2 20 2 INSTANCE INSTANCE-ID AVAILABILITY-ZONE STATE STATUS LAUNCH-CONFIG INSTANCE i-01234567 eu-west-1a InService Healthy MyFirstConfig INSTANCE i-01234567 eu-west-1b InService Healthy MyFirstConfig
To remove your config
First you need to remove all instances from the group. Set maximum and minimum to 0. Make the auto scaling do this for you so it removes correctly from the Load Balancer.
# as-update-auto-scaling-group MyFirstGroup --min-size 0 --max-size 0 OK-Updated AutoScalingGroup
Verify in the AWS console that instances are termintated
Next step: Delete both alarms
# mon-delete-alarms MyFirstHighCPUAlarm OK-Deleted alarms # mon-delete-alarms MyFirstLowCPUAlarm OK-Deleted alarms
Then delete Auto scaling group
# as-delete-auto-scaling-group MyFirstGroup OK-Deleted AutoScalingGroup
Last step delete your config
# as-delete-launch-config MyFirstConfig OK-Deleted launch configuration
Setting up a web server
Learn how to configure a web server from home, read more