在创建事件订阅之前,请从ARM部署功能

时间:2020-06-14 18:44:44

标签: azure azure-functions azure-resource-manager azure-eventgrid

使用ARM我创建了function APPevent grid topic

现在,我想从同一ARM中创建对该主题的订阅,但是要实现此订阅,我需要先将函数部署到function App中。

是否可以从同一ARM部署功能?还是我必须拥有两个不同的ARM,一个用于创建除相关功能以外的所有内容(storagetopics),另一个用于创建需要部署功能的所有资源(event subscriptions) ?在第二种情况下,我将需要在中间部署功能。

我正在寻找一种ARM来从零开始部署完整的基础架构(包括functions中的function app)。这可能吗?怎么样?

1 个答案:

答案 0 :(得分:0)

根据我的测试,我们可以在一个模板中创建这些资源。例如

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "eventGridTopicName": {
      "type": "string",
      "defaultValue": "EventGridTopic",
      "metadata": {
        "description": "The name of the Event Grid custom topic."
      }
    },
    "eventGridSubscriptionName": {
      "type": "string",
      "defaultValue": "EventGridSub",
      "metadata": {
        "description": "The name of the Event Grid custom topic's subscription."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "The location in which the Event Grid resources should be deployed."
      }
    },
    "appName": {
      "type": "string",
      "defaultValue": "Func",
      "metadata": {
        "description": "The name of the function app that you wish to create."
      }
    },
    "functionName": {
      "type": "string",
      "defaultValue": "EventGridFunction",
      "metadata": {
        "description": "Function App Name"
      }
    },
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS"
      ],
      "metadata": {
        "description": "Storage Account type"
      }
    },
    "runtime": {
      "type": "string",
      "defaultValue": "dotnet",
      "allowedValues": [
        "node",
        "dotnet",
        "java"
      ],
      "metadata": {
        "description": "The language worker runtime to load in the function app."
      }
    }
  },
  "variables": {
    "eventGridTopic": "[concat(uniquestring(resourceGroup().id),parameters('EventGridTopicName'))]",
    "eventGridSub": "[concat(uniquestring(resourceGroup().id),parameters('eventGridSubscriptionName'))]",
    "functionUrl": "[concat('https://', variables('FunctionAppName'),'.azurewebsites.net/runtime/webhooks/eventgrid?functionName=', parameters('FunctionName'),'&code=')]",
    "functionAppName": "[concat(uniquestring(resourceGroup().id),parameters('appName'))]",
    "hostingPlanName": "[parameters('appName')]",
    "applicationInsightsName": "[parameters('appName')]",
    "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'azfunction')]",
    "functionWorkerRuntime": "[parameters('runtime')]"
  },
  "resources": [
   // create event topic
    {
      "name": "[variables('eventGridTopic')]",
      "type": "Microsoft.EventGrid/topics",
      "location": "[parameters('location')]",
      "apiVersion": "2020-04-01-preview",

    },
    // create event topic subscrition
    {
      "name": "[concat(variables('eventGridTopic'), '/Microsoft.EventGrid/', variables('eventGridSub'))]",
      "type": "Microsoft.EventGrid/topics/providers/eventSubscriptions",
      "location": "[parameters('location')]",
      "apiVersion": "2020-04-01-preview",
        "dependsOn": [
          "[variables('eventGridTopic')]",
          "[resourceId('Microsoft.Web/sites/functions/', variables('functionAppName'), parameters('functionName'))]"

        ],
      "properties": {
        "destination": {
          "endpointType": "AzureFunction",
          "properties": {
            "resourceId": "[resourceId('Microsoft.Web/sites/functions/', variables('functionAppName'), parameters('functionName'))]"
          }
        },
          "filter": {
          }
        }
      },
     // create storage account
      {
        "type": "Microsoft.Storage/storageAccounts",
        "name": "[variables('storageAccountName')]",
        "apiVersion": "2016-12-01",
        "location": "[parameters('location')]",
        "kind": "Storage",
        "sku": {
          "name": "[parameters('storageAccountType')]"
        }
      },
      // create host plan
      {
        "type": "Microsoft.Web/serverfarms",
        "apiVersion": "2018-02-01",
        "name": "[variables('hostingPlanName')]",
        "location": "[parameters('location')]",
        "sku": {
          "name": "Y1",
          "tier": "Dynamic"
        },
        "properties": {
          "name": "[variables('hostingPlanName')]",
          "computeMode": "Dynamic"
        }
      },
      // create function app
      {
        "apiVersion": "2015-08-01",
        "type": "Microsoft.Web/sites",
        "name": "[variables('functionAppName')]",
        "location": "[parameters('location')]",
        "kind": "functionapp",
        "dependsOn": [
          "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
          "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]",
          "[concat('microsoft.insights/components/', variables('applicationInsightsName'))]"
        ],
        "properties": {
          "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
          "siteConfig": {
            "appSettings": [
              {
                "name": "AzureWebJobsStorage",
                "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')),'2015-05-01-preview').key1)]"
              },
              {
                "name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
                "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')),'2015-05-01-preview').key1)]"
              },
              {
                "name": "WEBSITE_CONTENTSHARE",
                "value": "[toLower(variables('functionAppName'))]"
              },
              {
                "name": "FUNCTIONS_EXTENSION_VERSION",
                "value": "~3"
              },
              {
                "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                "value": "[reference(resourceId('microsoft.insights/components/', variables('applicationInsightsName')), '2015-05-01').InstrumentationKey]"
              },
              {
                "name": "FUNCTIONS_WORKER_RUNTIME",
                "value": "[variables('functionWorkerRuntime')]"
              }

            ]
          }
        }
      },
      // create event gride trigger function
      {
       "apiVersion": "2019-08-01",
      "name": "[concat(variables('functionAppName'),'/', parameters('functionName'))]",
      "type": "Microsoft.Web/sites/functions",
      "dependsOn":[ "[variables('functionAppName')]"],
      "properties": {
        "config": {
          "bindings": [
            {
              "name": "eventGridEvent",

              "direction": "in",
              "type": "eventGridTrigger"
            }
          ]

        },
        "files":{
          // the code
           "run.csx":"#r \"Microsoft.Azure.EventGrid\"\r\nusing Microsoft.Azure.EventGrid.Models;\r\n\r\npublic static void Run(EventGridEvent eventGridEvent, ILogger log)\r\n{\r\n    log.LogInformation(eventGridEvent.Data.ToString());\r\n}"
          }
       }
      },
      // create application insight
      {
        "apiVersion": "2018-05-01-preview",
        "name": "[variables('applicationInsightsName')]",
        "type": "microsoft.insights/components",
        "location": "East US",
        "tags": {
          "[concat('hidden-link:', resourceGroup().id, '/providers/Microsoft.Web/sites/', variables('applicationInsightsName'))]": "Resource"
        },
        "properties": {
          "ApplicationId": "[variables('applicationInsightsName')]",
          "Request_Source": "IbizaWebAppExtensionCreate"
        }
      }
    ],
    "outputs": {

    }
  }

enter image description here 有关更多详细信息,请参阅blog