Tip for staying under the 250mb Lambda limit

While using containers does solve the limit problem, it does remove a critical feature: SnapStart.

We have made SO many package mods etc to stay under the 250mb limit of the years, but we hit a wall recently. Our lambdas are HEAVY (pandas + numpy) … and not feasible to change anytime soon.

We were able to reduce the package size by about 5MB by just removing whitespace, doctrings and comments. While these are helpful for humans working on the code, it is not needed for the code to run in the Lambda*

We also removed whitespace and comments from the installed packages and their header files. The doc strings had to be left alone, see the * below.

So if you are in the same boat, this might help you out.

* unless the code makes use of the __doc__ method, then removing these can be breaking.

You might want to look into lambda layers https://docs.aws.amazon.com/lambda/latest/dg/chapter-layers.html

Oh boy, if you are on that level after already cleaning up the dependencies, good luck is all i can say…

To my knowledge layers count towards the package limit

Maybe rewrite it in rust?

Oops, yep you are right https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html

Container image?

golang?

your lambda is doing too much.

i think /tmp is also saved, but confirm this is the case.

if so, you can dynamically download content from s3 in the init phase. the only writeable location is /tmp, but it is fine, because you can simply add the directory to sys.path.

Have you considered a Lambda isn’t the right solution…?

What the heck is your lambda doing?!

Nearly all of ours are under 2mb and we’re processing billions of data per day.

Lambdas are meant to be small, if yours aren’t then stop “building around” lambdas and just make a proper backend, you don’t need to go full django mode but maybe a fastapi is enough.

You are making your code worst for nothing.

/tmp is there if the runtime is reused. If you have two launch at the same time temp will be empty on both. If the lambda is unused for I think 10 minutes you’ll lose tmp. It’s not something to rely upon, reuse of the micro vms is managed by the lambda service, they could destroy it sooner in some cases.

Hitting the limit generally means your lambda is doing too much.

I’ve also found that if the task requires pandas and other heavy data science libraries it’s usually not a task suited for lambda, and is probably best served by some kind of batch etl job.

First of all, it sounds like your Lambda is doing a lot more than it probably should be. I’d recommend either moving to a container-based Lambda if you truly need >25 MB (and using Provisioned Concurrency to keep instances warm), or restructuring the workflow with something like Step Functions so the work is split across multiple Lambdas in one execution.

That said, if you want to stick with a ZIP deployment, another option is to store large dependencies or assets in S3 and download them into the /tmp directory during initialization before your handler runs. That gives you roughly an additional 500 MB of usable local storage while still keeping a ZIP-based deployment.

we are talking about snapstart here.

I haven’t used this word in forever, but your system of scouring your dependencies for whitespace and comments is very kludgey.

I would treat this only as a temporary workaround until you can rearchitect to use something other than lambda, which doesn’t seem to be a good fit for your needs.

Ah yeah, fair enough, yeah probably right if done in init.

This is what I was going to suggest.

Data science is a heavily batch oriented process.

Lambda is simply the wrong tool for the job.

OP. If you’re having to use a tool incorrectly to make it kind of work, consider its the wrong tool.