Sending SNS Notifications to HipChat

Over a year ago, I was working for a company fully adopting the AWS platform, working as part of the DevOps team, I was looking for ways to make everything a little easier and one of the things I had noticed was that we had set up our Auto Scaling Groups Notification emails – via Amazon’s SNS, which provided very useful information as and when we would scale up or down our instances, or had errors from failed random instance terminations at times.  But due to the information being delivered to us as emails, it wasn’t very visible and therefore useful as we had made HipChat our central communication channel.

So as with many other things, I went about investigating SNS to see how I could get that useful data into our HipChat rooms.

Amazon SNS has several different notification types, as I mentioned email. but also it can push notifications to SMS text messages, to its own SQS queues and the one which proved most useful to me, was to post notifications via HTTP(S).

By providing a means to send the payload of notification data to an endpoint of our choosing, I could then craft a service which would be able to present an endpoint, take in the SNS payload and convert the data into human readable text and send it off to HipChat.

So deciding on what to use as the service, I went for Sinatra, as its very lightweight and this service wouldn’t be doing much more than exposing a single endpoint which then enabled messages to get sent to a HipChat room.  As Sinatra’s written in Ruby, It meant I would then also get to leverage the hipchat-api gem which would do the HipChat sending work for me.

So on to the code, I basically wrote the application so that it exposed an endpoint for SNS HTTP like this.

post '/hipchat/:roomid' do
  data = JSON.parse(request.body.read)
  @hipchat_message = JSON.parse(data['Message'])
  
  hipchat_api = HipChat::API.new("#{ENV['HIPCHAT_AUTH_TOKEN']}")
  @status = hipchat_api.rooms_message(params[:roomid], 'Sns Topic', @hipchat_message, notify = 1, color = @message_color, message_format = 'html')
end

That’s the gist of it, feel free to have a look at the real thing.  Nothing special going on really, just parsing the json payload and presenting the information in a more readable format.  I make “bad” things like instance terminations appear as red notifiction messages in hipchat and “good” things like instance creations green – simple.  Alarms raised by SNS are detailed parsed as well.  I automatically confirm subscription to new topics in the code as this has to be done manually to protect against notification abuse I guess, but meant that you had to copy the SubscribeUrl data from the payload of the first message sent after creating a topic and request it from a browser.

Setting up SNS Notifications is easy and well documented by the AWS team so I won’t repeat that here.  You will select Http(s) and then enter the url to your server i.e, http://my.notification.service/hipchat/27463 (assuming 27463 is your room id)

Only other thing to do is to work out your room id in HipChat, but again I’ll assume that’s not an issue for you clever kids.

All of this when put together left us with some great results.

aws_sns_hipchat
Enjoy!

 

Advertisements

3 thoughts on “Sending SNS Notifications to HipChat

  1. Yes this looks fantastic, but I do have one question, in the readme it says to start the service I need to run this command from the terminal RACK_ENV=production ruby aws_sns_to_hipchat.rb -p , but I am using a chef knife plugin to spin up a datapipeline, I just want notifications to be sent on success or failure, how can I start the service from inside my ruby file?
    I want to use the service as part of a method that I am calling when I run a chef knife command to spin up my data pipeline, and I am a little confused about how I would do that

  2. Hi Kevin, thanks for the comments!
    Assuming you’ve checked out the code to see how i auto-subscribe, so onto your second question.
    You can do several things to start the service, such as wrapping that command inside an upstart or init script, and have chef start the service before you spin up your data pipeline. Hope this helps!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s