How to securely connect azure static web app with cosmos mongo db?

Sunny Chawla 0 Reputation points
2025-05-08T09:38:20.2666667+00:00

I need to securely connect Azure static web app with Azure cosmos mongo db. I have tried creating private endpoint also tried creating app service. But still struggling with connecting the web application code with azure cosmos mongo db.

This connection works perfectly in development environment.

Please provide detailed steps and help resolve the issue.

Azure Static Web Apps
Azure Static Web Apps
An Azure service that provides streamlined full-stack web app development.
1,155 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Harshitha Veeramalla 1,116 Reputation points Microsoft External Staff Moderator
    2025-05-14T11:19:52.1766667+00:00

    Hi @Sunny Chawla,

    If I setup azure cosmos Db to accept all networks, is this approach secure?

    This is not secure, turning on Allow All Networks makes your DB open to everyone on the network. If anyone has your connection string, they can access your DB.

    This can be used only for development or testing.

    Can I get outbound IP address of Azure static web app?

    Unlike App service or Function apps, Azure Static Web Apps do not provide a fixed outbound IP address.

    Can I restrict access of cosmos Db only to this azure static web app that is publicly accessible?

    You can restrict access to Azure Cosmos DBonly from a publicly accessible Azure Static Web App, but this involves a more complex configuration. Since Azure Static Web Apps do not have a fixed outbound IP address, you can't directly restrict Cosmos DB access by IP.

    As an alternate you can use Function App or App service.

    Check the below steps to configure with Function App:

    • Create Azure Cosmos DB with MongoDB API.{CB0F9AED-0D43-4465-91DC-7D783A16BA2E}

    Use an Azure Function as a Proxy to Secure Cosmos DB Access.

    • Create a sample Azure Function and use the Managed Identity to authenticate and connect to Azure Cosmos DB.
    • Configure Managed Identity by enabling System-assigned Managed Identity for the Azure Function.

    {6FAED5E7-695E-4FF6-BE69-90DE42E71BB9}

    • Assign the Cosmos DB Operator role to the Azure Function's Managed Identity so it can access the Cosmos DB.
      {18880ABA-2867-41CC-9A61-BC59FD6D56B3}

    Refer this MSDoc for more clear steps, it explains configuring Cosmos DB with App service and Function App.

    Hope this helps


    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions, please reply back.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.