If you would like to see the full version of this article please click Here
Building the Lambda Layer.
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.
We start by creating a new working folder for our lambda Layer. Inside we create a second folder named python.
mkdir lambda_layer cd lambda_layer mkddir python cd python
Next, we use the pip package manager to install the package into our working folder. The “-t” argument passed to pip, instructs pip to install the package in the “Target” location instead of at a system location.
pip install pymysql -t ./
The directory structure for the lambda layer should look like this.
lambda_layer python pymysql pyMySQL-0.9.3.dist-info
Now it is time to compress these files into our Lambda layer. This is where things will become a little different for Windows and Linux/Mac users
Step 3 Linux/Mac/Cloud 9
For Linux/Mac, the zip command can be used. Please note the “.” at the end of this command. The “.” is used as a wild card for the zip command.
cd .. zip -r /tmp/lambda_layer.zip .
Step 3 Windows
For Windows, navigate file explorer to the root of the lambda_layer folder. Right click the python folder and choose “7-zip -> add to python.zip”. A new zip file will be created in the lambda_layer directory.
Navigate to the Lambda portion of the AWS Web Console. From the left menu choose layers and click “Create Layer.
Follow the prompts to create the lambda layer.
For Windows, choose the local zip file created.
For Linux/Mac/Cloud 9, choose "upload from S3". Provide the bucket and file name. Example: s3://geektopia-lambda-layers/lambda_layer.zip
Select all of the runtimes you would like this layer to be made available for. When finished, click “Create” at the bottom of the screen
Navigate back to the lambda function. Push the “Layers” button in the center of the console. Scroll down and choose the lambda layer from the drop down list. Choose version 1 to the right.
Be sure to click the save button for the layer to be added to the lambda function.