Deploy to AWS Lambda
Learn how to deploy your Prisma ORM-backed applications to AWS Lambda with AWS SAM, Serverless Framework, or SST
Quick summary
This guide explains how to avoid common issues when deploying a project using Prisma ORM to AWS Lambda.
Questions answered in this page
- How to deploy Prisma to AWS Lambda?
- Which binaryTargets should I configure?
- How to handle connection pooling on Lambda?
While a deployment framework is not required to deploy to AWS Lambda, this guide covers deploying with:
-
AWS Serverless Application Model (SAM) is an open-source framework from AWS that can be used in the creation of serverless applications. AWS SAM includes the AWS SAM CLI, which you can use to build, test, and deploy your application.
-
Serverless Framework provides a CLI that helps with workflow automation and AWS resource provisioning. While Prisma ORM works well with the Serverless Framework "out of the box", there are a few improvements that can be made within your project to ensure a smooth deployment and performance. There is also additional configuration that is needed if you are using the
serverless-webpackorserverless-bundlelibraries. -
SST provides tools that make it easy for developers to define, test, debug, and deploy their applications. Prisma ORM works well with SST but must be configured so that your schema is correctly packaged by SST.
General considerations when deploying to AWS Lambda
This section covers changes you will need to make to your application, regardless of framework. After following these steps, follow the steps for your framework.
Connection pooling
In a Function as a Service (FaaS) environment, each function invocation typically creates a new database connection. Unlike a continuously running Node.js server, these connections aren't maintained between executions. For better performance in serverless environments, implement connection pooling to reuse existing database connections rather than creating new ones for each function call.
You can use Prisma Postgres, which has built-in connection pooling, to solve this issue. For other solutions, see the connection management guide for serverless environments.
Deploying with AWS SAM
Loading environment variables
AWS SAM does not directly support loading values from a .env file. You will have to use one of AWS's services to store and retrieve these parameters. This guide provides a great overview of your options and how to store and retrieve values in Parameters, SSM, Secrets Manager, and more.
Deploying with the Serverless Framework
Loading environment variables via a .env file
Your functions will need the DATABASE_URL environment variable to access the database. The serverless-dotenv-plugin will allow you to use your .env file in your deployments.
First, make sure that the plugin is installed:
npm install -D serverless-dotenv-pluginThen, add serverless-dotenv-plugin to your list of plugins in serverless.yml:
plugins:
- serverless-dotenv-pluginThe environment variables in your .env file will now be automatically loaded on package or deployment.
serverless packageRunning "serverless" from node_modules
DOTENV: Loading environment variables from .env:
- DATABASE_URL
Packaging deployment-example-sls for stage dev (us-east-1)
.Deploying with SST
Working with environment variables
While SST supports .env files, it is not recommended. SST recommends using Config to access these environment variables in a secure way.
The SST guide available here is a step-by-step guide to get started with Config. Assuming you have created a new secret called DATABASE_URL and have bound that secret to your app, you can set up PrismaClient with the following:
import { PrismaClient } from "./generated/client";
import { Config } from "sst/node/config";
import { PrismaPg } from "@prisma/adapter-pg";
const globalForPrisma = global as unknown as { prisma: PrismaClient };
const adapter = new PrismaPg({ connectionString });
export const prisma =
globalForPrisma.prisma ||
new PrismaClient({ adapter });
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
export default prisma;