Optimizing AWS Lambda: How to Reduce Node.js Application Bundle Size
Tags: serverless, node, javascript, programming
Published: 2023-03-03 18:02:46

## Introduction

AWS Lambda is an incredibly powerful tool for building serverless applications. However, when it comes to building Lambda functions in Node.js, one issue that developers often encounter is the size of their application bundle. While it's relatively easy to write a Lambda function in Node.js, if the size of the application bundle is too large, it can cause several problems.

In this article, we'll explore a strategy for optimizing AWS Lambda functions written in Node.js by reducing the size of the application bundle.

## The problem

When the bundle size is too large, it can cause several problems that can impact the performance and functionality of your Lambda functions. Let's take a closer look at these issues:

### Longer cold start times
When you invoke a Lambda function for the first time, it triggers a cold start, which involves downloading and unzipping the application bundle. If the bundle size is too large, it can significantly increase the time it takes for the function to start executing. This can lead to a poor user experience and reduced performance, especially if your application needs to scale quickly to handle a high volume of requests.

### Upload errors
When you upload a Lambda function to AWS, there's a maximum file size limit of 50 MB. If your application bundle exceeds this limit, you'll encounter an upload error, preventing you from deploying your function.

### Difficulties with the AWS Lambda console editor

The AWS Lambda console editor provides a convenient way to view and edit your Lambda functions. However, if the size of your application bundle is too large, you may encounter difficulties in using the console editor. For example, you may experience slower load times, or the console may not be able to display or edit your code at all.

## The solution

To illustrate how you can reduce the size of your Node.js application bundle for AWS Lambda, I've created a sample project using the Serverless Framework. The project contains two endpoints: light and heavy. The light endpoint returns a simple string, while the heavy endpoint utilizes multiple dependencies in order to increase the size of the application bundle.

You can access the source code for this project on GitHub at the following URL: https://github.com/alanneves/optimizing-aws-lambda. Take a look at the code and follow along with the optimization techniques we'll be exploring in the following sections.



Serverless.yml
![Serverless.yml](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54m56e39mej6vylw30vu.png)

Light.js
![Light.js](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ly48zp8fzp9em6919dcm.png)

Heavy.js
![Heavy.js](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2morc90e9741sluoq3h5.png)

### Current bundle size
Let's start by taking a look at the current bundle size for our project.

To generate the deployment package, we can run the following command:

    serverless package

This command will generate a `.serverless` directory in the root of our project, which contains the deployment packages for our Lambda functions.

As you can see in the screenshot below, it generates only one deployment package, which contains both of our Lambda functions. The size of the deployment package is 4.2MB.

![size-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/svfgppndl5eegginzgts.png)

### Package individually

The first optimization technique we'll explore is the `package.individually` option. This option allows you to generate a separate deployment package for each Lambda function. You need to add the following configuration to your `serverless.yml` file:

    package:
      individually: true

After adding this configuration, we can run the `serverless package` command again.

![size-3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e65s80nfenxhqeq41cbs.png)

This time, it generates two deployment packages, one for each Lambda function. Unfortunately, both of the deployment packages are still 4.2MB in size. This is because the `package.individually` option only generates separate deployment packages, it doesn't optimize the size of the individual packages. But, it's still a good starting point for our optimization efforts.

### Browserify

The next optimization technique we'll explore is Browserify. Browserify is a tool that allows you to bundle your Node.js application into a single file. This is useful for reducing the size of your application bundle because it allows you to remove unused dependencies from your bundle. To install Browserify, run the following command:

    npm install -D browserify serverless-plugin-browserifier

After installing Browserify, we need to add the following configuration to our `serverless.yml` file:

    plugins:
      - serverless-plugin-browserifier

After adding this configuration, we can run the `serverless package` command again.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1ozxaoizvogh63ptnh2.png)

This time, the size of the deployment packages has been reduced to 1 KB and 240 KB. It occurs because the `light` endpoint doesn't use any dependencies, so Browserify is able to remove them from the bundle. The `heavy` endpoint still contains all of its dependencies, but it gets only the necessary code from each dependency, which reduces the size of the bundle.


### Conclusion

In this article, we explored a strategy for optimizing AWS Lambda functions written in Node.js by reducing the size of the application bundle. We started by taking a look at the problems that can occur when the bundle size is too large. Then, we explored two optimization techniques that can be used to reduce the size of the application bundle: `package.individually` and Browserify.