The Chronicles of Lambda Layers: The What, the Why and the How

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.

Latest

SENTRY integration in your React Native App for Error/Crash tracking

Sentry captures data by using an SDK within your...

Recall the concepts of useCallback.

useCallback hook is one of the best hooks offered...

Value of Comments in the code!!

During my journey of Software Development, I am always...

YOLO:Bullet Paced Algorithm

http://sh017.hostgator.tempwebhost.net/media/33949d0e61af4b50f374c534713f56b3 According to world health organization, more than 1.35 million...

Featured

Developing Enterprise Application in Node.js – CJS Vs ESM

Node.js is a popular runtime environment for building server-side...

Integrating your web react applications with their React Native(android and IOS) apps using QR code

Integrating a web application with Android and iOS apps...

YOLO: Bullet Paced Algorithm – popular choice for object detection in autonomous vehicles 

According to world health organization, more than 1.35 million...

Introduction to Amazon Web Services(AWS)

Amazon Web Services (AWS) is a cloud computing platform that offers a wide range of services to help organizations and individuals build and run...

The EC2 Storage Trinity

Amazon Elastic Compute Cloud (EC2) is a popular cloud computing service that provides resizable compute capacity in the cloud. EC2 instances can be used...

Recursive AWS Lambda Functions: An Overview

AWS Lambda is a popular serverless computing service that allows you to run code in response to a variety of events, such as changes...

LEAVE A REPLY

Please enter your comment!
Please enter your name here