Step by Step guide : PyMongo to Connect MongoDB Atlas with AWS Lambda
Take your cloud development to the next level by linking MongoDB Atlas and AWS Lambda with PyMongo. Explore how to insert, query, and manage data effortlessly in a fully managed environment.
Published Nov 29, 2024
To create an Atlas cluster, log into the Atlas UI and follow the setup steps. We recommend the free tier, but any tier works—just make sure to select AWS as the cloud provider. Choose a secure username and password for future authorization and set appropriate IP address access.
If you have an AWS account, you can sign up for MongoDB Atlas through the AWS Marketplace without any upfront commitment.
Once your cluster is running, click the ellipsis next to "Browse Collections" to download the required files. After provisioning your cluster, we can proceed to set up the AWS Lambda function.
Sign in to your AWS account and search for “Lambda.” Click the orange “Create function” button at the top right. Select the “Author from scratch” option, name your function (e.g., AWSLambdaDemo), choose Python 3.8 as the runtime, and set the architecture to x86_64. Then click the orange “Create function” button to proceed.
After your function is created, you’ll see an overview at the top and the code source below. To connect AWS Lambda to your MongoDB cluster, we’ll use Visual Studio Code for coding, as we need to include Pymongo—a dependency not pre-installed in AWS Lambda.
We will need to package our code specifically to incorporate Pymongo, so this tutorial won’t follow a typical step-by-step testing approach. Instead, we’ll download the necessary dependencies and upload our code to Lambda first, then verify it works later. More details will follow.
Now we are ready to establish a connection between AWS Lambda and our MongoDB cluster!Create a new directory on your local machine and name it
awslambda-demo
.Let’s install pymongo
. As said above, Lambda doesn’t have every library available. So, we need to download pymongo
at the root of our project. We can do it by working with .zip file archives: In the terminal, enter our awslambda-demo
directory:cd awslambda-demo
Create a new directory where your dependencies will live:
mkdir dependencies
Install pymongo
directly in your dependencies
package:pip install --target ./dependencies pymongo
Open Visual Studio Code, open the
awslambda-demo
directory, and create a new Python file named lambda_function.py
. This is where the heart of our connection will be.Insert the code below in our lambda_function.py
. Here, we are setting up our console to check that we are able to connect to our Atlas cluster. Please keep in mind that since we are incorporating our environment variables in a later step, you will not be able to connect just yet. We have copied the lambda_handler
definition from our Lambda code source and have edited it to insert one document stating my full name into a new “test” database and “test” collection. It is best practice to incorporate our MongoClient outside of our lambda_handler
because to establish a connection and performing authentication is reactively expensive, and Lambda will re-use this instance.If this is properly inserted in AWS Lambda, we will see “Document inserted successfully” and in MongoDB Atlas, we will see the creation of our “test” database and collection along with the single document holding the name “Anaiya Raisinghani.” Please keep in mind we will not be seeing this yet since we haven’t configured our environment variables and will be doing this a couple steps down.
Now, we need to create a .zip file, so we can upload it in our Lambda function and execute our code. Create a .zip file at the root:
cd dependencies
zip -r ../deployment.zip *
This creates a
deployment.zip
file in your project directory.Now, we need to add in our lambda_function.py
file to the root of our .zip file:cd ..
zip deployment.zip lambda_function.py
Once you have your .zip file, access your AWS Lambda function screen, click the “Upload from” button, and select “.zip file” on the right hand side of the page:
Upload your .zip file and you should see the code from your
lambda_function.py
in your “Code Source”:Let’s configure our environment variables. Select the “Configuration” tab and then select the “Environment Variables” tab. Here, put in your “ATLAS_URI” string. To access your connection string, please follow the instructions in our docs.
Once you have your Environment Variables in place, we are ready to run our code and see if our connection works. Hit the “Test” button. If it’s the first time you’re hitting it, you’ll need to name your event. Keep everything else on the default settings. You should see this page with our “Execution results.” Our document has been inserted!
When we double-check in Atlas, we can see that our new database “test” and collection “test” have been created, along with our document with “Anaiya Raisinghani.”This means our connection works and we are capable of inserting documents from AWS Lambda to our MongoDB cluster. Now, we can take things a step further and input a simple aggregation pipeline!
For our pipeline, let’s change our code to connect to our
sample_restaurants
database and restaurants
collection. We are going to be incorporating our aggregation pipeline to find a sample size of five American cuisine restaurants that are located in Brooklyn, New York. Let’s dive right in!Since we have our pymongo
dependency downloaded, we can directly incorporate our aggregation pipeline into our code source. Change your lambda_function.py
to look like this:Here, we are using
$match
to find all the American cuisine restaurants located in Brooklyn. We are then using $limit
to only five documents out of our database. Next, we are using $project
to only show the fields we want. We are going to include “borough”, “cuisine”, and the “name” of the restaurant. Then, we are executing our pipeline and printing out our results. Click on “Deploy” to ensure our changes have been deployed to the code environment. After the changes are deployed, hit “Test.” We will get a sample size of five Brooklyn American restaurants as the result in our console: