Connect to Cosmos DB from local machine

Tomas E 1 Reputation point
2022-09-14T06:39:20.723+00:00

Hi,

Can I connect to Cosmos DB via a VPN Gateway without adding my public IP to the firewall?

I'm working from home (laptop) and connect to CosmosDB and SQL server instances during development. I connect using the public IP. The drawback is that I currently don't have a fixed IP and I need to update the firewall almost every day (CosmosDB takes some time to update).

I hoped that a VPN connection to an Azure virtual network would solve the problem and also improve security as I could shut down public access. So I created a VPN Gateway and a Virtual Network (vnet), but I can't get it working because it still uses the public IP from the laptop, and the request is denied.

MongoServerError: Error=13, Details='Request originated from IP xx.xx.xx.xx through public internet. This is blocked by your Cosmos DB account firewall settings. More info: https://aka.ms/cosmosdb-tsg-forbidden

Here is my setup

The CosmosDB and SQL server databases are connected to the vnet (let's call it vnetX) using a private endpoint. I also have a VM on a different vnet (vnetY). I peered vnetX with vnetY and I can connect with ssh to the VM using the private IP, so I know the gateway and vnet works. When I do echo $SSH_CLIENT when I'm connected to the VM I get the private IP the laptop is assigned from the VPN Gateway. I've mapped the addresses of the databases to their private IPs in my hosts file.

Laptop -> VPN Gateway -> vnetX
Cosmos DB -> Private endpoint -> vnetX
SQL server -> Private endpoint -> vnetX
VM -> vnetY -> vnetX

I connect from a Pop_OS! laptop using the build in OpenVPN-client. I would like to be able to use mongosh and sqlcmd to connect to the database.

Adding my public IP to the firewall kind of defeats the purpose of the VPN. So is there a way to connect to Cosmos DB and SQL server using VPN, without adding my public IP to the firewall?

Azure Private Link
Azure Private Link
An Azure service that provides private connectivity from a virtual network to Azure platform as a service, customer-owned, or Microsoft partner services.
472 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,469 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. msrini-MSFT 9,261 Reputation points Microsoft Employee
    2022-09-14T09:23:28.01+00:00

    Hi,

    yes you can access the SQL DB from your laptop which is connected to the VNET which has the Private Endpoint of the SQL in it.

    You will need to create a host name entry to your actual SQL FQDN pointing to the Private Endpoint IP of the SQL. Then open your sqlcmd and try to connect to the SQL using the FQDN which should resolve to the Private Endpoint IP.

    Let me know if you have any questions.

    Regards,
    Karthik Srinivas


  2. KapilAnanth-MSFT 36,861 Reputation points Microsoft Employee
    2022-09-14T10:00:52.313+00:00

    Hi @Tomas E ,

    Welcome to the Microsoft Q&A Platform. Thank you for reaching out & I hope you are doing well.
    I understand that you would like to connect to Cosmos DB with Private EndPoint via P2S.

    • I see you have created the CosmosDB PrivateEndPoint in VNetX
    • As stated by @msrini-MSFT , you should be able to connect to the DB via P2S as long as the PE and VPN Gateway are in same VNet.
    • Kindly make sure you are able to successfully connect to the CosmosDB from a VM in the VNetX (This will help us eliminate any issues from CosmosDB side)
    • Once the above is confirmed, make sure you are using the correct FQDN of the cosmosDB in your host file
    • Do "ping <cosmosDBFQDN>" and make sure correct IP is getting resolved

    Please do let me know if this works or you require additional information on the above.

    Cheers,
    Kapil.

    ----------------------------------------------------------------------------------------------------------------

    Please don’t forget to close the thread by clicking "Accept the answer" wherever the information provided helps you, as this can be beneficial to other community members.


  3. Tomas E 1 Reputation point
    2023-02-08T12:11:19.5266667+00:00

    I've finally found out what the issue was. It was a missing/incorrect record in the local hosts file.

    I had:

    <IP> <account-name>.mongo.cosmos.azure.com

    <IP2> <account-name>-<region>.privatelink.mongo.cosmos.azure.com

    in my hosts file. I was told by MS support that it was important to have the "privatelink" record. That was incorrect, and unfortunately I didn't try without it until recently. So the hosts file should look like this:

    <IP> <account-name>.mongo.cosmos.azure.com

    <IP2> <account-name>-<region>.mongo.cosmos.azure.com

    Now it works!