Connect an Azure Cosmos DB database to your application in Azure Spring Apps
Article
Note
The Basic, Standard, and Enterprise plans will be deprecated starting from mid-March, 2025, with a 3 year retirement period. We recommend transitioning to Azure Container Apps. For more information, see the Azure Spring Apps retirement announcement.
This article applies to: ✅ Basic/Standard ✅ Enterprise
Instead of manually configuring your Spring Boot applications, you can automatically connect selected Azure services to your applications by using Azure Spring Apps. This article demonstrates how to connect your application to an Azure Cosmos DB database.
By default, Service Connectors are created at the application level. To override the connections, you can create other connections again in the deployments.
Use the Azure CLI
Use the Azure CLI to configure your Spring app to connect to a Cosmos NoSQL Database by using the az spring connection create command, as shown in the following example. Be sure to replace the variables in the example with actual values.
Note
Updating Azure Cosmos DB database settings can take a few minutes to complete.
If you're using Service Connector for the first time, start by running the command az provider register --namespace Microsoft.ServiceLinker to register the Service Connector resource provider.
Tip
Run the command az spring connection list-support-types --output table to get a list of supported target services and authentication methods for Azure Spring Apps. If the az spring command isn't recognized by the system, check that you have installed the required extension by running az extension add --name spring.
Use the Azure portal
Alternatively, you can use the Azure portal to configure this connection by completing the following steps. The Azure portal provides the same capabilities as the Azure CLI and provides an interactive experience.
Select your Azure Spring Apps instance in the Azure portal and select Apps from the navigation menu. Choose the app you want to connect and select Service Connector on the navigation menu.
On the Authentication tab, choose Connection string. Service Connector automatically retrieves the access key from your Cosmos DB account. Select Next: Networking.
On the Networking tab, select Configure firewall rules to enable access to target service, then select Next: Review + Create.
On the Review + Create tab, wait for the validation to pass and then select Create. The creation can take a few minutes to complete.
Once the connection between your Spring apps and your Cosmos DB database has been generated, you can see it in the Service Connector page and select the unfold button to view the configured connection variables.
The following Terraform script shows how to set up an app deployed to Azure Spring Apps with an Azure Cosmos DB account.
In this article, you learned how to connect your application in Azure Spring Apps to an Azure Cosmos DB database. To learn more about connecting services to your application, see Connect to an Azure Cache for Redis cache.
This tutorial demonstrates how to deploy a Spring Boot application to Azure Kubernetes Service and use it to perform operations on data in an Azure Cosmos DB for NoSQL account.