Hi there. I want to find a better way to expose no...
# help
a
Hi there. I want to find a better way to expose node_modules to the API. Currently, I create a layer with all node_modules that is used within my Lambdas. I am using a script for that before the deploy.
function prepare_node_modules_lambda_layer() {
echo "Cleaning up workspace ..."
rm -rf lambda-layers-node_modules
echo "Creating layer ..."
mkdir -p lambda-layers-node_modules/nodejs/node_modules
echo "Prepare server node_modules lambda layer ..."
cp -r ../../node_modules/knex lambda-layers-node_modules/nodejs/node_modules
...
then
layers: [
new lambda.LayerVersion(stack, 'NodeModulesLayer', {
code: lambda.Code.fromAsset('./lambda-layers-node_modules'),
}),
],
The idea was taken from https://dev.to/eddeee888/how-to-deploy-prisma-in-aws-lambda-with-serverless-1m76 But ultimately I don't find it an elegant solution. Any ideas?
s
I think this process is too difficult aswell, would something like a layer definition that would just include all node modules by default (limit is 512MB, so under the hood it could create multiple layers). Optionally only include modules that are needed like below: Something like:
Copy code
new sst.Function(this, '', {
      handlers: 'src/lambda.main',
      layers: [
        {
          layerName: 'knex-layer',
          type: 'nodejs'
          node_modules: [
            'knex'
          ]
        }
      ]
    })
a
Correct me if I am wrong, but there is a limit to creating 5 layers per stack (account?)?
s
Hmm, yeah, so there is a limit of 5 layers attached to a function, and a total unzipped package limit of 250MB (inc. layers)
t
Is there a reason to include all node modules? I've created layers for specific things like Prisma but I prefer bundling to take care of including what's actually needed
s
I think you are right, trusting the bundler to deal with what is needed would make better sense.
Here's some monstrous code building a layer for what Prisma needs
a
I am trying to make Lambda functions as small as possible, that's why I moved node_modules to the Layer. 1) Does it make sense to make them small? 2) At least keeping knex inside function broke my build, there could be similar issues with other libs
t
Yes it definitely makes sense to make them as small as possible to speed up cold starts and the
esbuild
bundler will help with that by only including code that's actually used and minifying everything. Putting things in a layer doesn't necessarily help at runtime because AWS will still have to load the layer and including all node_modules for every function probably results in a larger net size than just relying on bundling
Certain libraries that have native code (knex, prisma) do need to be deployed through a layer but as you can see in my code snippet you should be precise about what you include