create-zip-file-aws-lambda

How to create a ZIP file for AWS Lambda in Python

To deploy a Python-based AWS Lambda function, your code must be uploaded in a ZIP format. This ZIP file can contain just your Python script or additional files and other dependencies like modules and libraries. AWS Lambda reads this package to run your function in the cloud. 

This guide explains how to prepare and ZIP your Python Lambda function for deployment.

How to create a ZIP file for AWS Lambda in Python

Step 1: Prepare your Python file

The first things you need to do is define your fuction in a Python file. 

For example: You can have the file named as lambda_function.py

This file could contain a basic Python function as shown below:

def lambda_handler(event, context):

    return {

        'statusCode': 200,

        'body': 'Hello from Lambda!'

    }

Step 2: Install dependencies locally (if needed)

If your Lambda function uses external Python libraries (like requests, boto3, pandas, etc.), you need to install them in a local directory using the following code:

mkdir python

pip install requests -t python/

Here, you’ll need to replace requests with your actual dependencies.

Step 3: Bundle your code and dependencies

Now you’ll need to bundle your code and dependencies into a single ZIP file. You can do that using the following code:

cd python

zip -r ../function.zip .

cd ..

zip -g function.zip lambda_function.py

If you only have a .py file and no dependencies, then you can use the following code:

zip function.zip lambda_function.py

Step 4: Upload the ZIP to AWS Lambda

Now, you can upload this ZIP file by going to the AWS Lambda Console, choosing or creating a function, and uploading function.zip under Function code > Code entry type > Upload a .zip file.

Alternatively, you can deploy it using the AWS CLI:

aws lambda update-function-code --function-name YourFunctionName --zip-file fileb://function.zip



Pump saves ~60% on CLOUD *for free*

FAQ

Get started with PUMP for free

No contracts, no credit card needed to sign up

Get started with PUMP for free

No contracts, no credit card needed to sign up

Get started with PUMP for free

No contracts, no credit card needed to sign up

1390 Market Street, San Francisco, CA 94102

Made with

in San Francisco, CA

© All rights reserved. Pump Billing, Inc.