AWS Lambda : Zip or Docker image ?

Meidi Airouche - Jul 25 - - Dev Community

A common decision I had to make those past years was to make a choice between zip files or docker images as code deliverables for AWS Lambda functions. I'll share with you some hints about AWS Lambda that could help you taking the right decision in the right context.

AWS Lambda : reminder about how it works

Lambda functions are stateless compute services that execute code. It can be invoked, croned, react to events... To upload your code into those lambda functions, there are many ways to do it. Two common ways are :

  • Push a zip file with code and dependencies
  • Pull a docker image with code and dependencies

Many organizations have now integrated Docker images as their goto delivery format. Moreover, most of them have built their CI/CD around this format and want to keep the same for delivering AWS Lambdas.

So, is it a good idea ?

Performance considerations

When it comes to AWS Lambdas, you may have heard about Cold and Warm start.

Performances

  • Cold start : the time required for the Lambda to download your code and make the underlying infrastructure ready to run.
  • Warm start : the time required for your code to be runned.

So, the first time a lambda is called, cold + warm start will occur. AWS will keep this underlying infrastructure up for a non-deterministic amount of time to improve resource management and performance. Hence, if the same lambda is called again during this period, there is only a warm start and no cold start.

That's why many people use function warmers with EventBridge (cron awaking the function periodically to make it warm for business) or play with the provisionned concurrency to have a pool of warmed up functions.

How does Zip or Docker image affect lambda performances

Here is an illustration of start times between zipped code by language and docker analysis made by Mikhail Shilkov.

Languages

First, we can see that whatever the language you code with, Docker images are slower to load than zipped code. It can take 0,6 to 1,4 sec for a containerized lambda to load against around 700ms for a zip.

Why is that ?

Unzipping a ready to load folder, even a big one, is way faster than pulling an image from a registry and starting it.

How do I choose between both ?

Performance is not the only thing you have to take in consideration when you have to choose between zip or docker for AWS Lambda functions. The tools, the skills... used to be used inside your organization are also very important. The ability to deliver properly. The end to end cost, including the modifications you will have to make around CI/CD to integrate this serverless delivery...

Here are some guidelines to help taking the right decisions. It must be challenged by your other constraints :

  • When it comes to synchronous scenarios, I highly recommend to use warmers to avoid cold starts and to use zip files deliverables for your AWS lambdas. Because, having around 1sec of load in such scenarios is not acceptable in a user experience perspective.
  • When it comes to asynchronous scenarios, I would recommend to choose what seems the best for you regarding the skills of your teams, your existing CI/CD etc...

Conclusion

Of course, choosing between zip files and container images delivery for AWS lambda functions cannot be resumed only by the guidelines in this article. But, taking it in consideration in addition to your other constraints is important to obtain a completly informed opinion.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .