I had some misconceptions about how AWS lambda functions worked. I thought they were lightweight things that could only run functions a couple KB in size. I have since learned that this is not really what is going on.
It would probably be useful if the initial tutorial included steps to add a node package to package.json, npm install it, and then explain that the package is getting deployed up to AWS along with the function. That’s the part I initially missed, it did not occur to me that you can send 10MB of stuff up to AWS for a lambda function.
I’ve since looked inside the deployment zip and see all of the node.js packages in there. It would be nice to be able to prune those packages down, one package I looked at had 2MB of testing data in it.
It is possible for Lambda functions to only be a couple of kb but you need to restrict yourself to just code you write plus the AWS-SDK (as a dev dependency). Once you start to use third party NPM’s then you get all of the baggage that comes with them.
Serverless helps you with some of this. It will automatically exclude dev dependencies from the upload package. You can further control what is being uploaded using the exclude/include options in your serverless.yml file. This allows you to exclude additional config files, source files, test files, etc.
If that still isn’t enough then consider something like webpack to reduce the size further through tree shaking but understand that while it might decrease the size of each function it may increase the size of the upload package.
I just removed the npm dependencies from my functions and now my deploy package is 100K for 30 functions. But… when starting off with serverless it would make sense to include some of that npm baggage initially to demonstrate how it can be done. Now that I understand how libraries can be loaded along with the functions, I’ll implement my complex support code as a native library. So get started quick and easy – include a node package – then work on making it smaller.
I also assume that if any function inside my zip gets called it will keep my lambda container hot? Or is AWS putting every lambda function on my account into a single container? How does that work if I have different memory sizes set on my functions?
Each Lambda function is its own container. One function might stay hot while another goes cold. If you want to keep a function hot you need to call that function. The zip file created by Serverless has nothing to do with that. It’s just how the code gets packaged for uploading to AWS.