DEV Community

Cover image for Handle Python Packages in Lambda Environment
Rameesha Khan
Rameesha Khan

Posted on

Handle Python Packages in Lambda Environment

I was working on a microservice in which I need to make a lambda service for a flask application. And I was like yeah so easy man! but as soon as I dive into the project, I faced problem with managing python packages. So I am here to tell you how to easily cope up with this problem.

Follow these steps:

  • First of all create a python project using serverless either lambda function or an API gateway or any other.

  • Initialize npm in the project by typing command

`npm init`
Enter fullscreen mode Exit fullscreen mode
  • Install the plugin named serverless-python-requirements by command
`npm i serverless-python-requirements`
Enter fullscreen mode Exit fullscreen mode
  • Add these lines in your yml file

Plugin Configuration in yml file

Now, create a virtual environment for installing the packages. If you don't create a virtual environment it will install all the packages in the root directory and your folder structure would be a mess and it is necessary to have a virtual environment.

  • For creating a virtual environment: You can use either of the command. Here i am creating a virtual environment named "venv".
virtualenv venv
Enter fullscreen mode Exit fullscreen mode

or

python -m venv venv
Enter fullscreen mode Exit fullscreen mode

for vitualenv you should have the pacakage or you can install by using command

pip install virtualenv

Now after running this command you will see a venv folder has been created into your directory.

  • After creating virtual environment activate the environment by typing the command
venv\Scripts\activate.bat
Enter fullscreen mode Exit fullscreen mode

Additional Info: If you want to be out of the virtual env, type
venv\Scripts\deactivate.bat

when activated, you will see the environment name in the left side of the terminal.

Now in this environment, you can install all the packages needed for your application.

  • After installing all the packages you have to make a requirments.txt file for listing down all the packages necessary for the application. For this you don't need to list all the packages manually, just type in the command
pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

This will list all the packages with their installed versions.

Now you are almost right there, just one thing is needed.

  • After doing all the steps you have to install docker in your system and start that in your machine. Importantly don't forget to add these lines into your yml file

Docker Configuration in yml file

Basically what it will do is to tell the AWS SAM to use docker to install packages in a non-linux environment.

At the time of deployment, It will first look into the requirments.txt file and install all the listed packages which will make them available for the application.

And voila ! you are all set to depend on dependencies.

Top comments (1)

Collapse
 
rameeshakhan profile image
Rameesha Khan

Feel free to ask anything in the comments section!