AWS

How to Install Python Packages in AWS Lambda?

Python is arguably the most growing and popular scripting language among experts in various domains. Packages are the same as the directory which contains other sub-packages and modules in a structured way, making its access easy.

This guide will explain how to install Python packages in AWS Lambda.

How to Install Python Packages in AWS Lambda?

To install Python packages in AWS Lambda, search the Lambda service from the AWS Management Console and click on it:

Click on the “Create function” button to head into the function configuration page:

On this page, choose the “Author from scratch” option and type the function name with runtime:

Scroll down and click on the “Create function” button:

On the function overview page, visit the “Code” section:

Add a package in the code and click on the “Deploy” button, and then click on the “Test” button:

import openai

Select the “Create new event” option and type the event name:

Scroll down to the bottom of the window and click on the “Save” button:

Click on the “Test” button:

The error displays that the package is not installed yet:

Simply select the code and copy it:

Paste it on the “Editor”:

Save the file from the “File” menu or pressing “Ctrl + S” from the keyboard:

Save the file in the directory (Lambda Test) on the system:

Head into the “Command Prompt” from the directory in which the file has been saved and install the package:

pip install openai -t .

Simply, zipped the selected directory (Lambda Test):

Head back to the “Lambda” dashboard and expand the “Upload from” menu and click on the “.zip file” button:

Click on the “Upload” button:

Select the zip file to be uploaded and click on the “Save” button:

Click on the “OK” button for the code updated message:

Simply click on the “Test” button:

The code has been successfully tested and the output displays the “Hello from Lambda” message:

This is all about installing Python packages in AWS Lambda.

Conclusion

To install Python Packages in AWS Lambda, create a function from the Lambda dashboard and test its code by adding the package of your choice. Create a file with the Python code on the local system and head into the Terminal from that directory and install the package. Zip the directory and upload it on the Lambda dashboard. Test the code again with the installed package providing a successful result. This guide has explained how to install Python packages in AWS lambda.

About the author

Talha Mahmood

As a technical author, I am eager to learn about writing and technology. I have a degree in computer science which gives me a deep understanding of technical concepts and the ability to communicate them to a variety of audiences effectively.