GeekTopia


Never Stop Learning

Building The Lambda Package TLDR;

If you would like to see the full version of this article please click here

Video Reference

Yoube Video Instructions

For this example, we are going to use the third party Python library called pymysql. This package is used as a MySQL client used to connect to MySQL databases

To continue you will Python and pip installed on your local machine. For Linux you will need the zip command line utility installed. To compresse the package in Windows you will need 7zip or another third party compression utility. While using the default compression utility in Windows may work, I often hear from users that this utility caused issues.

Step 1

Create a working folder and install the package


mkdir lambda_package
cd lambda_package
pip install pymysql -t ./
                    

Step 2

Add The Custom Appliction Files

Now we can copy the application code into the root of this working directory. If the custom application file was named lambda_function.py, you would place this directly in the lambda_package directory. The directory structure should look like this.

lambda_package
    pymysql
    pyMySQL-0.9.3.dist-info
    lambda_function.py
                    

Step 3

Compress The Lambda Package

Now it is time to compress these files into our Lambda package. This is where things will become a little different for Windows and Linux/Mac users.

Linux/Mac

Please note the “.” at the end of this command. The “.” is used as a wild card for the zip command


zip -r /tmp/lambda_package.zip . 
                    

Windows

Navigate file explorer to the root of the lambda_package folder. Select the directories pymysql, pyMySQL-0.9.3.dist-info as well as the file lambda_function.py. Right click this selection and choose “7-zip -> add to lambda_function.zip”. A new zip file will be created in the lambda_function directory.

7 Zip Add File To AWS Lambda Function Package

Using 7-Zip to Compress files into a AWS Lambda Python Package

Step 4

Updating The Lambda Function Code

Now that our Lambda function package has been created, all that is left is to update the lambda function code and provide AWS with the new package to execute. Navigate to the lambda function in the Lambda portion of the AWS Web console. Below the function configuration section, you will find the Function code section. Below the heading “Code Entry Type”, select “upload a zip file”.

Update Lambda Function Code Web UI
Using AWS Web UI to Update the Lambda Function Code

Follow the prompts to select the lambda_function.zip file previously created. Once you have selected this file, press the save button at the top of the screen.

Save Button
AWS Web UI Lambda Function Save Button

The lambda function page will fresh and the application function code will display an updated copy of code uploaded if the total size of the lambda package is less than 3MB

PyMysql Python Pacakge Lambda Function Layout
Package Contents Viwed In AWS Web Console

Optional - Updating The Lambda Function Code Using The Command Line Tools

If the AWS command line tools are installed and configured, you can use the aws lambda command to update the lambda function code.This requires the lambda function has already been created.


aws lambda update-function-code --function-name ReplaceFunctionName --zip-file fileb://PathTo/lambda_function.zip 
                    

References Used