Lets take Prisma 2 for a test drive on AWS Lambda with GraphQL 🏎️

Amo Moloko
ITNEXT
Published in
6 min readMay 1, 2020

--

Prisma is an open source database toolkit for Node. It is meant to replace ORMs like TypeORM, Knex.js and others. Prisma is also an alternative to writing raw SQL in your backend API. Initially it was touted around with GraphQL, however, you can use it with REST too. Prisma makes it easier for you to work with your database by creating queries that match the models of your database.

Note Prisma only supports MySQL, PostgreSQL and SQLite at the moment.

I’ve used Prisma 1 before on a project and had an awful time getting it deployed. This time out the gate there is clear documentation for how to get it deployed. So I will spend the rest of this post going over what I experienced while giving Prisma 2 a test drive on AWS Lambda.

A repo is available on GitHub with the final code that is used throughout this post and IMO it is pretty decent for you to get up and running and building something important.

Throughout this post I’m assuming you have used AWS and the Serverless Framework before. 🤰 If not the Serverless Stack tutorial is a great free resource! 🌠

How do go about setting everything up?

Prisma CLI will be what you use to to set up your Prisma project and tell it what database you are using.

While the Prisma Client is what will look into your database and make all the queries for you. In this particular case I am using GraphQL, Prisma generally advocates for a Code-first approach when making your GraphQL schema. Which will require you to use their Nexus library. In this particular project I’m going for a SDL first approach. 🤠

So I first scaffolded a Serverless project with the Serverless GraphQL Node Starter. Then I made a database in MySQL locally on my machine with the following schema:

I used MySQL workbench to create the tables.

Next, I installed the Prisma CLI and got the project initialised. Once that was done, I then added my local database URL to the schema.prismafile:

After this we need to introspect the database so Prisma can generate models for us, with the following command:

$ prisma introspect

Which will then create a data model for you, like below:

Now remember we need to use @prisma/clientto allow us to connect to the data model to be able to run queries against our database. If you run yarn prisma generatein your terminal you should get the following:

With everything setup you should be able to write queries against your database. Since I am using GraphQL, I setup a simple schema:

So In my case I just wrote a mutation to create a F1 Team:

Simple right? If I execute this in Playgroud, after running sls offlineit returns the data from the newly created mutation:

So now everything is working locally, but If you’ve made apps in the cloud long enough you would know that it does not work till it works on the cloud.

The key is to make sure you have the necessary Prisma commands in your package.jsonand have your Webpack configured correctly. Lots of credit needs to go to Salvatore (check out his fullstack project with a similar stack) who ran into a similar issue when deploying to AWS. CloudWatch gives the following error:

To avoid this error make sure these commands are in your package.json:

Now you need to add the copy-webpack-plugin that will give Webpack access to the binary file that is needed once our code is bundled.

Next up make sure you are referencing the prisma generate command in the custom part of your serverless.ymlunder the Webpack section. This is done to make sure your Prisma Client Library is created in node_modules:

Lastly, we just need to update our Webpack config accordingly to tell the plugin where to find the binary file (schema.prisma) to copy it into our build:

Everything should be done on the code side of things 🚀.

Now we need infrastructure for a Serverless RDS Aurora cluster. Make sure you have an AWS account and configured a VPC for your Lambda. You can follow the first part of this post to configure Aurora Serverless on RDS. If you have time you can try and configure it as IaaS by looking at this example repo from the Serverless framework team.

Once all that is done make sure you have your VPC referenced in serverless.yml.Then also insure you have added the connection URL to your .envfile for Prisma to be able to connect to the database in the cloud.

Here is an example .envfile (courtesy of Prisma):

DATABASE_URL="mysql://johndoe:randompassword@localhost:3306/mydb"

And here is what the serverless.ymlwith the VPC endpoints looks like:

Now we can deploy this function:

$ sls deploy

If everything went well your console output should look like this:

We should be able create a mutation after clicking on the provided endpoints:

make sure ‘/dev/’ is inbetween the Lambda endpoint and ‘graphql’

This example does not account for a change in NODE_ENVstages, where you would have different database URLs based on your environments (dev,staging etc). To achieve that I would try the following:

 process.env.database_url = process.env.NODE_ENV = DEVELOPEMENT ? ‘dev_url’ : ‘prod_url’

Cool so why would we want to use this over what is out in the wild currently 🤷‍♂️?

Raw SQL

Personally I do not want to be writing raw SQL again, This what I wrote in a previous project:

This is because Knex and Sequelize did not work on Lambda, which ultimately left me with no choice but to write raw SQL.

The developer experience using Prisma was seamless! After an hour I had something up and running locally, which would make it a strong case when advocating it to your CTO/tech lead.

Prisma Studio

If you run the following in your terminal:

$ prisma studio --experimental

It opens a new window in your browser with this 😯:

Prisma Studio

Prisma Studio allows me to see all the data related to the models that I created in a nice GUI and can do basic CRUD operations. This totally removes the need for installing other 3rd-party tools for these kinds of workflows. 🎉

However, Prisma 2 is still in Beta so you might want to hold back on using it for production workloads. Once it is generally available and with the content from this post you should be able to manufacture a bleeding edge, production ready API on Lambda with GraphQL !💰

Now that you have a bleeding edge developer experience, go out and delight your users!

--

--