Hello Amin Kharaghani,
Welcome to the Microsoft Q&A and thank you for posting your questions here.
I understand that your Azure App Service unable to connect to Azure Cosmos DB (MongoDB API). You have really done a lot and thank you for the lucid explanatory.
Let me go straight to your questions:
Is there a limitation with the free tier of Azure App Service or Cosmos DB that affects connectivity?
There are no specific limitations in terms of connectivity, but the free tier of Azure App Service has limited networking capabilities, which might affect DNS resolution or overall connection performance. Try upgrading to a paid tier to test if this resolves the issue.
Are there any additional network configurations required to allow Azure App Service to connect to Cosmos DB?
Since you’ve enabled "Allow access from Azure services" and added outbound IPs, no additional configurations should be necessary. However, double-check DNS resolution as it seems to be the root cause. Azure App Service might be having trouble resolving the DNS name of the Cosmos DB endpoint. This is more common with the mongodb+srv://
connection string format, which relies on DNS SRV records for sharded clusters. Try switching to the mongodb://
format in the connection string (without SRV protocol) and see if that resolves the issue.
MONGO_URI="mongodb://<username>:<password>@<hostname>:<port>/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000"
Could there be an issue with SSL/TLS settings when using the free tier?
SSL/TLS should work the same on both free and paid tiers. You are using certifi
correctly, and it works locally, so SSL/TLS doesn’t appear to be the problem. However, ensure you're using the appropriate connection parameters (ssl=true
or tls=true
).
Has anyone faced a similar issue or can provide insights into what might be going wrong?
Many have faced DNS-related issues when using mongodb+srv://
in combination with Azure App Service. Using mongodb://
(without SRV) can resolve these DNS issues.
I hope this is helpful! Do not hesitate to let me know if you have any other questions.
Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.