How to Use Azure Functions with MongoDB Atlas in Java
Rate this tutorial
Cloud computing is one of the most discussed topics in the tech industry. Having the ability to scale your infrastructure up and down instantly is just one of the many benefits associated with serverless apps. In this article, we are going write the function as a service (FaaS) — i.e., a serverless function that will interact with data via a database, to produce meaningful results. FaaS can also be very useful in A/B testing when you want to quickly release an independent function without going into actual implementation or release.
- A basic understanding of the Java programming language.
Getting started with the Azure serverless function is very simple, thanks to the Azure IntelliJ plugin, which offers various features — from generating boilerplate code to the deployment of the Azure function. So, before we jump into actual code, let's install the plugin.
With this, we are ready to create our first Azure function.
Now, let's create a project that will contain our function and have the necessary dependencies to execute it. Go ahead and select File > New > Project from the menu bar, select Azure functions from Generators as shown below, and hit Next.
Now we can edit the project details if needed, or you can leave them on default.
In the last step, update the name of the project and location.
With this complete, we have a bootstrapped project with a sample function implementation. Without further ado, let's run this and see it in action.
We can deploy the Azure function either locally or on the cloud. Let's start by deploying it locally. To deploy and run locally, press the play icon against the function name on line 20, as shown in the above screenshot, and select run from the dialogue.
Copy the URL shown in the console log and open it in the browser to run the Azure function.
This will prompt passing the name as a query parameter as defined in the bootstrapped function.
Update the URL by appending the query parameter
http://localhost:XXXXX/api/HttpExample?name=World, which will print the desired result.
In the previous step, we created our first Azure function, which takes user input and returns a result. But real-world applications are far more complicated than this. In order to create a real-world function, which we will do in the next section, we need to understand how to connect our function with a database, as logic operates over data and databases hold the data.
Add these to the IP individually under Network Access.
With this, our project is ready to connect and interact with our cloud database.
Let's generate the boilerplate code for the function by right-clicking on the package name and then selecting New > Azure function class. We'll call this function class
- Rename the function name from
- Remove the
namevariables, as we don't have any query parameters.
Our updated code looks like this.
Using the connection string, we can create an instance of
MongoClientsthat can be used to open connection from the
We can query our database for the total number of movies in the collection, as shown below.
Updated code for
getMoviesCountfunction looks like this.
Copy the URL from the console output and paste it on Postman to validate the output.
Let's deploy this on the Azure cloud on a
Linuxmachine. Click on
Azure Exploreand select Functions App to create a virtual machine (VM).
Now right-click on the Azure function and select Create.
Change the platform to
If for some reason you don't want to change the platform and would like use Window OS, then add standard DNS route before making a network request.
After a few minutes, you'll notice the VM we just created under
Function App. Now, we can deploy our app onto it.
Press Run to deploy it.
Once deployment is successful, you'll find the
URLof the serverless function.
Again, we'll copy this
URLand validate using Postman.
Let's add the boilerplate code again.
To capture the user input year that will be used to query and gather information from the collection, add this code in:
To use this information for querying, we create a
Filtersobject that can pass as input for
The updated code is:
Now let's validate this against Postman.
The last step in making our app production-ready is to secure the connection
URI, as it contains credentials and should be kept private. One way of securing it is storing it into an environment variable.
Adding an environment variable in the Azure function can be done via the Azure portal and Azure IntelliJ plugin, as well. For now, we'll use the Azure IntelliJ plugin, so go ahead and open Azure Explore in IntelliJ.
Then, we select
Function Appand right-click
This will open a tab with all existing properties. We add our property into it.
Now we can update our function code to use this variable. From
After redeploying the code, we are all set to use this app in production.
With MongoDB Atlas on Microsoft Azure, developers receive access to the most comprehensive, secure, scalable, and cloud–based developer data platform on the market. Now, with the availability of Atlas on the Azure Marketplace, it’s never been easier for users to start building with Atlas while streamlining procurement and billing processes. Get started today through the Atlas on Azure Marketplace listing.
Easy Deployment of MEAN Stack with MongoDB Atlas, Cloud Run, and HashiCorp Terraform
Jan 30, 2023