Search code examples
azureazure-rm-template

Dynamically add CORS allowedOrigins based on condition Azure Arm template


I am trying to add allowedOrigin dynamically based on prod or non prod environments. Have tried few ways, but none of these are working. For Prod, we want only 3 domains to be allowed, but for lower environments we want to allow any origins. Could someone please help.

"variables": { 
   "corsAllowedUrls": [
        "https://www.example.com",
        "https://app2.example.com",
        "https://m2.example.com"
    ]
 }
 .......
 "type": "Microsoft.Web/sites",
 "siteConfig": {
      "cors": {
        "allowedOrigins": [
          "[if(equals(parameters('azureEnvironment'),'prod'), variables('corsAllowedUrls'), '*')]"
        ]
      },

Second Option, this works for the prod, but for other environments, it add three rows, which randomly works and randomly don't work.

"siteConfig": {
      "cors": {
        "allowedOrigins": [
          "[if(equals(parameters('azureEnvironment'),'prod'), 'https://ww2.example.com', '*')]",
          "[if(equals(parameters('azureEnvironment'),'prod'), 'https://app2.example.com', '')]",
          "[if(equals(parameters('azureEnvironment'),'prod'), 'https://m2.example.com', '')]"
        ]
      }

I have also tried by sending a comma separated string and using the split function. The ideal could have been to have a wild card, but it seems these are not supported in Azure.

Any help is appreciated.


Solution

  • Please try this :

    "variables": { 
      "corsAllowedProdUrls": [
        "https://www.example.com",
        "https://app2.example.com",
        "https://m2.example.com"
      ],
      "corsAllowedDevUrls": [
        "*"
      ]
    }
    .......
    "type": "Microsoft.Web/sites",
    "siteConfig": {
      "cors": {
        "allowedOrigins": "[if(equals(parameters('azureEnvironment'),'prod'), variables('corsAllowedProdUrls'), variables('corsAllowedDevUrls'))]"
      },