How to connect Azure Function with MongoDB Atlas

Ahsan Habib 126 Reputation points
2021-04-04T17:44:55.227+00:00

Here is my scenario: My Database is on MongoDB Atlas. I need to access Atlas from Azure Function(Dynamic/Consumption Plan) and App Service. In doing so, I am facing A timeout issue. I know, this is because I have to whitelist outbound IP addresses of Azure Function and App Service Plan in Atlas. But Azure Function(with Consumption Plan) and App Service update their outbound IP addresses during the autoscaling process. Now there are 2 solutions I can think of:

  1. Whitelist all the IP addresses of an Azure Region/Zone. But this is not an ideal approach, as a single Zone may have many(500~1000 or more) IP addresses.
  2. Using the VPC peering. I guess we may connect Azure and Atlas through VNET. However, to do that, I need to connect the Azure function and App Service to VNET first. The problem is VNET Integration with these services is a very expensive approach.

So my question is what other options do I have? Is there any way to connect them using some managed identity or Authentication/Authorization approach using Azure AD or something like that? Is Federated Authentication fits for such a case?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JayaC-MSFT 5,606 Reputation points
    2021-04-05T17:36:05.473+00:00

    Hello @Ahsan Habib , In case of the first option I would recommend you to consider using service tags : https://learn.microsoft.com/en-us/azure/virtual-network/service-tags-overview#available-service-tags

    Also if you need static, dedicated IP addresses, we recommend App Service Environments (the Isolated tier of App Service plans). { Again this could be an expensive approach as mentioned in point no.2 }
    https://learn.microsoft.com/en-us/azure/azure-functions/ip-addresses#dedicated-ip-addresses

    For ref : https://www.mongodb.com/blog/post/how-to-integrate-azure-functions-with-mongodb

    https://thecodebarbarian.com/getting-started-with-azure-functions-and-mongodb.html


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.