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:
- Create a bootstrap script that downloads installation files from S3.
- Create an install script that initializes your application code.
- Upload your install script and installation files to S3.
- Create an IAM role so your instances can download installation files.
- Create a Launch Configuration with your bootstrap script as the user data.
- 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.
#!/bin/sh 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 install.sh su - ubuntu /tmp/install/install.sh 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.
Create a launch configuration
In the EC2 console, go to Auto Scaling > Launch Configurations.
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!