After Deployment in Azure function - Internal server error 500 (exception caught attempting to connect to SQL DB)

Reshma 0 Reputation points
2025-06-17T12:39:07.56+00:00
  • Create Azure Resources:
    • Azure Function App: Create an Azure Function App in the Azure portal.
    • Key Vault: Set up an Azure Key Vault to store your sensitive information like passwords securely.
    • Azure SQL Database: Create an Azure SQL Database in the Azure portal.
  • Configure Key Vault:
    • Store the secret password securely in Azure Key Vault.
  • Develop Azure Function:
    • Visual Studio: Develop an Azure Function with HTTP trigger in Visual Studio.
    • HTTP Trigger: Implement an HTTP trigger function that will receive HTTP requests.
  • Access Key Vault Secret:
    • Use Azure Function's Managed Identity to access Key Vault secrets securely without storing credentials in code.
    • Retrieve the secret password from Key Vault within your Azure Function code.
  • Connect to Azure SQL Database:
    • Use ADO.NET or Entity Framework within your Azure Function to connect to the Azure SQL Database.
    • Perform data retrieval or other operations based on the secret password verification.
  • Verify and Respond:
    • Verify the secret password retrieved from Key Vault with the one stored in Azure SQL Database.
    • If verified, retrieve data from the database.
    • Construct a response from the Azure Function based on the database query results.
  • Return Response to User:
    • Send the response back to the user who initiated the HTTP request.

Once these steps done I have deployed to the Azure Function. But when I run the code I am getting the Internal server error.

If I run the python program locally I am getting result, but after deployment I am getting this error.

Developer technologies | Visual Studio | Extensions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Gade Harika (INFOSYS LIMITED) 2,250 Reputation points Microsoft External Staff
    2025-11-14T07:12:42.66+00:00

    Thanks for reaching out.
    Your local run succeeds because it uses credentials and local network context. In Azure, a 500 indicates the Function can’t authenticate or reach SQL/Key Vault. Fix by enabling the Function’s Managed Identity, granting it AAD access to SQL (CREATE USER FROM EXTERNAL PROVIDER + roles), using Authentication=ActiveDirectoryMsi in the connection string, and ensuring Key Vault RBAC + (if applicable) VNet/Private DNS for private endpoints. Validate with Application Insights and Diagnose & Solve Problems. Docs & refs: Functions → SQL with Managed IdentityPyODBC MSI stringPython Functions MSI to SQL (ODBC 17.8)Key Vault RBAC & identitiesKV reference identity behaviorSQL firewall & Allow Azure servicesFunctions + VNet + Private Endpoints tutorialApp Insights / 500 troubleshooting.

    Let us know if the issue persists after following these steps. I’ll be happy to assist further if needed. If the issue has been resolved, Kindly mark the provided solution as "Accept Answer", so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.


Your answer

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