Kyle Banks

Building and Deploying Self-Managed Applications with Amazon Web Services

Written by @kylewbanks on Aug 26, 2015.

When you're talking about auto scaling your applications, automating everything is key. That includes updating your source code, installing dependencies, starting your web server, etc. In this post, I'm going to walk you through setting up a Launch Configuration for your Auto Scaling Group that does just that.

Note: This example assumes we're deploying a Node.js application, but the idea is pretty much the same regardless of your platform.

Note 2: It is also assumed that you have at least a decent understanding of Launch Configurations, Auto Scaling Groups, and EC2 in general.

Launch Configuration

First, we're going to need an AMI with Git, Node.js, and npm installed.

Next, create a Launch Configuration with your new AMI, and add the following User Data script:

#!/bin/bash

# 1
sudo su - ec2-user

# 2
export HOME="/home/ec2-user"
export BRANCH="release-1.2.3"
export REPOSITORY_URL="https://github.com/Example/SweetNodeApp"

# Add any other environment variables you'll need
export NODE_ENV="prod"

# 3
git clone -b $BRANCH $REPOSITORY_URL app-dir

# 4
cd $HOME/app-dir
./start.sh

A little explanation of what's going on here:

  1. User Data scripts actually run as root, so we switch to ec2-user before running anything.
  2. Setting some environment variables we'll need both in this script and in the script we're going to be running later. Replace BRANCH and REPOSITORY_URL with the appropriate values.
  3. Next up, clone the correct branch of your Git repository into a new app-dir folder.
  4. Finally, run the script that we're going to be putting in the Git repository. Be sure that the name of this script matches what you create below and has execute permissions.

With our User Data set, and the Launch Configuration created, it's time to add the deployment script.

Deployment Script

Our second and final script is going to do any final setup, such as installing dependencies and running the web server. The benefit to this script, and the reason we don't put everything in User Data, is that User Data cannot be editted. Any time you need to change it, you need to create a new Launch Configuration. This script, since it's in Git, will be automatically refreshed each time an instance is deployed, and thus can be modified as much as you need. Ideally, the User Data script is kept as minimal as absolutely possible and the majority of your setup goes in here. Let's have a look at a basic example:

#!/bin/bash

# Debug Logging
echo "Starting App with"
echo "node.js version:"
node -v
echo "npm version:"
npm -v

# Install dependencies
npm install

# Start the application
npm start

Our simple example actually does very little. First, a little debug logging, followed by installing dependencies, and finally starting the application. There's not a lot to do here, but the beauty is that because this script can be modified at any time, it can really do absolutely anything you need it to.

Let me know if this post was helpful on Twitter @kylewbanks or down below!