Search code examples
azure-web-app-serviceazure-virtual-network

Connect to cosmos db from azure app service via vnet


I have a service deployed in the Azure app service, which interacts with Azure Cosmos DB, both of which are in Australia East. I have integrated VNET(in Australia East) with subnet_1 with Azure Cosmos DB and integrated Azure App service with VNET in subnet_2 using Outbound Traffic in the networking tab.

Using this config app service is not able to connect to cosmos db, even though they are in same vnet but different subnet.

I have enabled Accept connections from within public Azure datacenters in cosmos db, which is allowing app service to connect but this is not ideal. My understanding is that resources in the same VNET even though in different subnets should be able to connect to each other.

I am expecting a solution to integrate app service and cosmos db via VNET. I am facing the same with blob storage as well, I hope the solution provided by cosmos db will work for blob storage as well.


Solution

  • I created Cosmos DB with container items like below:

    enter image description here

    In App service -> Networking -> vnet integration like below:

    enter image description here

    I created virtual network and app service Cosmos db in same Australia East location connected with vnet integration like below:

    enter image description here

    Create vnet access in cosmos DB account. In cosmos DB -> Networking -> select your virtual network and update firewall configuration like below:

    enter image description here

    In the firewall settings, select Allow access to Azure services is enabled. This allows your App Service to connect to Cosmos DB even if it doesn't have a public IP address.

    Make sure in virtual network add service endpoint in Microsoft.AzureCosmosDB like below.

    enter image description here

    When you enable service endpoint traffic it will go through azure backbone network, redirects to app service successfully like below:

    enter image description here

    Reference:

    azure-docs/articles/cosmos-db/how-to-configure-vnet-service-endpoint.md at main · MicrosoftDocs/azure-docs · GitHub