In a land far, far away, lives a young man named Unnchit. Unnchit has always dreamt of being a software engineer and has devoted himself in learning the hidden arts of AWS-jitsu. In his quest to learn the ancient arts, he stumbles upon the Book of Layers, which contains spells so powerful that it could lead organizations and teams to cleaner and efficient codes. Unnchit, being the kind soul he his, decided to spread the secrets of the book to help all the developers. Today we unravel some of his teachings on an artifact called The Lambda Layers.
The What?
Before we try to understand what exactly Lambda Layers are, a little bit on AWS-Lambda first (ref. AWS Lambda Documentation).
With AWS Lambda, you can run code without provisioning or managing servers. You pay only for the compute time that you consume — there’s no charge when your code isn’t running. You can run code for virtually any type of application or backend service — all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.
Now, Lambda Layers helps us to manage code and data (like binaries, custom runtime environments) shared across multiple lambda functions. This effectively helps in better code reusability across multiple dev teams. Lambda Layers are immutable, meaning, once a layer is associated with lambda, you cannot modify it directly. It can however be versioned like a lambda function.
The Why?
Why should we use Lambda Layers you asked?
Using layers organizations can promote separation of code responsibilities. For example, we have two different lambda functions but both of them have same dependency. A dependency can be as simple as importing a third-party library to our code or something a bit more complex like setting up custom runtime for our lambda. In this scenario, we can separate the dependent parts of our code and package them as a lambda layer, while two different teams could be working on their independent lambda functions. The two different functions can now reference the same lambda layer. As we see, this would lead to way cleaner and efficient code. It gives the developers the leverage to iterate faster and focus on the writing the business logic
The How?
Hmm, so how does such a powerful artifact work behind the hood?
So, when we create a layer, it gets uploaded independently as a zip file and then the files are extracted into /opt
directory in the execution environment. We can have up to five layers associated to a lambda function and each of them are extracted to the same folder. The order of lambda layer is important as they can potentially overwrite each other.
cc: AWS online tech talk YT
Inside the /opt
directory, AWS automatically stores certain things according to the runtime environment of the layer, as shown in the image above. Our code can then directly reference these files from the lambda execution environment without us explicitly providing the path to the files.
Bonus: The When???
Now, we have a got a grasp of Lambda Layer, but Unnchit still got an anecdote to share on when he used the Lambda Layer functionality and that will help us in understanding how we may use it ourselves as well.
Once, Unnchit was setting up a WhatsApp webhook to handle user responses on the WhatsApp service and send relevant messages accordingly (sounds interesting?? You can read more in it by accessing these glyphs: WhatsApp Template, WhatsApp Msg Payload).
Now, in one of the responses he had to send a PDF file to the user. The PDF file was to be converted from a html string. Unnchit, using his python prowess summoned the pdfkit
library. But if only it was that simple. Disaster struck, the pdfkit
library demanded the presence of wkhtmltopdf
binary file which could only be installed in the system with the help ancient command sudo apt-get install wkhtmltopdf
. However, having known all the exploits people have done and could do with sudo
command, AWS had setup defense against it and blocked all root access to the Lambda executable environment. Now, what can Unnchit do?? Well, he recalled all of the valuable lesson the Book of Layers had taught him and invoked the Layer property on his lambda function. He uploaded the binary files as a lambda layer and overcame this hurdle.
A layer associated with the lambda function called ESS-WhatsApp
a code excerpt of Unnchit using the code from the layer
— — — — — — — — — — — — — — — — — The End — — — — — — — — — — — — — — — — —
TLDR: Lambda Layer OP, use it
Author: Manish Bisht, Software Engineer with VectoScalar Technologies.