Create Lambda Layer for PyMongo To Connect DocumentDB using Python
To connect Amazon DocumentDB from Lambda function, Python developers can use PyMongo library to establish a connection object. Using PyMongo in a Lambda function requires creation of a Lambda layer which includes PyMongo package. In this Lambda tutorial, I want to show how Python programmers can build a Lambda layer with PyMongo package that is used to connect Amazon DocumentDB cluster which is MongoDB compatible.
First of all, I will create the Lambda function layer on an EC2 instance that is launched by creation of an Amazon Cloud9 environment.
I will not explain the steps of launching a new Cloud9 environment in this tutorial but basically a Cloud9 environment provides a cloud IDE for developers running on AWS.
Basically, I will be using Amazon Cloud9 as the developer environment used to create Lambda layer for this tutorial.
Of course, you can use your own development platform for the tasks explained in this tutorial
When Cloud9 environment is ready open the cloud IDE.
Lambda function layers require following specific directory or path structure as explained at Layer paths for each Lambda runtime AWS Lambda documentation.
Because the guide dictates us to use a particular path structure for Python Lambda function layers, I create the container path folder sturucture as follows:
When you create the site-packages subfolder and navigate into it, install the pymongo library
Now we are ready to package our layer.
Go to top folder.
Zip the package
At the end of all these steps, the environment will have the same structure on your Cloud9 IDE.
You can validate if everything is correctly done on your side by comparing the below screenshot with your Cloud9 development environment.
Finally copy the compressed file into S3 bucket so that in later steps while creating the Lambda layer you can reference this compressed file. For this demo I created and used a sample Amazon S3 bucket named docdb-lambda-layer and copied the compressed file into it.
Now let's create the PyMongo Python Layer for our Lambda function.
Navigate to AWS Lambda Service initial screen on AWS Management Console.
On the left menu, you will see Layers menu option. Click on Layers.
Click on ""Create layer" button
You can give a descriptive name to distinguish your Lambda Layer later from other layers created.
Also provide a description as best practise.
For the source code of the Lambda Layer, since we already copied the compressed file into an Amazon S3 bucket, choose the option "Upload a file from Amazon S3"
Get the Object URL of the zip file from S3 bucket and paste it into the textbox.
It is similar to:
https://docdb-lambda-layer.s3.eu-central-1.amazonaws.com/docdb_pymongo_layer.zip
Finally, it is better to provide the list of compatible runtimes. At least you should be selecting Python rather than Java, Go or Node.js, etc.
Now we are ready to click create button and complete Lambda layer creation for PyMongo which is a required library to connect MongoDB compatible Amazon DocumentDB database from our Lambda functions.
For a sample AWS Lambda function which is developed in Python and connects to Amazon DocumentDB for read and write operations, you can refer to Lambda tutorial Connect Amazon DocumentDB from AWS Lambda Function using Python
In the referenced tutorial, you will find source codes developed in Python which opens a connection to DocumentDB using pymongo and writes sample data to a collection and reads back within the Lambda function.
I hope cloud developers find this tutorial useful to understand basics of creating a Lambda function layer in order to introduce new packages and libraries into their Lambda codes.