首页 文章

Azure功能:如何通过自动化设置CORS?

提问于
浏览
2

我有一个天蓝色的功能应用程序,我想以可重复(自动)的方式设置,以便我可以在不同的环境/资源组中复制它 . 我可以通过azure cli创建功能应用程序,但我还需要配置CORS选项,以便我可以从浏览器调用它 .

我've found where to do that in the azure portal web ui, in the '平台功能选项卡(https://docs.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings#cors),但我可以't find anything about modifying that setting via azure cli, or by the VSTS deployment task that I'设置为在我更改应用程序中的功能时执行发布 .

您似乎甚至可以通过local.settisg.json为本地开发指定CORS设置,但这仅适用于本地(https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local#local-settings) . 如果我通过azure函数工具cli部署应用程序,我可以在部署时指定 --publish-local-settings 标志,但我没有采用这种方式部署 .

似乎必须有一种方法来修改CORS配置而不使用Web UI,我只是找不到它?

2 回答

  • 2

    Fabio的答案是正确的,Azure Resource Manager模板适用于此 . 由于他链接到的示例是关于逻辑应用程序而不是azure函数,因此正确获取模板需要进行一些更改,我想添加一些可以帮助其他人更快地实现目标的细节 .

    为了制作模板,我最终从我手动创建的功能应用程序中下载了自动化模板,然后删除了东西,直到我达到了我认为最低限度 . 这是我正在使用的:

    {
      "$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": []
        }
      ]
    }
    

    我还有一个参数文件,看起来像这样:

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

    然后我在我的发行版定义中有一个 Azure Resource Group Deployment 步骤,它会部署它并根据我正在部署的环境替换所需的功能应用名称 .

  • 2

    要以编程方式设置CORS设置,您需要使用ARM .

    以下是您可以遵循的示例:https://msftplayground.com/2016/08/setting-api-definition-url-cors-value-arm/

相关问题