Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Note
The Basic and Standard plans entered a retirement period on March 17, 2025. For more information, see the Azure Spring Apps retirement announcement.
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.
Prerequisites
- An application deployed to Azure Spring Apps. For more information, see Quickstart: Deploy your first application to Azure Spring Apps.
- An Azure Cosmos DB database instance.
- Azure CLI version 2.45.0 or higher.
Prepare your project
Add one of the following dependencies to your application's pom.xml file. Choose the dependency that is appropriate for your API type.
API type: NoSQL
<dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-starter-data-cosmos</artifactId> </dependency>API type: MongoDB
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>API type: Cassandra
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-cassandra</artifactId> </dependency>
Update the current app by running
az spring app deploy, or create a new deployment for this change by runningaz spring app deployment create.
Connect your app to the Azure Cosmos DB
The following Terraform script shows how to set up an app deployed to Azure Spring Apps with an Azure Cosmos DB account.
provider "azurerm" {
features {}
}
variable "application_name" {
type = string
description = "The name of your application"
default = "demo-cosmosdb"
}
resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "China North 2"
}
resource "azurerm_cosmosdb_account" "cosmosdb" {
name = "cosmosacct-${var.application_name}-001"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
offer_type = "Standard"
kind = "GlobalDocumentDB"
consistency_policy {
consistency_level = "Session"
}
geo_location {
failover_priority = 0
location = azurerm_resource_group.example.location
}
}
resource "azurerm_cosmosdb_sql_database" "cosmosdb" {
name = "cosmos-${var.application_name}-001"
resource_group_name = azurerm_cosmosdb_account.cosmosdb.resource_group_name
account_name = azurerm_cosmosdb_account.cosmosdb.name
}
resource "azurerm_spring_cloud_service" "example" {
name = "${var.application_name}"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
}
resource "azurerm_spring_cloud_app" "example" {
name = "${var.application_name}-app"
resource_group_name = azurerm_resource_group.example.name
service_name = azurerm_spring_cloud_service.example.name
is_public = true
https_only = true
}
resource "azurerm_spring_cloud_java_deployment" "example" {
name = "default"
spring_cloud_app_id = azurerm_spring_cloud_app.example.id
quota {
cpu = "2"
memory = "4Gi"
}
instance_count = 1
jvm_options = "-XX:+PrintGC"
runtime_version = "Java_11"
environment_variables = {
"spring.cloud.azure.cosmos.endpoint" : azurerm_cosmosdb_account.cosmosdb.endpoint
"spring.cloud.azure.cosmos.key" : azurerm_cosmosdb_account.cosmosdb.primary_key
"spring.cloud.azure.cosmos.database" : azurerm_cosmosdb_sql_database.cosmosdb.name
}
}
resource "azurerm_spring_cloud_active_deployment" "example" {
spring_cloud_app_id = azurerm_spring_cloud_app.example.id
deployment_name = azurerm_spring_cloud_java_deployment.example.name
}
Next steps
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.