# Getting Started With Azure Spring Apps and MongoDB Atlas: A Step-by-Step Guide

## Introduction

Embrace the fusion of cloud computing and modern application development as we delve into the integration of Azure
Spring Apps
and MongoDB. In this tutorial, we'll guide you through the process of creating
and deploying a Spring Boot
application in the Azure Cloud, leveraging the strengths of Azure's platform, Spring Boot's simplicity, and MongoDB's
capabilities.

Whether you're a developer venturing into the cloud landscape or looking to refine your cloud-native skills, this
step-by-step guide provides a concise roadmap. By the end of this journey, you'll have a fully functional Spring Boot
application seamlessly running on Azure Spring
Apps, with MongoDB handling your data storage needs and a REST API ready
for interaction. Let's explore the synergy of these technologies and propel your cloud-native endeavors forward.

## Prerequisites

- Java 17
- Maven 3.8.7
- Git (or you can download the zip folder and unzip it locally)
- MongoDB Atlas cluster (the M0 free tier is enough for this tutorial). If you don't have
  one, you can create one for free.
- Access to your Azure account with enough permissions to start a new Spring App.
- Install the Azure CLI to be
  able to deploy your Azure Spring App.

I'm using Debian, so I just had to run a single command line to install the Azure CLI. Read the documentation for your
operating system.

```shell
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
```

Once it's installed, you should be able to run this command.

```shell
az --version 
```

It should return something like this.

```
azure-cli                         2.56.0

core                              2.56.0
telemetry                          1.1.0

Extensions:
spring                            1.19.2

Dependencies:
msal                            1.24.0b2
azure-mgmt-resource             23.1.0b2

Python location '/opt/az/bin/python3'
Extensions directory '/home/polux/.azure/cliextensions'

Python (Linux) 3.11.5 (main, Jan  8 2024, 09:08:48) GCC 12.2.0]

Legal docs and information: aka.ms/AzureCliLegal

Your CLI is up-to-date.
```

> Note: It's normal if you don't have the Spring extension yet. We'll install it in a minute.

You can log into your Azure account using the following command.

```shell
az login
```

It should open a web browser in which you can authenticate to Azure. Then, the command should print something like this.

```json
[
  {
    "cloudName": "AzureCloud",
    "homeTenantId": "",
    "id": "",
    "isDefault": true,
    "managedByTenants": [],
    "name": "MDB-DevRel",
    "state": "Enabled",
    "tenantId": "",
    "user": {
      "name": "maxime.beugnet@mongodb.com",
      "type": "user"
    }
  }
]
```

Once you are logged into your Azure account, you can type the following command to install the Spring extension.

```shell
az extension add -n spring
```

## Create a new Azure Spring App

To begin with, on the home page of Azure, click on `Create a resource`.

![Create a resource][1]

Then, select Azure Spring Apps in the marketplace.

![Azure Spring Apps][2]

Create a new Azure Spring App.

![Create a new Azure Spring App][3]

Now, you can select your subscription and your resource group. Create a new one if necessary. You can also create a
service name and select the region.

![Basics to create an Azure Spring App][4]

For the other options, you can use your best judgment depending on your situation but here is what I did for this
tutorial, which isn't meant for production use...

- Basics:
    - Hosting: "Basic" (not for production use, but it's fine for me)
    - Zone Redundant: Disable
    - Deploy sample project: No
- Diagnostic settings:
    - Enable by default.
- Application Insights:
    - Disable (You probably want to keep this in production)
- Networking:
    - Deploy in your own virtual network: No
- Tags:
    - I didn't add any

Here is my `Review and create` summary:

![Review and create][5]

Once you are happy, click on `Create` and wait a minute for your deployment to be ready to use.

## Prepare our Spring application

In this tutorial, we are deploying
this [Java, Spring Boot, and MongoDB template,
available on GitHub. If you want to learn more about this template, you can read
my article, but in a few words:
It's a simple CRUD Spring application that manages
a `persons` collection, stored in MongoDB with a REST API.

- Clone or download a zip of this repository.

```shell
git clone git@github.com:mongodb-developer/java-spring-boot-mongodb-starter.git
```

- Package this project in a fat JAR.

```shell
cd java-spring-boot-mongodb-starter
mvn clean package
```

If everything went as planned, you should now have a JAR file available in your `target` folder
named `java-spring-boot-mongodb-starter-1.0.0.jar`.

## Create our microservice

In Azure, you can now click on `Go to resource` to access your new Azure Spring App.

 for
  the Java driver. It should look like this:

```
mongodb+srv://user:password@free.ab12c.mongodb.net/?retryWrites=true&w=majority
```

- Create a new environment variable in your configuration.

, it's time
> to create one and use the login and password in your connection string.

## Atlas network access

MongoDB Atlas clusters only accept TCP connections from known IP addresses.

As our Spring application will try to connect to our MongoDB cluster, we need to add the IP address of our microservice
in the Atlas Network Access list.

- Retrieve the outbound IP address in the `Networking` tab of our Azure Spring App.

,
you can access the Swagger UI here:

```
https:///swagger-ui/index.html
```

 and start exploring all the features
MongoDB Atlas has to offer.

Got questions or itching to share your success? Head over to
the MongoDB Community Forum – we're all ears and ready to help!

Cheers to your successful deployment, and here's to the exciting ventures ahead! Happy coding! 🚀

[1]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt85b83d544dd0ca8a/65b1e83a5cdaec024a3b7504/1_Azure_create_resource.png

[2]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltbb51a0462dcbee8f/65b1e83a60a275d0957fb596/2_Azure_marketplace.png

[3]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltf3dd72fc38c1ebb6/65b1e83a24ea49f803de48b9/3_Azure_create_spring_app.png

[4]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltba32459974d4333e/65b1e83a5f12edbad7e207d2/4_Azure_create_spring_app_basics.png

[5]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltc13b9d359d2bea5d/65b1e83ad2067b1eef8c361a/5_Azure_review_create.png

[6]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt6c07487ae8a39b3a/65b1e83ae5c1f348ced943b7/6_Azure_go_to_resource.png

[7]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt37a00fe46791bb41/65b1e83a292a0e1bf887c012/7_Azure_create_app.png

[8]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt402b5ddcf552ae28/65b1e83ad2067bb08b8c361e/8_Azure_create_app_details.png

[9]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltf0d1d7e1a2d3aa85/65b1e83a7d4ae76ad397f177/9_Azure_access_new_microservice.png

[10]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltb238d1ab52de83f7/65b1e83a292a0e6c2c87c016/10_Azure_env_variable_mdb_uri.png

[11]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt67bc373d0b45280e/65b1e83a5cdaec4f253b7508/11_Azure_networking_outbound.png

[12]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt18a1a75423ce4fb4/65b1e83bc025eeec67b86d13/12_Azure_networking_Atlas.png

[13]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltdeb250a7e04c891b/65b1e83a41400c0b1b4571e0/13_Azure_deploy_app_tab.png

[14]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt390946eab49df898/65b1e83a7d4ae73fbb97f17b/14_Azure_deploy_app.png

[15]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt7a33fb264f9fdc6f/65b1e83ac025ee08acb86d0f/15_Azure_app_deployed.png

[16]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt2d899245c14fb006/65b1e83b92740682adeb573b/16_Azure_assign_endpoint.png

[17]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltc531bc65df82614b/65b1e83a450fa426730157f0/17_Azure_endpoint.png

[18]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt6bf26487ac872753/65b1e83a41400c1b014571e4/18_Azure_Atlas_doc.png

[19]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt39ced6072046b686/65b1e83ad2067b0f2b8c3622/19_Azure_Swagger.png