Take the 2-minute tour ×

Does anyone know of a way or has anyone devised a clever workaround to place a callback/hook into the Amazon APIs (http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/query-apis.html) such that for actions like create instance, one can simply be notified via the callback when the instance is in the running state?

I'm thinking that I could write a loop in node.js that simply checks for the desired state and eventually timesout after a certain # of requests but I would like to hear better programmatic approaches :)

share|improve this question

2 Answers 2

Unless the AWS APIs support some kind of notification endpoint (I'm not very familiar with the APIs) you're probably stuck with polling. However, you could use an EventEmitter to hide this behind a clever API that exposes a callback. Pseudo-ish code:

// aws_server.js

var EventEmitter = require('events').EventEmitter;
var util = require('util');

function AwsServer(some_data) {
  this.data = some_data;
util.inherits(AwsServer, EventEmitter);

AwsServer.prototype.createInstance = function() {
  // Do something with an API to create an EC2 instance
  console.log("Creating instance, data:", this.data);

  // Here, you would begin polling for state changes, etc. waiting for
  // the server to change state. We will simulate this with a setTimeout call.
  setTimeout(function() {
  }.bind(this), 3000);

module.exports = AwsServer;

// somewhere_else.js

var AwsServer = require('./aws_server')

var newServer = new AwsServer('some_data');
newServer.on('running', function() {
  console.log('New instance is running');

share|improve this answer
When experimenting with setTimeout, I realized that it runs only once and with setInterval I couldn't think of a way to stop once the desired 'running' state was verified. So I now have each setTimeout kick off another one until the desired 'running' state is reached, that way the last one to run proceeds with the callback passed in ... is this what you are describing above? With the difference being that an Event is emitted instead of having the callback passed in and run ... right? –  pulkitsinghal Jun 12 '12 at 12:42
Well, the setTimeout in my example was just to simulate some passing of time, and demonstrate emitting and listening to EventEmitter events. However, you're probably on the right track as far as how to poll. I personally prefer a setTimeout that calls another setTimeout instead of a setInterval, in case the operation inside the function takes longer than the timer on the interval. –  Brandon Tilley Jun 12 '12 at 17:01
Hey Brandon, I hope you don't mind if I wait a bit to make sure that noone has a dead-on answer about hooks in AWS API or notification ... before I mark yours as the correct answer? –  pulkitsinghal Jun 13 '12 at 3:12
Of course not! I hope you find what you're looking for. –  Brandon Tilley Jun 13 '12 at 3:51

Your best bet would be to add a shell script on the servers init.d, which will run whenever the server is stopped or started.

Probably useless for this question but other ways of programmatically detecting whether an instance on is by using Amazon's EC2 shell tools:

ec2-describe-instance-status <ec2 instance id>

As described here. Which will return blank if the machine is not running, and data about it if it is.

share|improve this answer
I like the init.d idea of the instance reporting back but where would it report back to, some kind of dynamic url for devops? –  pulkitsinghal Feb 6 '13 at 12:05
Yes, you would need to do the same thing regardless if it came from an official AWS hook or your own server, right? You can have a micro instance up 24/7 for things like this (cost about $15/mo). We also use Pingdom (pay for service) and Nagios (open source) to detect uptime of all machines. –  Mauvis Ledford Feb 6 '13 at 18:57

Your Answer


