Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Functions: how to set CORS via automation?

I have an azure function app that I would like to set up in repeatable (automated) manner so that I can duplicate it in different environments/resource groups. I'm able to create the function app via the azure cli, but I also need to configure the CORS options such that I can call it from a browser.

I've found where to do that in the azure portal web ui, in the 'Platform Features' tab(https://docs.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings#cors), but I can't find anything about modifying that setting via azure cli, or by the VSTS deployment task that I've set up to do releases when I change the functions in the app.

It seems you can even specify the CORS setting for local development via the local.settisg.json, but that only applies locally (https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local#local-settings). Were I deploying the app via the azure function tools cli I could supposedly specify the --publish-local-settings flag when I deploy, but I'm not deploying that way.

It seems like there must be a way to modify the CORS configuration without using the web UI, am I just not finding it?

like image 875
Bjornicus Avatar asked Oct 25 '17 00:10

Bjornicus


2 Answers

Fabio's answer is correct, Azure Resource Manager templates work for this. Since the example he linked to was about logic apps and not azure functions, the getting the template right required a few changes and I wanted to add some detail that may help others get there faster.

To craft the template I ended up downloading the automation template from the function app that I created manually, and then deleting stuff until I got to what I think is the minimum. Here's what I'm using:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "function_app_name": {
      "defaultValue": "my-function-app",
      "type": "string"
    }
  },
  "variables": {},
  "resources": [
    {
      "comments": "CORS allow origins *.",
      "type": "Microsoft.Web/sites/config",
      "name": "[concat(parameters('function_app_name'), '/web')]",
      "apiVersion": "2016-08-01",
      "properties": {
        "cors": {
          "allowedOrigins": [
            "*"
          ]
        }
      },
      "dependsOn": []
    }
  ]
}

I also have a parameters file that goes with this that looks like this:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "function_app_name": {
            "value": null
        }
    }
}

and then I have an Azure Resource Group Deployment step in my release definition that deploys this and substitutes the desired function app name depending on the environment I'm deploying to.

like image 197
Bjornicus Avatar answered Oct 07 '22 20:10

Bjornicus


To set CORS settings programatically, you want to use ARM.

Here's an example you can follow: https://msftplayground.com/2016/08/setting-api-definition-url-cors-value-arm/

like image 28
Fabio Cavalcante Avatar answered Oct 07 '22 19:10

Fabio Cavalcante