Minimize the size of lambdas by deleting boto3 from the deployment package

Last April, I wrote a post about managing boto3 in deployed packages. (See Minimizing the size of lambdas by avoiding having boto3 and its stubs in the deployment package). This approach works when all dependencies can be set in the development section.

But, what about when dependencies are required in production? For example, we recently had to deploy a lambda that depends on awswrangler. So the technique suggested in the previous post did not work. Here I will cover a different approach that allows to remove boto3 from the package to be deployed in the example context. It is however less flexible and based on a specific technology: serverless.

The approach is simple: You need to remove the unwanted files, boto3 and botocore directories, before uploading the package to AWS.

Thanks to the rich ecosystem of serverless, because a plugin allows us to perform this task: serverless-scriptable-plugin. This plugin allows to execute arbitrary code at different stages of the deployment process.

You just have to install it (npm install serverless-scriptable-plugin) and then update the serverless configuration. Here is an example.

  - serverless-scriptable-plugin

      - "rm -r .serverless/requirements/boto3 || echo ERR: UNABLE TO REMOVE boto3"
      - "rm -r .serverless/requirements/botocore || echo ERR: UNABLE TO REMOVE botocore"

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: