Creating Python Deployment Package for AWS Lambda Function

Dhandapani Sudhakar
BI3 Technologies
Published in
4 min readJun 20, 2020

AWS Lambda

AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services. It is a computing service that runs code in response to events and automatically manages the computing resources required by that code.

Building Lambda functions with Python

You can run Python code in AWS Lambda. Lambda provides runtimes for Python that execute your code to process events. Your code runs in an environment that includes the SDK for Python (Boto 3), with credentials from an AWS Identity and Access Management (IAM) role that you manage.

Need for the deployment package

Creating a lambda function using python is very easy and also it has a lot of packages by default but sometimes we need to add some additional packages to satisfy our requirements.

Those required packages can be added to the function by uploading the python code as a deployment package with the needed packages.

We can use the AWS Lambda console to upload the deployment package.

For instance, the below python code is using the “requests” package which can be added manually using PIP (PIP is package management software that is used to install and manage software packages written in Python).

import json
import tempfile
import os
import requests
def lambda_handler(event, context):
url = “https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/us-east-1/index.csv"
req = requests.get(url)
fileName = tempfile.gettempdir()+”/”+”index.csv”;
open(fileName, ‘wb’).write(req.content)
print(“File Downloaded to tmp folder”)
os.remove(fileName);
print(“File Removed from tmp folder”)
return {
‘statusCode’: 200,
‘body’: json.dumps(‘Hello from Lambda!’)
}

If you execute the above code in Lambda Console, it will throw an error like the one below.

We can avoid the module missing error by creating the deployment package and uploading it to Lambda Function, to achieve this please follow the below steps.

Step 1: Open Command Prompt and create one folder

Step 2: Get inside the folder you created

Step 3: Now Create one virtual environment using python’s virtualenv package

Project folder and virtual environment creation

Once you created the virtual environment, you can see a folder named with the env name and it will contain some directories like below

Folder and files inside the environment I created

Step 4: Now activate the virtual environment by executing the activate.bat file from the environment's Scripts folder.

Once you have activated the environment, you can install the required packages to that project which will be stored inside the environment’s Lib\site-packages folder, for our example, I have installed the requests package.

Activating the environment and installing the required packages
The site-packages folder which contains the required and default packages with the lambda_function.py python file

Step 5: Now create a python file named lambda_function.py with your desired code and paste it inside the site-packages folder.

Step 6: Now zip the site-packages folder with your lambda function name created in AWS Lambda Console.

Note: You can avoid the additional packages before zipping the packages folder If you want to reduce the size of the deployment package.

The zip file is named as your lambda function name, which contains the required packages and lambda_function.py file

Step 7: Now go to your lambda console and select your function.

Step 8: In the function code part, click on the Actions drop-down and upload the zip file.

Once the upload is successful, If u execute the above-mentioned code it will work perfectly and return the response.

Code execution results in the AWS Lambda console

Now we have successfully created the python deployment package for the AWS Lambda Function.

About Us

Bi3 has been recognized for being one of the fastest-growing companies in Australia. Our team has delivered substantial and complex projects for some of the largest organizations around the globe and we’re quickly building a brand that is well known for superior delivery.

Website : https://bi3technologies.com/

Follow us on,
LinkedIn : https://www.linkedin.com/company/bi3technologies
Instagram :
https://www.instagram.com/bi3technologies/
Twitter :
https://twitter.com/Bi3Technologies

--

--