Schedule EC2 Instances With Fog - RareSloth Games
Schedule EC2 Instances

Schedule EC2 Instances With Fog

How to Schedule EC2 Instances to Auto Start and Shutdown

We’re a small startup that wants to keep overhead as low as possible (weird right?) As a part of this, we want to make sure that we aren’t paying for anything we’re not using. This is a quick start guide on how to schedule EC2 instances to startup and shutdown using the fog and whenever gems. You’ll need a machine that can run the resulting cron jobs at the times you desire. If you don’t have a machine that can do this, you can also schedule EC2 instances to startup and shutdown using Auto-Scaling Groups in AWS.

fog

To be able to connect to AWS to issue our start and stop commands, we use the fog gem. There are other ways of issuing commands, but this was the simplest I found. Start by installing the gem.

gem install fog

Now, create a .fog file in the home directory of the user that’s going to be running the scripts.

default:
  aws_access_key_id: YOUR_ACCESS_KEY
  aws_secret_access_key: YOUR_SECRET_ACCESS_KEY
  private_key_path: PATH_TO_PEM

You’ll have to get the keys from your AWS Management Console to be able to issue requests to your instances. The private key path is the path to the same key you use to ssh into your AWS instance. Once that’s complete, create the scripts to start your instance. I put mine in a hidden .aws directory called start_staging_server.rb.

#!/usr/bin/env ruby
require 'fog'

connection = Fog::Compute.new({:provider => 'AWS'})
connection.start_instances('YOUR_SERVER_INSTANCE_ID')

Put in the instance ID for your EC2 instance that you want start, it’ll be something like ‘i-01234abcd’. Create a similar script for stopping your instance (or just use one script and pass in params, whatever you choose):

#!/usr/bin/env ruby
require 'fog'

connection = Fog::Compute.new({:provider => 'AWS'})
connection.stop_instances('YOUR_SERVER_INSTANCE_ID')

Test these scripts out by running them and checking your EC2 console to see if anything is happening. You can also modify the scripts to print out information. Once they’re good and working, we’ll move on to scheduling them.

whenever

I tried to run these scripts by just adding a couple simple entries in my crontab, but it turns out that it doesn’t work quite that simply when it comes to ruby scripts. The whenever gem deals with the boilerplate of ensuring the scripts can be run within the rvm environment. As an added bonus, it makes it simple to declare when you want the scripts to run.

gem install whenever
cd ~/.aws/
mkdir config
whenever .

This creates a default config/schedule.rb file for you to declare your job schedule. Modify config/schedule.rb to run your ruby scripts:

every 1.day, :at => '5:00 am' do
  command '/home/ubuntu/.aws/stop_staging_server.rb'
end

every 1.day, :at => '12:30 pm' do
  command '/home/ubuntu/.aws/start_staging_server.rb'
end

Keep in mind what timezone your machine running the scripts is in. Mine’s in UTC, so these will stop my server every night at midnight EST and start it every morning at 7:30am EST. Finally, the schedule is now set so we tell whenever to write the it to the crontab.

whenever -w

You can see what it actually wrote by using crontab -l. Everything should now be good to go. If you find that your scripts aren’t running correctly, you’ll have to add some output in the scripts and check your cron logs for errors. Let me know if you find this helpful, and happy savings!

3 Comments on “Schedule EC2 Instances With Fog

    • The script is run in a cron job using the whenever gem. If you want to test the start/stop scripts before using whenever, you can just run it like you would any other script: ruby script.rb. These are stored on any server/computer you have that is always on.

Leave a Reply

Your email address will not be published.