Save Money with Temporary EC2 Deploy Server

You can save a lot of money on servers that you use infrequently by using AWS EC2 servers.

The obvious paradigm shift with using Amazon Web Services’ Elastic Compute CLoud is that you no longer need to lease servers for an extended period of time. There has been plenty written about spinning up a bunch of servers to handle a large task then shutting them all down, but it can also come in handy for just one server.

There are several types of servers that you don’t need to have running 247: staging, deploy, continuous integration. You only need a staging server when you are evaluating a new build, you only need a deploy server when you are actually deploying, and the continuous integration server only needs to run when someone pushes new code. Let those servers (and your bill) have a rest when you aren’t using them!

At StatHat, we use a deploy server to pull the latest version of the code, build all the various components (most of them in Go), compress javascript and CSS, package it all up, then distribute to the servers. The process is automated and just requires running one command. Because the server only runs for a few hours per month, we are using a much faster server than we would ever pay to run continuously.

We have a script called dssh that finds our deploy server on AWS. If it isn’t running, it starts it up. It takes about 20-30 seconds for it to come up. Then it is able to ssh into it and run the build and deploy scripts.

The build and deploy scripts that run on the server touch the file /tmp/activity whenever they run. There is another script on the deploy server that runs every minute. It checks the last time /tmp/activity was touched. If it’s been more than ten minutes, it shuts the server down. This way if we deploy something and need to deploy a quick fix to something, we don’t have to wait to start the server again as it will stay up for ten minutes.

It’s important for the activity file to be in /tmp so that it will automatically get deleted when you shut down the server.

Here’s a version of the dssh script that will start a server if it isn’t running and ssh into it if it is. You can easily modify it to run whatever command you’d like.

dssh

And this is the script that shuts the server down if there hasn’t been any activity for ten minutes.

stalestop

They can easily be modified for any temporary server you need. A git hook could start up a continuous integration server and tell it to test the latest code. Your staging server could touch /tmp/activity with every http request. When they stop, the server will stop.


Comments? Send us a tweet.

Permalink:

Previous:
How To Track Rails Request Durations
Next:
Easy Statistics For Twitter Users