Using the Node.js MongoDB Driver with AWS Lambda
Rate this tutorial
This article will show you how to write an AWS Lambda serverless function that connects to MongoDB Atlas to query some data and how to avoid common pitfalls that would cause poor performance.
To get started, let's create a basic lambda function. This function will be used later on to connect to our MongoDB instance.
In AWS, go to the Lambda service. From there, you can click on the "Create Function" button. Fill in the form with a name for your function, and open the advanced settings.
Because you'll want to access this function from a browser, you will need to change these settings:
- Check the "Enable function URL" option.
- Under "Auth Type," pick "NONE."
- Check the "Configure cross-origin resource sharing (CORS)" box.
Now click "Create Function" and you're ready to go. You will then be presented with a screen similar to the following.
You can see a window with some code. This function will return a 200 (OK) status code, and the body of the request will be "Hello from Lambda!".
You can test this function by going to the "Configuration" above the code editor. Then choose "Function URL" from the left navigation menu. You will then see a link labeled "Function URL." Clicking this link will open a new tab with the expected message.
If you change the code to return a different body, click "Deploy" at the top, and refresh that second tab, you will see your new message.
You've just created your first HTTPS endpoint that will serve the response generated from your function.
While it can be trivial to write simple functions, there are some considerations that you'll want to keep in mind when dealing with AWS Lambda and MongoDB.
You can write your functions directly in the code editor provided by AWS Lambda, but chances are you will want to store your code in a repository to share with your team. When you push your code, you will want to be careful not to upload some of your secret keys. With your database, for example, you wouldn't want to push your connection string accidentally. You could use an environment variable for this.
From the AWS Lambda screen, go into the "Configuration" tab at the top, and pick "Environment Variables" from the left navigation bar. Click "Edit," and you will be presented with the option to add a new environment variable. Fill in the form with the following values:
- Key: MONGODB_CONNECTION_STRING
- Value: This is a connection string
Now go back to the code editor, and use the
process.env to return the newly created environment variable as the body of your request.
If you refresh the tab you opened earlier, you will see the value of that environment variable. In the example below, you will change the value of that environment variable to connect to your MongoDB Atlas database.
When you initialize a
MongoClient with the Node.js driver, it will create a pool of connections that can be used by your application. The MongoClient ensures that those connections are closed after a while so you don't reach your limit.
A common mistake when using MongoDB Atlas with AWS Lambda is creating a new connection pool every time your function gets a request. A poorly written function can lead to new connections being created every time, as displayed in the following diagram from the Atlas monitoring screen.
That sudden peak in connections comes from hitting a Lambda function every second for approximately two minutes.
The secret to fixing this is to move the creation of the MongoDB client outside the handler. This will be shown in the example below. Once the code has been fixed, you can see a significant improvement in the number of simultaneous connections.
Now that you know the pitfalls to avoid, it's time to create a function that connects to MongoDB Atlas.
Because you'll need additional packages to run this function, you won't be able to use the code editor anymore.
Create a new folder on your machine, initialize a new Node.js project using
npm, and install the
Create a new
index.mjs file in this directory, and paste in the following code.
This code will start by creating a new MongoClient. Note how the client is declared outside the handler function. This is how you'll avoid problems with your connection pool. Also, notice how it uses the connection string provided in the Lambda configuration rather than a hard-coded value.
Inside the handler, the code connects to the
sample_mflix database and the
movies collection. It then finds the first 10 results and converts them into an array.
The 10 results are then returned as the body of the Lambda function.
Your function is now ready to be deployed. This time, you will need to zip the content of this folder. To do so, you can use your favorite GUI or the following command if you have the
zip utility installed.
Go back to the Lambda code editor, and look for the "Upload from" button in the upper right corner of the editor. Choose your newly created
output.zip file, and click "Save."
Now go back to the tab with the result of the function, and hit refresh. You should see the first 10 documents from the
Using AWS Lambda is a great way to write small functions that can run efficiently without worrying about configuring servers. It's also a very cost-effective way to host your application since you only pay per usage. You can find more details on how to build Lambda functions to connect to your MongoDB database in the .