How we built a Stonks checker API to monitor Wall Street Bets
Tags: aws, devops, serverless, cloud
Published: 2021-05-05 14:49:59

A while ago, a merry bunch on Reddit at the subreddit [r/WallStreetsBets](https://www.reddit.com/r/wallstreetbets/) (WSB) took on [Wall Street](https://en.wikipedia.org/wiki/Wall_Street). Ironically, through an app called [Robinhood](https://robinhood.com/).

> As Alanis Morisette would say, "A little too ironic, don't ya think?"

You had to be in there and in the know at the right time to benefit from the situation. That's why we built a **serverless API** to keep track of all the hot and trending stock chats on WSB, that will notify you when the next GME is about to blow up. In this article, we're showing you step by step how we **deployed, tested, and monitored it**.

Short Squeezing GameStop
------------------------

The idea behind that action was to "**short squeeze**" the stock. The GameStop stock was **expected to fall** so that people would **short-sell it**. *Short selling in a nutshell: borrow a stock, sell it, wait until it falls, and then repurchase it and give it back to the person you borrowed it from*. The **squeezing part** now is where WSB came in. They bought the GameStop stock **in masses**, inflating the price. Now, the people who **short-sold** would have to **buy it back** at a **much higher price** and, in turn, lose money, or at least not make a profit.

In short, lend some stock, sell it for $10 because you hope to repurchase it for $8, but in the end, it rises to $15, and you have a problem.

These things **happen all the time** because the market moves seemingly at random, but if hundreds or even thousands of people **band together** and **buy falling stocks** as WSB did, even seasoned stockbrokers can get into trouble.

**Now, making money on the stock market isn't easy**. You need to **stay up-to-date** to know what to buy and what to sell. **Wouldn't it be nice if you could simply query an API **to find out what the mass of stock traders on Reddit are up to?

Enter the Hot Stock Tracker
---------------------------

The Hot Stock Tracker (HST), is an **API that keeps track of trending stocks on Reddit** and **checks if Elon Musk tweeted about them**. If you call its endpoint, it will deliver **JSON with the latest stock market news** on Reddit.

![Elon Musk on Twitter
](https://lh5.googleusercontent.com/Im8LBkmNf-0qa2uOZL7nl6LqodpfsCDUIPd9b_psQGf9-w6TQ3RALzDFbzDgJISth9h3jFqwIk8vO4Bv3NYZK-mBdQWKkX9pgE8E8JpOIthuxkcJZ93C2RB8HwzlDHYv25QyQp5r)

The HST is located at <https://api.hot-stock-checker.com/great-deals> and can be called with a GET method. It will then return an array of JSON objects that contain the latest stock you should care about.

An example response can be seen here:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8kregcn7j795mzoad8s8.png)

_Find the original code snippets [here](https://dashbird.io/blog/serverless-hot-stock-checker/)_

Installing the Hot Stock Tracker
--------------------------------

If you want to use the HTS, you can** install it locally** or even on your **AWS account**. It's an application based on serverless technology, deployed with [Commandeer](https://commandeerit.com/), an IDE for the cloud.

### Prerequisites

You need [an AWS account](https://portal.aws.amazon.com/billing/signup?type=enterprise#/start) to deploy to it and **monitor the application with [Dashbird](https://dashbird.io/)** later. The application is built with [the Serverless framework](https://www.serverless.com/), which requires [Docker](http://docker.com/get-started) and [Node.js](https://nodejs.org/en/).

### Setup

First, you need to clone the **Commandeer** example repository.

`$ git clone git@github.com:commandeer/open.git commandeer`

You can find the HST inside the `commandeer/sample-apps/robinhood-for-reddit `directory. Inside this directory, you have to install all dependencies with NPM.

`$ npm i`

Next, you need to download and install [the Commandeer desktop app](https://app.getcommandeer.com/download). 

After the installation, you have to **add your AWS account**. For this, you need to click the big plus button at the top left, as you can see below.

![Add new account](https://lh4.googleusercontent.com/Q0HtsC1uIObkRzgh5-k2X9VLZWpHzJG1_1Dbb8bWBTtPfwRo1AVDrCmp2zel1F5RLa9HwmrRZ_vNIw7xiSBomHR12cF1I6DeHAsUfUtpB_aXq4vcO7H0zCnssbHujaCS6OLIE2z0)

Add new account

To add an AWS account, you first need to [create a new IAM user](https://console.aws.amazon.com/iam/home#/users$new?step=details) in the AWS Console. This user needs **programmatic access and admin permissions** because Commandeer will use it to manage all your resources.

![Adding an AWS account](https://lh5.googleusercontent.com/L7BKOTQytG04iVgm20aqZbm8hs8cGWz2Nr2ONKydOPyIf_oWarK5qgb7N7KFD4-3hraELC_LTmkkAAr5q5xDdmphthv8v0zDwvD1EZCn8s5azYPiZ05e6X4EK2H6_cOeUCO4pkjW)

Adding an AWS account

Now that Commandeer is connected with your AWS account, you can **deploy the HST**. It is implemented with the **Serverless framework**, so you need to scroll down in the list of deployment services on the right and select the Serverless/Runner, as shown below.

![Commandeer service list](https://lh6.googleusercontent.com/oDfIqkkQD5yVM_3aSo2bnNNCk96nAwxdmQX_xGNULAljBeSSZfUMzlUoter-kGQSPC2Bb1jozgoZSXxjZ4CkH87Xc78NfLhUvfLjogMGzyebSfExf86MvyJgpXWQX96Q3LuwxPkd)

Commandeer service list

If you click on "**choose file**," you can now select the `serverless.yml` inside the cloned repository; you find it inside the `sample-apps/robinhood-for-reddit` directory.

If the file is loaded and set up your AWS credentials correctly, you can now **deploy the stack** with a click on "**Run**", as seen here:

![Serverless runner](https://lh6.googleusercontent.com/DWVUGxEAiaxf15tYmbK3-AuPuGBFaQgzhI1oY4UoJ6xsw81OugjPImoVlGAwjbXqQLIHXO-3UDWonR-y-In7vsSdZB3OVqTC21btyl1PSV02weYxHgBUjJXTrbiak9rbKZU-nIgl)

Serverless runner

You will now have an **endpoint talking to a Lambda** with **logs being written to CloudWatch Logs**. In the below figure, you can see the **endpoint in the side navigation**.

![Commandeer Navigation](https://lh4.googleusercontent.com/btwKFx4HZYi_Zflo6jrF39jy5Em-FyqSUjH81H3UFnQe93pD1s01sqo9jCGVNt2iOZx2i0Noi01Y3Zy55K_9HQVrnPI0LMzebrs_84Iv-EKC2W3JYmtdrUA11hEVNVDcb70bJ4Fi)

Commandeer Navigation

To visualize the connection, you can see the endpoint connected to the Lambda and CloudWatch log in Figure 6. Once we set up CloudWatch Alarms on this endpoint. You will also see those on the System Diagram page.

![Commandeer System Diagram](https://lh4.googleusercontent.com/IahKxTaQyUOZe0FsLnXT1knXyX9xLOlfOTcdYF3I1NCK2ljlTp3TkV-8bF20ilBJ2tMb0ajCQaqnXDsFF3Rj4by2G7nehdlR_nHYrZpnmemOxLMb788a2lGJOftgGOBzQ1plw7FL)

Commandeer System Diagram

Testing the API
---------------

There are a couple of ways to **test this API endpoint**.

### Testing with Unit tests

If we wanted to use this **API in production**, we would implement **unit tests for it**. This way, the **test can be run inside a CI/CD pipeline** every time we deploy a new release.

### Testing with Commandeer

We can **call AWS resources directly with Commandeer**. This way, we can **deploy**, **test**, and **debug** parts of our application **all in one place**. 

Commandeer offers **two ways to test** our deployment. The first is **Lambda Invoke**, which will, as the name implies, just invoke our Lambda function stand-alone.

![Commandeer Invoke Lambda](https://lh5.googleusercontent.com/4KFVDBpfwr0w5AeXV-JsVZ0xrG_Grh1o3PDkzUptX7G9oHpI2_ZdCA_pILKS_FL6y7e0W0PBzIOdCWTbyUENvnAJiDHaUEZKJd1jVoWikpGUMvpTw8ubkDewZ4sr9oD0G97Fek4M)

Commandeer Invoke Lambda

Below, you can see **running the Lambda Invoke**. Commandeer **displays the payload and the body of the response**, along with the **status codes**. If you scroll down further, you will see the **CloudWatch Logs**. 

![Invoke Lambda results](https://lh3.googleusercontent.com/a_VyFIjodewLxMFkIRpxZGww4tjbHha39xHbBmlp-kUviUk5jVjTANqav3xLxYh817rzCpbKh6LXeeri1i_L5DhvxwvN33IjgWkk5ark0eaDuskh29T_ZnumeUKhuNApZG5fwUQ_)

Invoke Lambda results

![Invoke Lambda CloudWatch Logs](https://lh5.googleusercontent.com/QifPPmHtwzeVw9CpfUyXtZuT4YYN-Pfd0A-Xw9U9DyBfvT3_W0-9hIeZ9ICjS0vBHT0MvR3pDw5sPU8MDKE7BzIdTcnWSjeNBU6howJ-TD67EccwMqjBD8wXgYOzZ5pspNQzevmJ)

Invoke Lambda CloudWatch Logs

The second way is to **test your Lambda function** together with the **connected API Gateway** by calling **API Gateway Invoke** on the `GET /great-deals` endpoint. This is called an integration test, which makes sure these two **services work well together**. The added advantage of this is that you can make the actual call you would use from a **website or app**.

Below, you can see the results of such a test: the **API Gateway Logs** as well as the **CloudWatch Logs**.

![API Gateway Invoke results](https://lh4.googleusercontent.com/SoZuYJEuA23MquK5JyAIcCwrWuLg5ElbX-FscuANtTLQT8GFkmRwagHimVXL8bJ4Ya2OyaghGnOAI6KaBikHGVyliJwJ67JQprC3YEuu_3UIUodN7UtsZvtwS4bHqw6NWLMvDHuk)

API Gateway Invoke results

![API Gateway Invoke Logs](https://lh3.googleusercontent.com/hz5NvLVn-TT2PRbpZPour64cQd3KhcxIpoiFH3kki4GL8ApF2mg7hRSN1WNxb3YhlYgfjYvlNipHMYOp0VZYj7af1T2caE1HT3kbpmVyFXM-eyDxWkei1jJsxWDfhd2U4ZbA8ylD)

API Gateway Invoke Logs

If you want to make sure that the **API works independently **from all the Commandeer things we've done, you can also use a **third-party API **client like [Postman](https://www.postman.com/) to test the endpoint. In the below figure, you see how such a test could look like.

![Postman results](https://lh3.googleusercontent.com/q0ggEB649AkM4g7eaoFOH0M_Ztuqu9RsN4wiPnAAmhIeM894-PFvx7t5Pzjqq_2CbWUPcGJ_vHYpZ1k033-okq5RkU2k8nnKO7NUGJpg284g_xUi4APqT_soDFv5A5238fuu0Pid)

Postman results

Monitoring
----------

Now that you **deployed your API** and **checked that it worked** with a testing method of your choice, you want to make sure that **things stay working**, and if not, **you want to be notified that things have gone bad**.

[Dashbird serverless monitoring](https://dashbird.io/serverless-observability/) is the perfect way to **stay on top of things**. Simply sign up, [deploy the Dashbird CloudFormation stack](https://dashbird.io/docs/quickstart/setting-up-dashbird/), and you're ready to go. **No additional changes** to your Lambda functions or API Gateways are needed.

In the below figure, you see what **Lambda function insights **Dashbird gives you **out of the box**. 

![Dashbird Lambda function monitoring](https://lh4.googleusercontent.com/qdbc5iv7gyJdZfLsq1buqJYWzMk3R9MVxVLb4epLw5dkgCR1t87bPXLos4FllY1BFjFNZvwdSpy8OcR0mnzLloa8QsHCnovLBre_ZRHpVsYQ5zZhSZicz7QrRc7a2lm14lqihBmr)

Dashbird Lambda function monitoring

With just a few clicks, you can **add alerts** for these monitoring data, so you get **notified immediately when things go wrong**.

![Dashbird Alerts](https://lh3.googleusercontent.com/ATLaglVnQ5J7V-6kDI99XqVZs3V4-sTspuyfGszHatd540TiRa2kFlFRzhTYsNujNGOswufdrjZFd1j1o0MMi6QRc_w4l4t3_vrqSA6is2j_h9SRS_ecaSz1jPvUo8stICXGd5qh)

Dashbird Alerts

Below are some example alerts defined specifically for Lambda and API Gateway:

![Dashbird alert for Lambda](https://lh3.googleusercontent.com/5RrmdNvBQANjHTqK1xCu0TDEIy9xHmP_DjX8aMxiRPiBMK0Mm96NVC85JT2dgOSC_8Mz42p7F_wLBf2YGCBNR-v3F0qNqcrj8U2hbMporzJoLogmsCzGoVxiBE-26vDF6fXuepBS)

Dashbird alert for Lambda

![](https://lh4.googleusercontent.com/0nF34dTci4HLgxBEtKiX-09tlP_QQ1y--EiTjn7d5FHA4YoTmrBC_Bc3WNimAUfuMh4mnxrJC-HPLyJDa13vXHA6tsd8sc1cKzgojeKH6KyrtqPnNdakYopf-C6SakKGBfZzavIR)

Dashbird alert for API Gateway

Wrapping up
-----------

Commandeer and Dashbird are the perfect team for** serverless development**.

With [Commandeer](https://commandeerit.com/), you can **deploy and test** all your **serverless cloud infrastructure** with an awesome UI. Everything will be in one place to give you a great overview of your resources.\
[Dashbird](http://dashbird.i/) covers you for **all your serverless monitoring** needs, be it API Gateway, Lambda, or ECS. Everything can be found and looked into **from one place**, and with Dashbird alerts, you can tell Dashbird to **let you know immediately when things are about to break down**.