DEV Community

Pavol Rajzak
Pavol Rajzak

Posted on

Just do it! With AWS Lambda

The prequel

Last year I had a presentation about AWS Lambda. It's not something I am using in my daily work, but I wanted to explore this technology further and present it to public. When I was preparing code examples for this talk, I was having a problem of coming up with some real-world usage that would go beyond standard "Hello World" or some other peoples' examples. The talk was okay, but it was missing that "Wow!" moment when audience realize that this is the game changer.

Fast forward to present: Our company is finally adopting Slack as our primary communication tool. It's a huge improvement over traditional Skype for Business, mostly because we can create specialized channels. And with that, the most frequently used channel is... the discussion about where to go for a lunch!

The struggle

You see, this is quite a thing, here in Slovakia, since restaurants offer daily lunch menus for a reduced price. Each day different menu for several restaurants around our office. There are many options, so you see why the lunch discussion is an ongoing one.

The first idea (before Slack era) was to create a web application that would scrape information about menus from nearby restaurants. Either from their webpages or from similar services that provide this information. Thus, Luncher was born!

Luncher

And so, with Slack our lunch-flow is:

  1. Create a screenshot from Luncher
  2. Post it to Slack #lunch channel
  3. Create /poll where to go for lunch.

And that's not very Slack-ish (if that's a word).

The idea

Luckily, I designed Luncher to expose a REST API which provides a list of daily menus in JSON format. My first idea was to create a slack command and use this API. Without any prior knowledge, I started to explore how things work.

In Slack, it is fairly easy to create a command. You just create new app within your organization, and then provide a functionality to it. In my case, the intention was to create so called slash command, which starts with forward slash followed by command name and parameters. I called this command /obed (meaning lunch in Slovak).

Implementing obed

The twist

The rules to create slash commands are pretty simple: you use /obed command, it will call REST API to fetch the data and list the results in Slack. Well that's easy, but the problem (here comes the twist) is that the resulting JSON that is going to be read by Slack needs to be in specific format, like this:

{
    "text": "Here's the main text!",
    "attachments": [
        {
            "text":"Here be some additional text!"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

And Luncher sends responses in a very different format.

The easiest thing would be just to add additional endpoint that would send responses in Slack-friendly way, but here's the real problem. Luncher is deployed to Azure as a WAR file (it's Kotlin + Spring Boot) and it's a bit painful to change anything there, since I am not responsible for the deployments.

The solution

I didn't think long when I decided to try implementing a AWS Lambda function that would call Luncher API and transform the output to Slack format. The whole implementation was really easy, since I created and deployed the Lambda function using awesome Serverless framework.

Basically, when /obed is executed it will call an endpoint defined in AWS API Gateway which will point to Lambda function. This function will call the Luncher REST API, retrieve data, transform them in Slack JSON response and return it back. As a result you get this:

Ta-daaa

Final words

The most awesome thing about this is that it was super easy. I managed to do this within one hour or so. For now it is simple, but it can be enhanced so that it accepts parameters and formats output in a more fancy way. Also, now I have a real-world example for my next FaaS related topics.

Top comments (1)

Collapse
 
martyonthefly profile image
Sylvain Marty

Amazing, you give me motivation to try AWS Lambda !
A big thank you from France ! :-*