# Deploy to AWS Lambda (/docs/orm/v6/prisma-client/deployment/serverless/deploy-to-aws-lambda)

Location: ORM > v6 > Prisma Client > Deployment > Serverless > Deploy to AWS Lambda

> [!NOTE]
> Quick summary
> 
> This guide explains how to avoid common issues when deploying a project using Prisma ORM to [AWS Lambda](https://aws.amazon.com/lambda/).

<details>
  <summary>
    Questions answered in this page
  </summary>

  * How to deploy Prisma to AWS Lambda?
  * Which binaryTargets should I configure?
  * How to handle connection pooling on Lambda?
</details>

While a deployment framework is not required to deploy to AWS Lambda, this guide covers deploying with:

* [AWS Serverless Application Model (SAM)](https://aws.amazon.com/serverless/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](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-reference.html#serverless-sam-cli), which you can use to build, test, and deploy your application.
* [Serverless Framework](https://www.serverless.com/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`](https://www.npmjs.com/package/serverless-webpack) or [`serverless-bundle`](https://www.npmjs.com/package/serverless-bundle) libraries.
* [SST](https://sst.dev/) 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.

> [!NOTE]
> Use Prisma ORM without Rust binaries
> 
> If Prisma ORM's Rust engine binaries cause large bundle sizes, slow builds, or deployment issues (for example, in serverless or edge environments), you can use it without them using this configuration of your `generator` block:
> 
> ```prisma
> generator client {
>   provider   = "prisma-client-js" // or "prisma-client"
>   engineType = "client"
> }
> ```
> 
> Prisma ORM without Rust binaries has been [Generally Available](/orm/v6/more/releases#generally-available-ga) since [v6.16.0](https://pris.ly/release/6.16.0).
> 
> Note that you need to use a [driver adapter](/orm/v6/overview/databases/database-drivers#driver-adapters) in this case.
> 
> When using this architecture:
> 
> * No Rust query engine binary is downloaded or shipped.
> * The database connection pool is maintained by the native JS database driver you install (e.g., `@prisma/adapter-pg` for PostgreSQL).
> 
> This setup can simplify deployments in serverless or edge runtimes. Learn more in the [docs here](/orm/v6/prisma-client/setup-and-configuration/no-rust-engine).
> 
> Curious why we moved away from the Rust engine? Take a look at why we transitioned from Rust binary engines to an all-TypeScript approach for a faster, lighter Prisma ORM in this [blog post](https://www.prisma.io/blog/prisma-orm-without-rust-latest-performance-benchmarks).

General considerations when deploying to AWS Lambda [#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.

* [Deploying with AWS SAM](#deploying-with-aws-sam)
* [Deploying with the Serverless Framework](#deploying-with-the-serverless-framework)
* [Deploying with SST](#deploying-with-sst)

Define binary targets in Prisma Schema [#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

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

#### Node.js 20+

```prisma
binaryTargets = ["native", "rhel-openssl-3.0.x"]
```

This is necessary because the runtimes used in development and deployment differ. Add the [`binaryTarget`](/orm/v6/reference/prisma-schema-reference#binarytargets-options) to make the compatible Prisma ORM engine file available.

Lambda functions with arm64 architectures [#lambda-functions-with-arm64-architectures]

Lambda functions that use [arm64 architectures (AWS Graviton2 processor)](https://docs.aws.amazon.com/lambda/latest/dg/foundation-arch.html#foundation-arch-adv) must use an `arm64` precompiled engine file.

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

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

Prisma CLI binary targets [#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](/orm/v6/reference/environment-variables-reference#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:

```bash title=".env"
PRISMA_CLI_BINARY_TARGETS=native,rhel-openssl-1.0.x
```

> [!NOTE]
> `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 [#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 [Accelerate](/accelerate) for connection pooling or [Prisma Postgres](/postgres), which has built-in connection pooling, to solve this issue. For other solutions, see the [connection management guide for serverless environments](/orm/v6/prisma-client/setup-and-configuration/databases-connections#serverless-environments-faas).

Deploying with AWS SAM [#deploying-with-aws-sam]

Loading environment variables [#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](https://medium.com/bip-xtech/a-practical-guide-to-surviving-aws-sam-d8ab141b3d25) provides a great overview of your options and how to store and retrieve values in Parameters, SSM, Secrets Manager, and more.

Loading required files [#loading-required-files]

AWS SAM uses [esbuild](https://esbuild.github.io/) to bundle your TypeScript code. However, the full esbuild API is not exposed and esbuild plugins are not supported. This leads to problems when using Prisma ORM in your application as certain files (like `schema.prisma`) must be available at runtime.

To get around this, you need to directly reference the needed files in your code to bundle them correctly. In your application, you could add the following lines to your application where Prisma ORM is instantiated.

```ts title="app.ts" showLineNumbers
import schema from "./prisma/schema.prisma";
import x from "./node_modules/.prisma/client/libquery_engine-rhel-openssl-1.0.x.so.node";

if (process.env.NODE_ENV !== "production") {
  console.debug(schema, x);
}
```

Deploying with the Serverless Framework [#deploying-with-the-serverless-framework]

Loading environment variables via a .env file [#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

```bash
npm install -D serverless-dotenv-plugin
```

#### pnpm

```bash
pnpm add -D serverless-dotenv-plugin
```

#### yarn

```bash
yarn add --dev serverless-dotenv-plugin
```

#### bun

```bash
bun add --dev serverless-dotenv-plugin
```

Then, add `serverless-dotenv-plugin` to your list of plugins in `serverless.yml`:

```text title="serverless.yml" no-copy
plugins:
  - serverless-dotenv-plugin
```

The environment variables in your `.env` file will now be automatically loaded on package or deployment.

```bash
serverless package
```

```bash no-copy
Running "serverless" from node_modules
DOTENV: Loading environment variables from .env:
         - DATABASE_URL

Packaging deployment-example-sls for stage dev (us-east-1)
.
.
.
```

Deploy only the required files [#deploy-only-the-required-files]

To reduce your deployment footprint, you can update your deployment process to only upload the files your application needs. The Serverless configuration file, `serverless.yml`, below shows a `package` pattern that includes only the Prisma ORM engine file relevant to the Lambda runtime and excludes the others. This means that when Serverless Framework packages your app for upload, it includes only one engine file. This ensures the packaged archive is as small as possible.

```text title="serverless.yml" no-copy
package:
  patterns:
    - '!node_modules/.prisma/client/libquery_engine-*'
    - 'node_modules/.prisma/client/libquery_engine-rhel-*'
    - '!node_modules/prisma/libquery_engine-*'
    - '!node_modules/@prisma/engines/**'
    - '!node_modules/.cache/prisma/**'  # only required for Windows
```

If you are deploying to [Lambda functions with ARM64 architecture](#lambda-functions-with-arm64-architectures) you should update the Serverless configuration file to package the `arm64` engine file, as follows:

```text title="serverless.yml"
package:
  patterns:
    - '!node_modules/.prisma/client/libquery_engine-*'
    - 'node_modules/.prisma/client/libquery_engine-linux-arm64-*'
    - '!node_modules/prisma/libquery_engine-*'
    - '!node_modules/@prisma/engines/**'
```

If you use `serverless-webpack`, see [Deployment with serverless webpack](#deployment-with-serverless-webpack) below.

Deployment with serverless-webpack [#deployment-with-serverless-webpack]

If you use `serverless-webpack`, you will need additional configuration so that your `schema.prisma` is properly bundled. You will need to:

1. Copy your `schema.prisma` with [`copy-webpack-plugin`](https://www.npmjs.com/package/copy-webpack-plugin).
2. Run `prisma generate` via `custom > webpack > packagerOptions > scripts` in your `serverless.yml`.
3. Only package the correct Prisma ORM engine file to save more than 40mb of capacity.

1. Install webpack specific dependencies [#1-install-webpack-specific-dependencies]

First, ensure the following webpack dependencies are installed:

  

#### npm

```bash
npm install --save-dev webpack webpack-node-externals copy-webpack-plugin serverless-webpack
```

#### pnpm

```bash
pnpm add --save-dev webpack webpack-node-externals copy-webpack-plugin serverless-webpack
```

#### yarn

```bash
yarn add --dev webpack webpack-node-externals copy-webpack-plugin serverless-webpack
```

#### bun

```bash
bun add --dev webpack webpack-node-externals copy-webpack-plugin serverless-webpack
```

2. Update webpack.config.js [#2-update-webpackconfigjs]

In your `webpack.config.js`, make sure that you set `externals` to `nodeExternals()` like the following:

```javascript title="webpack.config.js" highlight=1,5;normal; showLineNumbers
const nodeExternals = require("webpack-node-externals");

module.exports = {
  // ... other configuration
  externals: [nodeExternals()], // [!code highlight]
  // ... other configuration
};
```

Update the `plugins` property in your `webpack.config.js` file to include the `copy-webpack-plugin`:

```javascript title="webpack.config.js" highlight=2,7-13;normal; showLineNumbers
const nodeExternals = require("webpack-node-externals");
const CopyPlugin = require("copy-webpack-plugin"); // [!code highlight]

module.exports = {
  // ... other configuration
  externals: [nodeExternals()],
  plugins: [
    // [!code highlight]
    new CopyPlugin({
      // [!code highlight]
      patterns: [
        // [!code highlight]
        { from: "./node_modules/.prisma/client/schema.prisma", to: "./" }, // you may need to change `to` here. // [!code highlight]
      ], // [!code highlight]
    }), // [!code highlight]
  ], // [!code highlight]
  // ... other configuration
};
```

This plugin will allow you to copy your `schema.prisma` file into your bundled code. Prisma ORM requires that your `schema.prisma` be present in order make sure that queries are encoded and decoded according to your schema. In most cases, bundlers will not include this file by default and will cause your application to fail to run.

> [!NOTE]
> Depending on how your application is bundled, you may need to copy the schema to a location other than `./`. Use the `serverless package` command to package your code locally so you can review where your schema should be put.

Refer to the [Serverless Webpack documentation](https://www.serverless.com/plugins/serverless-webpack) for additional configuration.

3. Update serverless.yml [#3-update-serverlessyml]

In your `serverless.yml` file, make sure that the `custom > webpack` block has `prisma generate` under `packagerOptions > scripts` as follows:

```yaml title="serverless.yml" showLineNumbers
custom:
  webpack:
    packagerOptions:
      scripts:
        - prisma generate
```

This will ensure that, after webpack bundles your code, the Prisma Client is generated according to your schema. Without this step, your app will fail to run.

Lastly, you will want to exclude [Prisma ORM query engines](/orm/v6/more/internals/engines) that do not match the AWS Lambda runtime. Update your `serverless.yml` by adding the following script that makes sure only the required query engine, `rhel-openssl-1.0.x`, is included in the final packaged archive.

```yaml title="serverless.yml" highlight=6;add showLineNumbers
custom:
  webpack:
    packagerOptions:
      scripts:
        - prisma generate
        -- find . -name "libquery_engine-*" -not -name "libquery_engine-rhel-openssl-*" | xargs rm # [!code ++]
```

If you are deploying to [Lambda functions with ARM64 architecture](#lambda-functions-with-arm64-architectures) you should update the `find` command to the following:

```yaml title="serverless.yml" highlight=6;add showLineNumbers
custom:
  webpack:
    packagerOptions:
      scripts:
        - prisma generate
        -- find . -name "libquery_engine-*" -not -name "libquery_engine-arm64-openssl-*" | xargs rm # [!code ++]
```

4. Wrapping up [#4-wrapping-up]

You can now re-package and re-deploy your application. To do so, run `serverless deploy`. Webpack output will show the schema being moved with `copy-webpack-plugin`:

```bash
serverless package
```

```bash no-copy
Running "serverless" from node_modules
DOTENV: Loading environment variables from .env:
         - DATABASE_URL

Packaging deployment-example-sls for stage dev (us-east-1)

asset handlers/posts.js 713 bytes [emitted] [minimized] (name: handlers/posts)
  asset schema.prisma 293 bytes [emitted] [from: node_modules/.prisma/client/schema.prisma] [copied]
  ./handlers/posts.ts 745 bytes [built] [code generated]
  external "@prisma/client" 42 bytes [built] [code generated]
  webpack 5.88.2 compiled successfully in 685 ms
Package lock found - Using locked versions
Packing external modules: @prisma/client@^5.1.1

✔ Service packaged (5s)
```

Deploying with SST [#deploying-with-sst]

Working with environment variables [#working-with-environment-variables]

While SST supports `.env` files, [it is not recommended](https://v2.sst.dev/config#should-i-use-configsecret-or-env-for-secrets). SST recommends using `Config` to access these environment variables in a secure way.

The SST guide [available here](https://v2.sst.dev/config#overview) 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](https://v2.sst.dev/config#bind-the-config), you can set up `PrismaClient` with the following:

```ts title="prisma.ts" showLineNumbers
import { PrismaClient } from "./generated/client";
import { Config } from "sst/node/config";

const globalForPrisma = global as unknown as { prisma: PrismaClient };

export const prisma =
  globalForPrisma.prisma ||
  new PrismaClient({
    datasourceUrl: Config.DATABASE_URL,
  });

if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;

export default prisma;
```

## Related pages

- [`Deploy to Azure Functions`](https://www.prisma.io/docs/orm/v6/prisma-client/deployment/serverless/deploy-to-azure-functions): Learn how to deploy a Prisma Client based REST API to Azure Functions and connect to an Azure SQL database
- [`Deploy to Netlify`](https://www.prisma.io/docs/orm/v6/prisma-client/deployment/serverless/deploy-to-netlify): Learn how to deploy Node.js and TypeScript applications that are using Prisma Client to Netlify.
- [`Deploy to Vercel`](https://www.prisma.io/docs/orm/v6/prisma-client/deployment/serverless/deploy-to-vercel): Learn how to deploy a Next.js application based on Prisma Client to Vercel.