Azure Function HTTP Trigger URL Unreachable if Cosmos DB Account Key is Referenced in Python Script

Mason-1850 0 Reputation points
2023-07-20T22:35:54.58+00:00

I've had an Azure Function Python app successfully up and running for a few months until yesterday when it started experiencing the strangest behavior where my HTTP trigger function URL (ex: https://my-function-name.azurewebsites.net/api/myHttpTrigger) returns a constant 502 error if my code is referencing my Cosmos DB account key or connection string. I'm pretty new to Azure Functions but again, this was working fine for months until yesterday and now the following happens:

  • When a Cosmos DB account key is stored as an App Setting and read as an environment variable in the function's Python scripts, the HTTP trigger function URL is down with a 502 error (using Cosmos DB connection string from App Setting, storing connection string and/or key in a Key Vault, as well as hard-coding the key or connection string in the python script gets the same result and the URL is unreachable)
  • Remove the Cosmos DB App Settings (or hard-coded keys / connection string) and the URL is immediately back up and working
  • Altering any other App Settings doesn't have any affect and I've checked all the diagnostic tools to no luck

Has anyone encountered this before? Thanks!

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,024 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,535 questions
{count} votes

2 answers

Sort by: Most helpful
  1. SnehaAgrawal-MSFT 17,486 Reputation points Microsoft Employee
    2023-07-26T08:55:31.8366667+00:00

    @Mason-1850 Thanks for reaching here!  

    It's possible that the issue is related to the Cosmos DB account key or connection string being stored as an App Setting. You may try below few things.

    1. Verify the Connection attribute and that it references a setting that exists in your Azure function app. The value on this attribute shouldn't be the connection string itself, but the name of the configuration setting.
    2. Verify that the databaseName and containerName values exist in your Azure Cosmos DB account. If you're using automatic value replacement (using %settingName% patterns), make sure that the name of the setting exists in your Azure function app.
    3. Confirm your Azure Cosmos DB account's firewall configuration to ensure that it's not blocking the Azure function.
    4. Also, Azure Functions trigger and bindings for Azure Cosmos DB depend on the extension package Microsoft.Azure.WebJobs.Extensions.CosmosDB  over the base Azure Functions runtime. Always keep these packages updated, because they include fixes and new features that can help you address any potential issues you might encounter.
    5. Note that the Azure Functions trigger uses latest version change feed mode. Currently, the Azure Functions trigger for Azure Cosmos DB is supported for use with the API for NoSQL only.

    Let us know.

    0 comments No comments

  2. Mason-1850 0 Reputation points
    2023-08-03T12:00:34.8066667+00:00

    Thanks so much for the reply. Unfortunately, this didn't resolve the issue and, for the sake of time, I moved some of the functionality of my app into two separate functions and that seems to be working