Prisma and the Serverless Framework

If you use Prisma with AWS Lambda, you can use the Serverless Framework to help deploy your projects. It provides a CLI that helps with workflow automation and AWS resource provisioning. For an introduction, see our AWS Lambda and Serverless Framework deployment guide.

This page contains additional information about how to use the Serverless Framework to deploy your Prisma projects to AWS Lambda.

Binary targets in schema.prisma

The Prisma schema contains the following in the generator block:

binaryTargets = ["native", "rhel-openssl-1.0.x"]

This is necessary because the local runtime is different from the Lambda runtime. Add the binaryTarget to make the compatible Prisma engine binary available.

Lambda functions with arm64 architectures

Lambda functions that use arm64 architectures (AWS Graviton2 processor) must use an arm64 precompiled binary.

In the generator block of your schema.prisma file, add the following:

schema.prisma
1binaryTargets = ["native", "linux-arm64-openssl-1.0.x"]

Update the Serverless configuration file to package the arm64 binary, as follows:

serverless.yml
1package:
2 patterns:
3 - '!node_modules/.prisma/client/libquery_engine-*'
4 - 'node_modules/.prisma/client/libquery_engine-linux-arm64-*'
5 - '!node_modules/prisma/libquery_engine-*'
6 - '!node_modules/@prisma/engines/**'

Reduce the package footprint

The Serverless configuration file serverless.yml includes a package pattern that includes only the Prisma engine binary relevant to the Lambda runtime, and excludes the others. This means that when Serverless packages your app for upload, it includes only one binary. This ensures the packaged archive is as small as possible.

serverless.yml
1package:
2 patterns:
3 - '!node_modules/.prisma/client/libquery_engine-*'
4 - 'node_modules/.prisma/client/libquery_engine-rhel-*'
5 - '!node_modules/prisma/libquery_engine-*'
6 - '!node_modules/@prisma/engines/**'

However, if you use serverless-webpack, then you cannot use this method. See Deployment with serverless webpack for details.

Deployment with serverless-webpack

If you use serverless-webpack, then you can use the serverless-webpack-prisma plugin without any additional configuration. serverless-webpack-prisma does the following:

  1. Copies your schema.prisma with the Webpack plugin.
  2. Runs the prisma generate command. This means that there is no need to add the command to the webpack options separately.
  3. Packages the correct Prisma engine binary. This can save more than 40mb of capacity.

Install the dependencies as follows:

npm install -D webpack serverless-webpack webpack-node-externals

If your project uses TypeScript, install ts-loader:

npm install -D ts-loader

Create a webpack.config.js file in the project root directory, and paste in the following configuration:

webpack.config.js
1/* eslint-disable @typescript-eslint/no-var-requires */
2const path = require('path')
3const nodeExternals = require('webpack-node-externals')
4const slsw = require('serverless-webpack')
5const { isLocal } = slsw.lib.webpack
6
7module.exports = {
8 target: 'node',
9 stats: 'normal',
10 entry: slsw.lib.entries,
11 externals: [nodeExternals()],
12 mode: isLocal ? 'development' : 'production',
13 optimization: { concatenateModules: false },
14 resolve: { extensions: ['.js'] },
15 output: {
16 libraryTarget: 'commonjs',
17 filename: '[name].js',
18 path: path.resolve(__dirname, '.webpack'),
19 },
20}

If you use TypeScript, insert the following configuration inside exports:

webpack.config.js
1// ...initial config
2module.exports = {
3 // ...initial config
4 resolve: { extensions: ['.js', '.ts'] },
5 module: {
6 rules: [
7 {
8 test: /\.tsx?$/,
9 loader: 'ts-loader',
10 exclude: /node_modules/,
11 },
12 ],
13 },
14 // ...initial config
15}

Refer to the Serverless Webpack documentation for additional configuration.

To install the dev dependency, use the following command:

npm install -D serverless-webpack-prisma serverless

In serverless.yml, add serverless-webpack to the plugins list, as follows:

serverless.yml
1plugins:
2 - serverless-webpack
3 - serverless-webpack-prisma
4
5custom:
6 webpack:
7 includeModules: true

serverless-webpack-prisma automatically runs prisma generate so that you do not need to use the webpack script option separately as shown below.

serverless.yml
1custom:
2 webpack:
3 packagerOptions:
4 scripts:
5 - prisma generate

serverless-webpack-prisma removes the unnecessary dependencies and packages the correct Prisma engine binary. Therefore, delete it in the package.patterns configuration in your serverless.yml file.

serverless.yml
1package:
2 patterns:
3 - '!node_modules/.prisma/client/libquery_engine-*'
4 - 'node_modules/.prisma/client/libquery_engine-rhel-*'
5 - '!node_modules/prisma/libquery_engine-*'
6 - '!node_modules/@prisma/engines/**'

Now redeploy your application. To do so, run serverless deploy. The deployment output shows you that the correct Prisma engine binary is packaged and distributed, as follows:

Serverless: Copy prisma schema for app...
Serverless: Generate prisma client for app...
Serverless: Remove unused prisma engine:
Serverless: - node_modules/.prisma/client/libquery_engine-darwin.dylib.node
Serverless: - node_modules/@prisma/engines/libquery_engine-darwin.dylib.node
Serverless: - node_modules/@prisma/engines/migration-engine-darwin
Serverless: - node_modules/@prisma/engines/prisma-fmt-darwin
Serverless: Copying existing artifacts...
Edit this page on GitHub