Reducing Package Size in Serverless Project Monorepo using Prisma and AWS Lambda, serverless-esbuild

192 views Asked by At

I'm working on a serverless project in a monorepo and encountering an issue related to the package size.

For my prisma to integrate effectively with AWS lambda function, I have to include two key files, 'schema.prisma' and 'libquery_engine-rhel-openssl-1.0.x.so.node'. They need to be located alongside my handler.js function in the build process. I have been able to use esbuild (coupled with the esbuild-plugin-copy) to replicate the two files next to the handler.js function.

The problem I've encountered is that the package size increasingly grows with each function I include in my serverless configuration. This stems from the bundled ZIP file duplicating these files for each individual function.

Here's my esbuild-plugin-copy script (please note that the issue persists even when I set 'once' to true):

const { copy } = require('esbuild-plugin-copy');

module.exports = [
    // The prisma schema has to be incorporated in the 'dist' folder
    copy({
        resolveFrom: 'out',
        copyOnStart: true,
        once: false,
        assets: {
            from: ['../../packages/database/prisma/schema.prisma'],
            to: './',
        },
        watch: false,
    }),
    copy({
        resolveFrom: 'out',
        copyOnStart: true,
        once: false,
        assets: {
            from: ['../../packages/database/node_modules/.prisma/client/libquery_engine-rhel-openssl-1.0.x.so.node'],
            to: './',
        },
        watch: false,
    })
];

In my serverless config, the following is the esbuild setup:

esbuild: {
      bundle: true,
      minify: true,
      sourcemap: false,
      exclude: [],
      target: 'node18',
      define: {
        'require.resolve': undefined as unknown as string,
      },
      platform: 'node',
      concurrency: 10,
      plugins: './scripts/esbuild.plugins.js' as any,
    }

Could anyone guide me on how to copy the needed files (for prisma) only once for each packaged function?

I have tried changing all the esbuild plugin copy options to no avail, I have tried switching over to serverless webpack but had other issues there. I have tried various configurations of package include and excluse as shown here:

    individually: true,
    include: [
      '../../packages/database/node_modules/.prisma/client/libquery_engine-rhel-openssl-1.0.x.so.node',
      '../../packages/database/node_modules/.prisma/client/schema.prisma',
    ],

    include: [
      'handler.js',
      'node_modules/**',
      'packages/database/prisma/schema.prisma',
      'packages/database/node_modules/.prisma/client/libquery_engine-rhel-openssl-1.0.x.so.node',
    ],
    exclude: [
      './**',
    ],
  }, ```
0

There are 0 answers