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.
- In which cloud we can use this serverless framework?
- What’s use of this?
- 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.
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
Step-2: After click on the create layer, we will loaded with form to enter the following details which is showed in below figure.
Step-3: After creating a layer which consists of now create a lambda function .
Step-4: Lambda Creation with Runtime and Permissions (IAM Role)
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.
Step-6: Finally, add the layer to function and save the function, then test the function. It works. Chill!!!