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