Configure your Lambda functions to use Webpack for better performance and cost.
Out of the box, AWS Lambda is known to be a cost-effective and approachable alternative to containers due to its short-lived nature, developer experience, and scalability. The Lambda service downloads your code on demand each time a Lambda function is spun up and destroys it away when not in use. It can handle millions of requests per second, is billed by the millisecond, and resuses the function execution environment for requests that come in after the first request that triggers the function environment to be provisioned. One issue with this is that some requests experience a small lag called a cold start when a function environment is first being provisioned and downloading the code. To reduce this cold start time you can reduce the size of your code that is to be downloaded. In order to do this you will need to use a bundler for your source code.
This example will use the Serverless Framework which makes it painless to get started with Lambda functions and has a rich set of plugins one of which is the serverless-webpack plugin.
To get started with the Serverless Framework ensure you have Node.js and npm installed and run:
Next create a project with their template generator:
cd into your project and install the serverless-webpack plugin
Lastly, set the required config files in the root of the project
To run Webpack on the source code and deploy the bundle to AWS Lambda configure the appropriate AWS credentials with the Serverless Framework and deploy: