# Interact with MongoDB Atlas in an AWS Lambda Function Using C#

# Interact with MongoDB Atlas in an AWS Lambda Function Using C#

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.

## The prerequisites

* Knowledge of the C# programming language.
* A MongoDB Atlas cluster with sample data, network access (firewall), and user roles already configured.
* An Amazon Web Services (AWS) account with a basic understanding of AWS Lambda.
* Visual Studio with the AWS Toolkit and the Lamda Templates installed (official tutorial).

## Create and configure your Atlas database

This step-by-step MongoDB Atlas tutorial will guide you through creating an Atlas database (free tier available) and loading the sample data.

Already have an AWS account? Atlas supports paying for usage via the AWS Marketplace (AWS MP) without any upfront commitment — simply sign up for MongoDB Atlas via AWS Marketplace.

We will open the network access to any incoming IP to keep this tutorial simple and make it work with the free Atlas cluster tier. Here's how to add an IP to your Atlas project. Adding 0.0.0.0 means that any external IP can access your cluster.

In a production environment, you should restrict access and follow best MongoDB security practices, including using network peering between AWS Lambda and MongoDB Atlas. The free cluster tier does not support peering.

## Build an AWS Lambda function with C#

In Visual Studio, create a basic AWS lambda project using the "AWS Lambda Project (.NET Core - C#)" project template with the "Empty Function" blueprint. We'll use that as the basis of this tutorial. Here's the official AWS tutorial to create such a project, but essentially:

1. Open Visual Studio, and on the File menu, choose New, Project.
2. Create a new "AWS Lambda Project (.NET Core - C#)" project.
3. 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 `FunctionHandler`, (`Function:: FunctionHandler`), which we will designate to Lambda as the entry point.

## Connecting to MongoDB Atlas from a C# AWS Lambda function

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:

```
using MongoDB.Bson;
using MongoDB.Driver;
```

In the Function class, we will declare a static MongoClient member. Having it as a `static` member 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.

If we did not declare MongoClient as `static`, each class instance would create its own set of resources. Instead, the static MongoClient is shared among multiple class instances after a first instance was created (warm start). You can read more technical details about managing MongoDB Atlas connections with AWS Lambda.

We will also add a `CreateMongoClient()` method that initializes the MongoDB client when the class is instantiated. Now, things should look like this:

```
public class Function
{
    private static MongoClient? Client;
    private static MongoClient CreateMongoClient()
    {
            var mongoClientSettings = MongoClientSettings.FromConnectionString(Environment.GetEnvironmentVariable("MONGODB_URI"));
            return new MongoClient(mongoClientSettings);
    }

    static Function()
    {
        Client = CreateMongoClient();
    }
...
}
```

To keep your MongoDB credentials safe, your connection string can be stored in an AWS Lambda environment variable. The connection string looks like this below, and here's how to get it in Atlas.

`mongodb+srv://USER:PASSWORD@INSTANCENAME.owdak.mongodb.net/?retryWrites=true&w=majority`

**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.

If you want to use environment variables in the Mock Lambda Test Tool, you must create a specific "Mock Lambda Test Tool" profile with its own set of environment variables in `aws-lambda-tools-defaults.json` (here's an example).

You can learn more about AWS Lambda environment variables. However, be aware that such variables can be set from within your Visual Studio when publishing to AWS Lambda or directly in the AWS management console on your AWS Lambda function page.

For testing purposes, and if you don't want to bother, some people hard-code the connection string as so:

```
var mongoClientSettings = FromConnectionString("mongodb+srv://USER:PASSWORD@instancename.owdak.mongodb.net/?retryWrites=true&w=majority");
```

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.

```
public string FunctionHandler(string input, ILambdaContext context)
{
    if (Client != null)
    {
        try
        {
            var database = Client.GetDatabase("sample_airbnb");
            var collection = database.GetCollection("listingsAndReviews");
            var result = collection.Find(FilterDefinition.Empty).First();
            return result.ToString();
        }
        catch
        {
            return "Handling failed";
        }
    } else
    {
        return "DB not initialized";
    }
}
```

Using the "listingsAndReviews" collection (a "table" in SQL jargon) in the "sample\_airbnb" database, the code fetches the first document of the collection.

`collection.Find()` normally takes a MongoDB Query built as a BsonDocument, but in this case, we only need an empty query.

## Publish to AWS and test

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 the screenshot below, the AWS Lambda function Handler ("Handler") information is essential as it tells Lambda which method to call when an event is triggered. The general format is Assembly::Namespace.ClassName::MethodName

In our case, the handler is `AWSLambda1::AWSLambda1.Function::FunctionHandler`.

If the option is checked, this dialog will save these options in the `aws-lambda-tools-defaults.json` file.

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.

## Conclusion

We just learned how to build a C# AWS Lambda serverless function efficiently by creating and sharing a MongoDB client and connecting multiple class instances. If you're considering building with a serverless architecture and AWS Lambda, MongoDB Atlas is an excellent option.

The flexibility of our document model makes it easy to get started quickly and evolve your data structure over time. Create a free Atlas cluster now to try it.

If you want to learn more about our MongoDB C# driver, refer to the continuously updated documentation. You can do much more with MongoDB Atlas, and our C# Quick Start is a great first step on your MongoDB journey.