Hello,
You need to create a deployment package whenever you need to use an external library other than the AWS SDK (boto3) like requests library or if you want to use different version of AWS SDK.
There are two ways in order to create a deployment package, the first one is to install the python packages (python dependencies) directly to project’s folder and then you zip it. Or to create a virtual environment and pip the package in the virtual env, and then you take the content of site-packages and put it in the root folder, then you zip it.
1- Without creating virtual env
You will use pip to install dependencies/libraries. For information to install pip, go to Installation.
- You create a directory, for example
project-dir. - Save all of your Python source files (the .py files) at the root level of this directory.
- Install any libraries using pip. Again, you install these libraries at the root level of the directory.
Copy
pip installmodule-name-t/path/to/project-dirFor example, the following command installs the
requestsHTTP library in theproject-dirdirectory.Copypip install requests -t/path/to/project-dirIf using Mac OS X and you have Python installed using Homebrew (see Homebrew), the preceding command will not work. A simple workaround is to add a
setup.cfgfile in yourwith the following content./path/to/project-dirCopy[install] prefix= - Zip the content of the
project-dirdirectory, which is your deployment package.Important
Zip the directory content, not the directory. The contents of the Zip file are available as the current working directory of the Lambda function. For example: /project-dir/codefile.py/lib/yourlibraries
2- With virtual environment
This section explains how to create a deployment package if you are using a Python environment that you created with the Virtualenv tool. Consider the following example:
- Created the following isolated Python environment using the Virtualenv tool and activated the environment:
Copy
virtualenvpath/to/my/virtual-envYou can activate the environment on Windows, OS X, and Linux as follows:
- On Windows, you activate using the
activate.bat:Copypath\to\my\virtual-env\Scripts\activate.bat - On OS X and Linux, you source the
activatescript:Copysourcepath/to/my/virtual-env/bin/activate
- On Windows, you activate using the
- Also, suppose you have installed the requests package in the activated environment (assume that you will you use these in your code). You can install these packages as follows :
Copy
pip install requests
Now, to create a deployment package you do the following:
- First, create .zip file with your Python code you want to upload to AWS Lambda.
- Add the libraries from preceding activated virtual environment to the .zip file. That is, you add the content of the following directory to the .zip file (note again that you add the content of the directory and not the directory itself).
For Windows the directory is:
Copy%VIRTUAL_ENV%\Lib\site-packagesFor OS X, Linux, the directory is:
Copy$VIRTUAL_ENV/lib/python3.6/site-packagesNote
If you don’t find the packages in the
site-packagesdirectory in your virtual environment, you might find it in thedist-packagesdirectory.