How we Build and Deploy our Serverless HarperDB Studio Backend Application
Tags: serverless, showdev, aws, database
Published: 2020-09-22 16:00:49

The HarperDB Management Studio is a graphical user interface that enables users to install, design, cluster, and manage databases without having to write a line of code. The Studio’s backend is a serverless application that utilizes [AWS Lambda](https://aws.amazon.com/lambda/) and [Amazon API Gateway](https://aws.amazon.com/api-gateway/). To build, test, debug and deploy this application we use the [AWS Serverless Application Model (SAM)](https://aws.amazon.com/serverless/sam/), not to be confused with the ace HarperDB engineer named [Sam](https://dev.to/samj)! 

SAM is an open-source framework that is used to build serverless applications on AWS. It is an extension of [AWS CloudFormation](https://aws.amazon.com/cloudformation/). With just a few lines per resource, you can define the application you want and model it using [YAML](https://yaml.org/) (a human-readable data-serialization language). During deployment, SAM transforms and expands the SAM syntax into AWS CloudFormation syntax and provisions all the AWS resources we need for the HarperDB Studio.

We decided to go serverless to shift more of our operational responsibilities to AWS. It allows for quick development, seamless scalability and reduced overhead. Our shift to serverless became a lot easier with SAM. SAM offers a single deployment configuration which makes it easy to organize related components and resources, and operate on a single stack. Our application has 30+ Lambdas, all integrated with API Gateway, along with multiple Lambda layers, Step Functions and SNS topics. We can build and deploy our complete application in around ten minutes overall. 

The SAM CLI is an essential tool when working with SAM, it provides an easy way to test and debug your application locally. We use the CLI in conjunction with the AWS toolkit for JetBrains WebStorm, the IDE we predominantly use. It provides a Lambda-like execution environment locally, which helps us test the functions before we deploy them. It also allows us to step through and debug our code to understand what the code is doing, as you would with any other type of project.

I’ll run through a few points that we used to get our SAM project setup with our environment (not all of the following steps are essential).

###Install Requirements###


- Install and setup the SAM CLI

https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html

- Install Docker for local testing 

https://docs.docker.com/get-docker/


- Pull the lambci lambda image for Docker

https://hub.docker.com/r/lambci/lambda/


- Install and setup the WebStorm AWS toolkit

https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/key-tasks.html#key-tasks-install

- Install the AWS CloudFormation plugin

https://plugins.jetbrains.com/plugin/7371-aws-cloudformation


- Install a YAML parser, I’m using this one

https://plugins.jetbrains.com/plugin/7792-yaml-ansible-support


- If you haven’t already, download and run the the sample application

https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started-hello-world.html


###Local Testing With WebStorm###


- Edit the Run/Debug Configurations and click on the Templates tab. With the AWS toolkit install you should see an AWS Lambda template tab. Click on that and then select Local.

- In the template make sure it is pointing to your YAML file, by default it is called template.yaml.  To the right of the template path, select the Function Name you are testing. If the function name doesn’t come up something is likely wrong with your template. Sometimes I need to save this setup, close and reopen for it to appear in the dropdown. The input is what is passed to the Lambda when it is invoked, you must have a value here. 

![](https://paper-attachments.dropbox.com/s_52F25B9E0FFD3E4F18EE5C982CC4B84117F1FBCDE38312B4AE0AFCDBB1F49478_1600277710519_Screen+Shot+2020-09-16+at+11.35.06+AM.png)



- If everything has been setup correctly (which it often isn’t, this process can be convoluted) you should be able to run your Lambda as you would any other project in WebStorm. Have it selected in the Run/Debug Configuration and press run or debug.

###Deploying the Application###

While there are multiple ways to build and deploy the application, this is how we do it at HarperDB. Find out more here - https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-command-reference.html


- Build the application

   ```sam build```

- Package the application. It creates a ZIP file of your code and dependencies, and uploads it to Amazon S3.

    ```sam package --s3-bucket my-s3-bucket --output-template-file packaged.yaml```

- Deploy the application. I suggest adding option -g for guided the first time your deploy your new application.

    ```sam deploy --template-file packaged.yaml --region us-east-2```


Working with SAM can be a little confusing initially, it took me a while to get my head around adding resources to the template file (I found this [document](https://github.com/aws/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction) helpful). However if you stick at it I’m sure you’ll find great value. If you're curious to learn more about other aspects of HarperDB, feel free to check out our [architectural overview](https://harperdb.io/blog/building-a-database-written-in-nodejs/), learn more about the [product](https://harperdb.io/product/), or [spin up a free instance](https://harperdb.io/developers/get-started/)!