Building a Serverless Microservice Using Rockset and AWS Lambda
Tags: aws, tutorial, serverless
Published: 2019-08-06 16:55:03

---
title: Building a Serverless Microservice Using Rockset and AWS Lambda
published: true
tags: aws, tutorial, serverless
canonical_url: https://rockset.com/blog/building-a-serverless-microservice-using-rockset-and-aws-lambda
---

*Authored by [Kevin Leong](https://www.linkedin.com/in/kevleong/)*

Rockset is a serverless search and analytics engine that makes it easy to develop data-driven apps. This [video demo](https://www.youtube.com/watch?v=KBb10Ttxjcc) shows an example of what's possible with Rockset. For this exercise, we will build a serverless microservice to discover the stock symbols with the most mentions on Twitter.

[![e59e6e97b5644ea87b8522a8d0295470](https://images.ctfassets.net/1d31s1aajogl/2gd0zziKMkqam0AOOGMiI6/00eee2b5d92385a2bac23ab2c027c0ba/e59e6e97b5644ea87b8522a8d0295470.png)](https://www.youtube.com/watch?v=KBb10Ttxjcc&playsinline=1)

### **Ingest**

Our Twitter stream comes from Amazon Kinesis and is continuously ingested into Rockset. It's a simple process to set up a [live integration between Rockset and Kinesis](https://docs.rockset.com/amazon-kinesis/) from the Rockset console. Refer to our [step-by-step guide](https://rockset.com/blog/live-dashboard-on-streaming-data-using-kinesis/) for more details, including info on setting up the Twitter Kinesis stream.

We also want to combine the stock mentions from Twitter with information about these stocks from [Nasdaq](https://www.nasdaq.com/screening/company-list.aspx). This information [comes from a file in Amazon S3](https://docs.rockset.com/amazon-s3/) and is ingested into a second Rockset collection.

![demo outline](https://images.ctfassets.net/1d31s1aajogl/4pMP9erohiOgUWIiGECge0/133b5a832b286c65d66803b1fb091a7e/demo_outline.png)

### **Query** 

Rockset automatically infers the schema for the Twitter JSON data in the `twitter-firehose` collection. We haven't performed any transformation on the data, but we can immediately run SQL queries on it. Examining the results of our SQL query, note how the Twitter data is organized in multiple levels of [nesting](https://rockset.com/blog/running-sql-on-nested-json/) and arrays.

![Screen Shot 2019-01-04 at 11.22.16 AM](https://images.ctfassets.net/1d31s1aajogl/z5UwbN4ZWKCuuMOCcc8yA/15b16362e674bce41c8706b59d4f9936/Screen_Shot_2019-01-04_at_11.22.16_AM.png)

In our example, we are specifically focused on tweets that contain stock mentions, which we find under the `symbols` arrays in the `entities` field. We gradually explore the data and build out our SQL query, joining tweet data with the Nasdaq company info in the `tickers` collection, to return the most popular stocks in our data set along with some descriptive info about each stock.

{% gist https://gist.github.com/kleong/ae9d852f222987612f52eb464f3a35f3 %}

### **Build** 

Rockset allows you to export your SQL query and embed it as is into your code.

![Screen Shot 2019-01-03 at 2.48.50 PM](https://images.ctfassets.net/1d31s1aajogl/2dyAZORCMkagQoic2s8acy/761e575b390fa5e93d5be0880c9a2d03/Screen_Shot_2019-01-03_at_2.48.50_PM.png)

For our demo, we've built a Python-based serverless API, using AWS Lambda, that returns the stock symbols occurring most often in tweets.

{% gist https://gist.github.com/kleong/8cd66d6e206077c7a7f72b51ddc874ee %}

Once set up, we can serve live queries on raw, real-time Twitter data. In these results, the company Name, Industry, and MarketCap come from the Nasdaq company info.

![Screen Shot 2019-01-03 at 3.37.37 PM](https://images.ctfassets.net/1d31s1aajogl/4QyjTF4vheKeK6a6cgeWac/b361f85f0733156814d2721bb15719b9/Screen_Shot_2019-01-03_at_3.37.37_PM.png)

We can also build a rudimentary app that calls the API and displays the stock symbols with the most mentions on Twitter for customizable time intervals.

![Screen Shot 2019-01-03 at 3.39.27 PM](https://images.ctfassets.net/1d31s1aajogl/1TXmFAlzJiSaOMIi2ks2ms/cad5bbb44d13a6e0ef39032eca6c59de/Screen_Shot_2019-01-03_at_3.39.27_PM.png)

We've provided the code for the **Build** steps—the Python Lambda function and the dashboard—in our [recipes repository](https://github.com/rockset/recipes/tree/master/rockset-aws-lambda), so you can extend or modify this example for your needs.

There's quite a lot going on in this example. We've taken raw JSON and CSV from streaming and static sources, written SQL queries joining the two data sets, used our final SQL query to create a serverless API, and called the API through our app. You can view more detail on how we implemented this serverless microservice in the [video](https://www.youtube.com/watch?v=KBb10Ttxjcc) embedded above. Hopefully this demo will spur your imagination as you consider what you can build on Rockset.