How to Use PyMongo to Connect MongoDB Atlas with AWS Lambda
Anaiya Raisinghani6 min read • Published Apr 02, 2024 • Updated Apr 02, 2024
Rate this tutorial
Picture a developer’s paradise: a world where instead of fussing over hardware complexities, we are free to focus entirely on running and executing our applications. With the combination of AWS Lambda and MongoDB Atlas, this vision becomes a reality.
Armed with AWS Lambda’s pay-per-execution structure and MongoDB Atlas’ unparalleled scalability, developers will truly understand what it means for their applications to thrive without the hardware limitations they might be used to.
This tutorial will take you through how to properly set up an Atlas cluster, connect it to AWS Lambda using MongoDB’s Python Driver, write an aggregation pipeline on our data, and return our wanted information. Let’s get started.
- AWS Account; Lambda access is necessary
- GitHub repository
- Python 3.8+
Our first step is to create an Atlas cluster. Log into the Atlas UI and follow the steps to set it up. For this tutorial, the free tier is recommended, but any tier will work!
Please ensure that the cloud provider picked is AWS. It’s also necessary to pick a secure username and password so that we will have the proper authorization later on in this tutorial, along with proper IP address access.
Once your cluster is up and running, click the ellipses next to the Browse Collections button and download the ![fully provisioned Atlas cluster with sample data downloaded](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fblt6f9a801b00041f85%2F64a703af1f8d5a624e58764b%2FScreenshot_2023-07-06_at_2.10.25_PM.png&w=3840&q=75)
sample dataset
. Your finished cluster will look like this:
![fully provisioned Atlas cluster with sample data downloaded](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fblt6f9a801b00041f85%2F64a703af1f8d5a624e58764b%2FScreenshot_2023-07-06_at_2.10.25_PM.png&w=3840&q=75)
Once our cluster is provisioned, let’s set up our AWS Lambda function.
Sign into your AWS account and search for “Lambda” in the search bar. Hit the orange “Create function” button at the top right side of the screen, and you’ll be taken to the image below. Here, make sure to first select the “Author from scratch” option. Then, we want to select a name for our function (AWSLambdaDemo), the runtime (3.8), and our architecture (x86_64).
![creating our Lambda function](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fblt5f19a8158061b23b%2F64a705858f332a953d54482d%2FScreenshot_2023-07-06_at_2.17.34_PM.png&w=3840&q=75)
![creating our Lambda function](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fblt5f19a8158061b23b%2F64a705858f332a953d54482d%2FScreenshot_2023-07-06_at_2.17.34_PM.png&w=3840&q=75)
Hit the orange “Create function” button on the bottom right to continue. Once your function is created, you’ll see a page with your function overview above and your code source right below.
![AWS Lambda function set up](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fblt2721b8c101d8d4fd%2F64a705f74d81430fe78ff398%2FScreenshot_2023-07-06_at_2.20.28_PM.png&w=3840&q=75)
![AWS Lambda function set up](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fblt2721b8c101d8d4fd%2F64a705f74d81430fe78ff398%2FScreenshot_2023-07-06_at_2.20.28_PM.png&w=3840&q=75)
Now, we are ready to set up our connection from AWS Lambda to our MongoDB cluster.
To make things easier for ourselves because we are going to be using Pymongo, a dependency, instead of editing directly in the code source, we will be using Visual Studio Code. AWS Lambda has a limited amount of pre-installed libraries and dependencies, so in order to get around this and incorporate Pymongo, we will need to package our code in a special way. Due to this “workaround,” this will not be a typical tutorial with testing at every step. We will first have to download our dependencies and upload our code to Lambda prior to ensuring our code works instead of using a typical
requirements.txt
file. More on that below.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:Create a new directory where your dependencies will live:
Install
pymongo
directly in your dependencies
package: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:
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:![how our directory is set up](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fblte3b31b42ab877c4e%2F64a70bc682c5c661f5c38578%2FScreenshot_2023-07-06_at_2.45.12_PM.png&w=3840&q=75)
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:
![how to upload a .zip file in AWS Lambda](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fblt4343f378c5402722%2F64a70bf926afdb0077c6fc19%2FScreenshot_2023-07-06_at_2.46.06_PM.png&w=3840&q=75)
![how to upload a .zip file in AWS Lambda](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fblt4343f378c5402722%2F64a70bf926afdb0077c6fc19%2FScreenshot_2023-07-06_at_2.46.06_PM.png&w=3840&q=75)
Upload your .zip file and you should see the code from your ![AWS Lambda code source once view](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fblt465ca3b17b00781d%2F64a70c3e26afdb1284c6fc1d%2FScreenshot_2023-07-06_at_2.47.16_PM.png&w=3840&q=75)
lambda_function.py
in your “Code Source”:
![AWS Lambda code source once view](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fblt465ca3b17b00781d%2F64a70c3e26afdb1284c6fc1d%2FScreenshot_2023-07-06_at_2.47.16_PM.png&w=3840&q=75)
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.
![environment variables configuration](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fbltb08cd9dfa2062bd0%2F64a70c8338fe4382296abbac%2FScreenshot_2023-07-06_at_2.48.23_PM.png&w=3840&q=75)
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!
![After running our code, we can see that it works](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fblt3114cad2f928facb%2F64a70cd905df43144f5aeb26%2FScreenshot_2023-07-06_at_2.49.52_PM.png&w=3840&q=75)
![After running our code, we can see that it works](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fblt3114cad2f928facb%2F64a70cd905df43144f5aeb26%2FScreenshot_2023-07-06_at_2.49.52_PM.png&w=3840&q=75)
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.”
![result of our connection shown in MongoDB Atlas](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fblt2bad4a2e4128a600%2F64a70d1f23241010123ff625%2FScreenshot_2023-07-06_at_2.50.57_PM.png&w=3840&q=75)
![result of our connection shown in MongoDB Atlas](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fblt2bad4a2e4128a600%2F64a70d1f23241010123ff625%2FScreenshot_2023-07-06_at_2.50.57_PM.png&w=3840&q=75)
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:
![results from our aggregation pipeline shown in AWS Lambda](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fblta88b18f150f5e4bb%2F64a70e12ce1ee5779b8fc24f%2FScreenshot_2023-07-06_at_2.55.03_PM.png&w=3840&q=75)
![results from our aggregation pipeline shown in AWS Lambda](/developer/_next/image/?url=https%3A%2F%2Fimages.contentstack.io%2Fv3%2Fassets%2Fblt39790b633ee0d5a7%2Fblta88b18f150f5e4bb%2F64a70e12ce1ee5779b8fc24f%2FScreenshot_2023-07-06_at_2.55.03_PM.png&w=3840&q=75)
Our aggregation pipeline was successful!
This tutorial provided you with hands-on experience to connect a MongoDB Atlas database to AWS Lambda. We also got an inside look on how to write to a cluster from Lambda, how to read back information from an aggregation pipeline, and how to properly configure our dependencies when using Lambda. Hopefully now, you are ready to take advantage of AWS Lambda and MongoDB to create the best applications without worrying about external infrastructure.
If you enjoyed this tutorial and would like to learn more, please check out our MongoDB Developer Center and YouTube channel.