How to use external libraries in Lambda Function

In this blog, we are going to add libraries to our lambda function so that we can use external libraries for our use because AWS has Boto3 library only, so if you want to use other libraries than you have to add those libraries to your function layer.

so let's start the process of how to add libraries. In the previous blog, we have already explained to you the lambda function and how to create the lambda function.

Step 1: go to your lambda function which we have created in the previous blog, you can see in the below image.

Step 2: Go to the code section and here we want to use the Flask library of python so we will import the flask library by writing some code, After writing the code save your code by clicking on the save button as shown in the below image.

Step 3: After saving the code click on the test button to check if your code is working or not.

Step 4: You can see in the below image there is an error because it is not able to find the library or module name flask, so we have to add the library to the layer of our function.

Step 5: now go above and click on layers to see if any layer is connected or not.

Step 6: As you can see in the below image there is no layer added so we have to add the layer of our libraries to it.

Step 7: Create on folder give it any name and open the command prompt in that folder and create one directory of as shown in the below image and install flask to that particular folder.

Note: you have to create a virtual environment for this task otherwise it will do the change in your original flask library in your system.

Step 8: After downloading the library you have to create a zip file of your folder we have created a package.zip file of that as you can see in the below image.

Step 9: Now go to the layer option you can see in the below image it is marked in red, click on that layer option to add library.

Step 10: you can see we have already added libraries in our layer but we have to add another library so click on create layer option as marked in the below image.

Step 11: give any name to your layer, upload your zip file and choose the compatible runtime, you can see in the below image how we have done.

Step 12: After adding all the details click on create option.

Step 13: After creating again go back to your lambda function and click on the layers option as shown in the below image.

Step 14: After that click on Add layer option, you can see marked below.

Step 15: Now in the below the image we have three options, the first option is AWS layers these are the layers provided by AWS itself, In second the option custom layers in this you can add the layers created by you, and the Third one is specified ARN, this ARN is provided when we create the layers so you can choose either the second option or the third one, we are choosing the third one and we added the ARN of our flask layer ARN.

Step 16: Now if you will go back then you can see in your layer option there is one layer added.

step 17: Again go back to your code and write the code to import the flask library and click on the save button to save it.

Step 18: Click on the test button To check if your code is working or not as shown in the below image.

Step 19: Congrats your code is working properly as shown in the below image.

So this is how we add external libraries to lambda functions, hope you liked this blog.

Keep learning, Happy Coding!!!!!!

--

--

Wakeupcoders - Digital Marketing & Web App Company
Wakeupcoders - Digital Marketing & Web App Company

Written by Wakeupcoders - Digital Marketing & Web App Company

We make your business smarter and broader through the power of the internet. Researcher | Web developer | Internet of things | AI | www.wakeupcoders.com