Reduce Existing Javascript Lambda Package by 49%? Yes please!

Photo by Markus Spiske on Unsplash

I was reading about the new aws-sdk for Javascript version 3 (aws-sdk-js-v3), and part of the advancement is the modularization of the SDK to reduce the resulting deployment package size — and by extension cold-start time.

You can read about the new v3 SDK, including the modular architecture, here.

What if you are working on a project though that is stabilizing for release, or you just aren’t ready to make the jump to v3 yet? That’s where I was, but out of curiosity I read the migration guide and learned that the first step is to convert the existing aws-sdk-js v2 imports away from things like:

into:

I’m already using Webpack and Tree Shaking!

This would supposedly reduce the amount of code imported, even with v2. So maybe I could make this simple change, preparing the code base for v3 later and safely improve performance now? I wasn’t sure how much such a change would help, since the project was already tree-shaking with webpack. I was aware though that webpack doesn’t work well on aws-sdk-js… so maybe?

To try it out, I first added a new rule to tslint.json (I’m using Typescript):

With that, tslint highlights all the places that need to be changed and bans anyone from unwittingly doing a top-level import later and undoing any benefits. Cool. 😎

Because the code base uses hexagonal architecture, tslint only found twelve files that needed to be updated. 👍 I made a few changes such as:

and

A quick deploy and into the AWS Console I go to look in the stack deployment buckets in AWS S3 and compare the new and previous packages. What I found sealed the deal! Here area few of the results:

Overall average reduction in size: 49%

It was an easy change that took mere minutes to forever speed up cold starts.

Yes, bundle your AWS SDK!

One last note, as I’m sure someone will point out that you can reduce sizes even more by using the SDK already within the Lambda execution environment instead of bundling it with your code.

Don’t fall for that trap!

That goes against Best practices for working with AWS Lambda functions, which includes controlling your dependencies. The SDK in the execution environment is there for quick little functions with no other dependencies — often made from within the AWS Console. Relying on this ever-changing version of the SDK in your production environment means that you never know when your system will change from under you and mysteriously break. It isn’t worth it! Bundle your dependencies, but only what you need.

Lead Software Architect and AWS Solutions Architect, specializing in cloud native development as well as web clients. Also, a square dance caller. 🤷‍♂️🕺🏻