Custom libraries with AWS Lambda Layers

In general, everyone who uses servers to run large applications have some standard libraries or their own custom libraries. So, they use them in their application for processing. These libraries are stored in a place and import them in code files.

Now-a-days, everyone is showing interest in using serverless framework to reduce cost. This serverless framework becomes much famous where they can do their coding in cloud efficiently. But, the question is.

  1. In which cloud we can use this serverless framework?
  2. What’s use of this?
  3. Where’s the place to store libraries?

Now, we are going to implement this serverless framework in AWS where it provides a service called “LAMBDA”. This lambda follows serverless framework where it consists of cloud9 IDE to implement code. This LAMBDA makes the developers to reduce their testing and development time cause the infrastructure is entirely handled by the public cloud AWS. No need of worrying about time and Infrastructure. LAMBDA uses S3 (Simple Storage Service) to store libraries and then they are imported into LAMBDA by using lambda layers. Each lambda function can be handle 5 layers at a time and each layer’s size is of 250MB at max.

Now, let’s take a use-case for implementing Lambda with Lambda Layers where we use libraries. The common use-case which every AWS admin faces is stopping and starting EC2 instances where they do manually or by enabling schedulers in Cloud Watch Events. This can be done with Lambda function by using Lambda Layers.

Before going into this use case, we are going to write code by using Python to control this EC2 service. Here’s the code.

Fig: Python Code to Start Instance

Fig: Python Code to Stop Instance

Now, if we observe here, Boto3 Package which is imported in first line, this is the standard library which was provided by boto3. This boto3 should be installed in a single directory/folder and zip it and upload to it lambda layers from S3 or can upload directly by selecting upload option.

This process has series of steps to implement:

Step-1: Creating Lambda Layer

Fig: Lambda Layers Create Layer

Step-2: After click on the create layer, we will loaded with form to enter the following details which is showed in below figure.

Fig: Form to Create Layer

Step-3: After creating a layer which consists of now create a lambda function

Fig: Create Lambda Function

Step-4: Lambda Creation with Runtime and Permissions (IAM Role)

Fig: Form to create new Lambda Function

Selecting the Runtime as Python 3.7 because we are working with Python standard libraries, then create function by selecting the role from IAM users or by creating a new role in IAM and assigning it to this lambda function.

Step-5: After Function creates, we add layer to created function in above step.

Fig: Selecting Layers option

Fig: Selecting Add a Layer option

Fig: Select the Created Layer

Step-6: Finally, add the layer to function and save the function, then test the function. It works. Chill!!!