Serverless Framework project fails to deploy because of archive size
Problem
Bug description Generated bundle of the Serverless project has too big size and exceeds allowed by AWS Lambda 250 MB. It happens because of big sizes of downloaded @prisma/client, which, for some reasons, downloads engines folder that has unzipped size of 96 MB. And the other big files are connected to generated using command `prisma generate` schema. Dependency versions @prisma/client: "^2.20.0" prisma (dev) : "^2.20.1" serverless (dev): "^2.32.1" serverless-webpack: "^5.4.0" webpack: "^5.11.0" @types/webpack: "^4.41.25" Webpack config [code block] Serverless webpack options [code block] Environment & setup - OS: Mac OS Big Sur 11.1 - Database: PostgreSQL - Node.js version: 12 - Prisma version: 2.20.1 Background This is on the first deploy of Prisma to our existing Lambda stack. We have not been able to get Lambda working yet. We bundle our functions through webpack before deploying. I have looked at the webpack bundle and confirm that @prisma/client is being bundled with the lambda function before deploying.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Optimize Prisma Client Bundle Size for AWS Lambda Deployment
The generated bundle of the Serverless project exceeds the AWS Lambda size limit due to the inclusion of large files from the @prisma/client package, specifically the engines folder and the generated schema files. The engines folder contains binaries that are not necessary for the Lambda function to run, leading to an oversized deployment package.
Awaiting Verification
Be the first to verify this fix
- 1
Exclude Engines Folder from Bundle
Modify the webpack configuration to exclude the engines folder from the @prisma/client package. This will significantly reduce the bundle size.
javascriptconst webpack = require('webpack'); module.exports = { // other webpack config externals: { '@prisma/client': 'commonjs @prisma/client', }, plugins: [ new webpack.IgnorePlugin({ resourceRegExp: /\/engines\/ }) ] }; - 2
Use Prisma Client in Production Mode
Ensure that the Prisma Client is generated in production mode to avoid unnecessary files. This can be done by setting the NODE_ENV environment variable to 'production' before running the prisma generate command.
bashNODE_ENV=production npx prisma generate - 3
Optimize Prisma Schema Generation
Review the Prisma schema to ensure that only necessary models and fields are included. Remove any unused models or fields that may contribute to the bundle size.
prisma// Example of removing unused model // model UnusedModel { // id Int @id // name String // } - 4
Use Lambda Layers for Prisma Client
Consider using AWS Lambda Layers to separate the Prisma Client from the main function bundle. This allows for a smaller deployment package and can help manage dependencies more effectively.
bashaws lambda publish-layer-version --layer-name prisma-client --zip-file fileb://path/to/prisma-client.zip - 5
Test Deployment
After making the above changes, deploy the Serverless project again and monitor the deployment process for any errors related to bundle size.
bashserverless deploy
Validation
Confirm that the deployment succeeds without exceeding the AWS Lambda size limit. Check the size of the generated bundle and ensure it is below 250 MB. Additionally, verify that the Prisma Client functions correctly in the deployed environment.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep