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.

  1. You create a directory, for example project-dir.
  2. Save all of your Python source files (the .py files) at the root level of this directory.
  3. Install any libraries using pip. Again, you install these libraries at the root level of the directory.
    Copy
    pip install module-name -t /path/to/project-dir

    For example, the following command installs the requests HTTP library in the project-dirdirectory.

    Copy
    pip install requests -t /path/to/project-dir

    If 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.cfg file in your /path/to/project-dir with the following content.

    Copy
    [install]
    prefix= 
  4. Zip the content of the project-dir directory, 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
    virtualenv path/to/my/virtual-env

    You can activate the environment on Windows, OS X, and Linux as follows:

    • On Windows, you activate using the activate.bat:
      Copy
      path\to\my\virtual-env\Scripts\activate.bat 
    • On OS X and Linux, you source the activate script:
      Copy
      source  path/to/my/virtual-env/bin/activate
  • 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:

  1. First, create .zip file with your Python code you want to upload to AWS Lambda.
  2. 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-packages 

    For OS X, Linux, the directory is:

    Copy
    $VIRTUAL_ENV/lib/python3.6/site-packages

    Note

    If you don’t find the packages in the site-packages directory in your virtual environment, you might find it in the dist-packages directory.