How to post to HipChat from AWS Lambda

29 Jan 2017

At first, I investigated using a bot to post to HipChat (GitHub’s Hubot) but it seemed a little overkill. It had many features that I didn’t need, and had to be run on a server 24/7. I did run it using Heroku so it didn’t cost anything, but it was a bit too clunky.

So instead, I started looking for other ways to achieve this, Amazon Web Services’ Lambda looked like the best option. The tagline Amazon use for the service is:

Run code without thinking about servers.

and that’s exactly what I wanted! Lambda lets you run Python code, and the best part is, the first million requests you make each month are free.

So, how do you setup Lambda to post to HipChat?

Firstly, login to hipchat.com, select the Rooms tab, and find the room you would like to post in. Now, make a note of the API ID as you will need this later. Then click on Tokens in the left menu.

Now, enter a name for your Token, make sure you have selected Send Notifications and click Create.

You will now see your API token, this will allow you to post to the room. Make a note of it.

Next, login to AWS (Create an account here if you don’t already have one). Navigate to the Lambda service. Click Create a Lambda function and choose the Blank Function blueprint.

You now have the option to choose a Trigger for the function. I wanted to post to HipChat once, per day at 9AM for example, so I selected CloudWatch Events - Schedule.

Enter a name, desciption and a schedule expression. You can find the syntax for scheduling here.

Make sure to tick the Enable box and click next.

Now we need to configure the function. Enter a name, description and choose Python 2.7 as the runtime.

Next, copy and paste the code from below into the code section.

Change enter_token_here and enter_room_id_here to the values you saved earlier. Note: you can also change ‘color’: ‘purple’ and ‘from’: ‘Lambda’ to change how your message will appear in HipChat.

message = “Write your message here” - Enter the message you would like to post inside the speech marks.

Now, set the Handler to the functionName.post so in my case, HipChat-Post.post

Select Choose an existing role then select the role called lambda_basic_execution.

Leave all other options as default, and click Next at the bottom of the page. Then scroll down and click Create function.

Congratulations, you have created your first Lambda function!

Select Test at the top of the page, it will ask you for input, but we don’t need any, so leave the options as default and click Save and Test.

You should now see ‘Success’ output to the execution results, and your message in HipChat!

Please enable JavaScript to view the comments powered by Disqus.