Search code examples
azureshellazure-cosmosdb

How to add IP address to a Cosmosdb database firewall through the Azure CLI


I want to add my IP to the cosmosdb firewall through the Azure CLI. The only way to do it with the Azure CLI is with az cosmosdb update, but it overrides the current IPs in the firewall.

I'm using MacOS so I can get my IP through curl ifconfig.me.

How can I add my IP to the firewall without overriding the current IPs?


Solution

  • You can simply GET the list with az cosmosdb list and append your IP to a list, and cosmosdb update with that list:

    DESIRED_IP=$(curl ifconfig.me)
    CURRENT_IPS=$(az cosmosdb list | jq -r '.[0].ipRules | .[] | .ipAddressOrRange' | paste -sd "," -)
    DESIRED_IPS=$CURRENT_IPS,$DESIRED_IP
    az cosmosdb update -n .. -g .. --ip-range-filter "$DESIRED_IPS"