Search code examples
azure-rm-templateazure-cosmosdb-sqlapi

How to configure preferred region list in ARM template for COSMOS DB?


I have a Cosmos DB set up in two regions "A" and "B" where "A" is the write region. Recently "A" region failed and no reads were served even though "B" region was on.
It makes sense because the PreferredLocations is not set as explained in here or here.
The problem is that I don't want to set ConnectionPolicy.PreferredLocations in code since the exact locations are set in ARM templates.
How do I add the PreferredLocations properties in ARM template?


Solution

  • You don't set Preferred Locations in an ARM template. This is set in the Cosmos client SDK when you initialize a new client in your application. In an ARM template you set the failover priority, which will direct the service to specify which region to promote to the write region should a write region fail when automatic failover is set in your Cosmos account. Failover priority in an ARM template is set by the order in which regions are specified in the ARM template. Example is below.

    "locations": [
        {
            "locationName": "[parameters('primaryRegion')]",
            "failoverPriority": 0,
            "isZoneRedundant": false
        },
        {
            "locationName": "[parameters('secondaryRegion')]",
            "failoverPriority": 1,
            "isZoneRedundant": false
         }
    ]
    

    To specify preferred locations you do this when initializing a new SDK client instance. Example for this is below.

    CosmosClientOptions options = new CosmosClientOptions
    {
        ApplicationPreferredRegions = new List<string>
        {
            "West US",
            "East US",
            "Central US"
        }
    };
    
    CosmosClient client = new CosmosClient(connection, options );
    

    PS: we have fairly details docs on this service. You should refer to these as much as possible rather than 4 year old docs written by a third party as the service and SDK have drastically changed. You can find our docs here