Setting up Auto Scaling on EC2

This post is specifically about Ubuntu, but the process is similar for other operating systems. You’ll need to have an S3 bucket, and know how to launch EC2 instances.

Here’s a quick overview of the process:

  1. Create a bootstrap script that downloads installation files from S3.
  2. Create an install script that initializes your application code.
  3. Upload your install script and installation files to S3.
  4. Create an IAM role so your instances can download installation files.
  5. Create a Launch Configuration with your bootstrap script as the user data.
  6. Create an Auto Scaling group based on the launch configuration.

 Create a bootstrap script

This is a minimal shell script that will install the AWS cli and nginx.


echo Installing software
apt-get -y install awscli
apt-get -y install nginx

echo Setting directory permissions
chgrp -R ubuntu /etc/nginx
chown -R ubuntu /etc/nginx

echo Copying installation files
aws s3 cp --region us-east-1 s3://your-bucket-here/private/auto-scaling-test/ /tmp/install/ --recursive

echo Running
su - ubuntu /tmp/install/

echo Cleaning up
rm -r /tmp/install

 Create an install script

This install script just starts nginx, but you can also copy configuration files, setup git repositories, and start application code.

echo Starting servers
service nginx start

 Create an IAM Role

Go to the IAM section of the AWS console. Click on Roles > Create New Role, then name the role. Select Amazon EC2, then Amazon S3 Read Only Access.
IAM Role 1.png
IAM Role 2.png

 Create a launch configuration

In the EC2 console, go to Auto Scaling > Launch Configurations.

Launch Configuration.png

 Create an Auto Scaling group

Go to Auto Scaling > Auto Scaling Groups > Create Auto Scaling group. For testing, you can set the minimum number of instances to one. After clicking Create Auto Scaling group, a new instance should start launching.

Once it has initialized, it should have a Public IP so you can SSH in and make sure everything worked correctly. If there was a problem in the installation script, you can update the file in S3, then kill the existing instance. A new one will start up to take its place, automatically using the new install script.

Once your install script is working, you can modify your scaling parameters, and sit back while your servers start themselves!

Auto Scaling.png


Now read this

Using the Cloud Vision API on App Engine

Here’s a quick snippet of how to use the Cloud Vision API on App Engine. It’s pretty straight forward, the only tricky thing is making sure you set the Content-Type. import json from logging import info from urllib import urlencode from... Continue →