Interact with MongoDB Atlas in an AWS Lambda Function Using C#
Rate this tutorial
AWS Lambda is an excellent choice for C# developers looking for a solid serverless solution with many integration options with the rest of the AWS ecosystem. When a database is required, reading and writing to MongoDB Atlas at lightning speed is effortless because Atlas databases can be instantiated in the same data center as your AWS Lambda function.
In this tutorial, we will learn how to create a C# serverless function that efficiently manages the number of MongoDB Atlas connections to make your Lambda function as scalable as possible.
- Knowledge of the C# programming language.
- A MongoDB Atlas cluster with sample data, network access (firewall), and user roles already configured.
- Open Visual Studio, and on the File menu, choose New, Project.
- Create a new "AWS Lambda Project (.NET Core - C#)" project.
- We'll name the project "AWSLambda1."
Follow the official AWS tutorial above to make sure that you can upload the project to Lambda and that it runs. If it does, we're ready to make changes to connect to MongoDB from AWS Lambda!
In our project, the main class is called
Function. It will be instantiated every time the Lambda function is triggered. Inside, we have a method called
Function:: FunctionHandler), which we will designate to Lambda as the entry point.
Connecting to MongoDB requires adding the MongoDB.Driver (by MongoDB Inc) package in your project's packages.
Next, add the following namespaces at the top of your source file:
In the Function class, we will declare a static MongoClient member. Having it as a
staticmember is crucial because we want to share it across multiple instances that AWS Lambda could spawn.
Although we don't have complete control over, or visibility into, the Lambda serverless environment, this is the best practice to keep the number of connections back to the Atlas cluster to a minimum.
We will also add a
CreateMongoClient()method that initializes the MongoDB client when the class is instantiated. Now, things should look like this:
Note: Visual Studio might store the connection string with your credentials into a aws-lambda-tools-defaults.json file at some point, so don't include that in a code repository.
For testing purposes, and if you don't want to bother, some people hard-code the connection string as so:
Finally, we can modify the FunctionHandler() function to read the first document from the sample_airbnb.listingsAndReviews database and collection we preloaded in the prerequisites.
The try/catch statements are not mandatory, but they can help detect small issues such as the firewall not being set up, or other configuration errors.
Using the "listingsAndReviews" collection (a "table" in SQL jargon) in the "sample_airbnb" database, the code fetches the first document of the collection.
It's time to upload it to AWS Lambda. In the Solution Explorer, right-click on the project and select "Publish to AWS Lambda." Earlier, you might have done this while setting up the project using the official AWS Lambda C# tutorial.
If this is the first time you're publishing this function, take the time to give it a name (we use "mongdb-csharp-function-001"). It will be utilized during the initial Lambda function creation.
In our case, the handler is
If the option is checked, this dialog will save these options in the
Click "Next" to see the second upload screen. The most important aspect of it is the environment variables, such as the connection string.
When ready, click on "Upload." Visual Studio will create/update your Lambda function to AWS and launch a test window where you can set your sample input and execute the method to see its response.
Our Lambda function expects an input string, so we'll use the "hello" string in our Sample Input, then click the "Invoke" button. The execution's response will be sent to the "Response" field to the right. As expected, the first database record is converted into a string, as shown below.