How do I create an AWS Lambda deployment package for Python?

You might need to create a deployment package that includes the function modules located in the root of the .zip file with read and execute permissions for all files.

"ModuleNotFoundError" and "Module cannot be loaded" are common errors for Lambda functions in Python. These errors are usually due to incorrect folder structure or file permissions with the deployment package .zip file.

Install all dependency modules local to the function project

Note: Most Python modules are platform-independent, but some modules are compiled against specific operating system environments. AWS Lambda runs under a Linux environment. If you are installing modules with pip for deployment, it's a best practice to build the .zip file in a Linux environment to be sure that dependencies are included for the correct platform.

If you are using pip install to install your modules, the modules will be in your user folder or system folders. When you create a deployment package for Lambda, the package will not include these system-installed dependencies. Install your dependencies locally to your function project folder by running the following command:

pip install boto3 -t ./

This installs the boto3 Python modules to the root of your project folder. Repeat this for all modules required for your Lambda function.

Add permissions (Linux and macOS only)

After all dependent modules are downloaded to the project folder, run the following command to make sure that the files have the required permissions:

chmod -R 755 .

Manually build a deployment package

Run this command to zip up the project folder for deployment to Lambda:

zip -r ../myDeploymentPackage.zip .

This zips all files in the publish folder into a .zip file located in the parent folder.

Verify deployment package

Linux and macOS:

unzip -l myDeploymentPackage.zip

This command displays the folder structure. The main Python function files for your application must be in the root of your project folder. Other dependency files, such as additional Python modules, can be in located in other folders.

Windows:

Use a zip tool to unzip your files. The main Python function files must be in the root folder of the .zip file. If there is another base folder in the .zip file that contains your Python function files, recreate the .zip file with all the function files in the root folder.

Upload deployment package to your Lambda function

  1. Open the AWS Lambda console.
  2. Choose Functions on the navigation pane, and then open your function.
  3. In the Function code section, expand the Code entry type drop-down list, and then choose Upload a .ZIP file.
  4. Choose Upload, and then select your .zip file.
  5. Choose Save.
  6. Choose Test.

You can also upload your .zip file from the AWS Command Line Interface (AWS CLI) using the update-function-code command.


Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2018-07-30