Deploy to AWS Lambda
This guide explains how to avoid common issues when deploying a project using Prisma ORM to AWS 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-webpack
orserverless-bundle
libraries. - 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.
Define binary targets in Prisma Schema
Depending on the version of Node.js, your Prisma schema should contain either rhel-openssl-1.0.x
or rhel-openssl-3.0.x
in the generator
block:
- Node.js 16 and 18
- Node.js 20+
binaryTargets = ["native", "rhel-openssl-1.0.x"]
binaryTargets = ["native", "rhel-openssl-3.0.x"]
This is necessary because the runtimes used in development and deployment differ. Add the binaryTarget
to make the compatible Prisma ORM engine file available.
Lambda functions with arm64 architectures
Lambda functions that use arm64 architectures (AWS Graviton2 processor) must use an arm64
precompiled engine file.
In the generator
block of your schema.prisma
file, add the following:
binaryTargets = ["native", "linux-arm64-openssl-1.0.x"]
Prisma CLI binary targets
While we do not recommend running migrations within AWS Lambda, some applications will require it. In these cases, you can use the PRISMA_CLI_BINARY_TARGETS environment variable to make sure that Prisma CLI commands, including prisma migrate
, have access to the correct schema engine.
In the case of AWS lambda, you will have to add the following environment variable:
PRISMA_CLI_BINARY_TARGETS=native,rhel-openssl-1.0.x
prisma migrate
is a command in the prisma
package. Normally, this package is installed as a dev dependency. Depending on your setup, you may need to install this package as a dependency instead so that it is included in the bundle or archive that is uploaded to Lambda and executed.
Connection pooling
Generally, when you use a Function as a Service (FaaS) environment to interact with a database, every function invocation can result in a new connection to the database. This is not a problem with a constantly running Node.js server. Therefore, it is beneficial to pool database connections to get better performance. You can use Accelerate 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.