【问题标题】:How to add AzureFunction as Event Subscription Endpoint in ARM Template?如何在 ARM 模板中添加 AzureFunction 作为事件订阅端点?
【发布时间】:2020-04-15 17:34:01
【问题描述】:

我编写了一个 ARM 模板来在现有存储帐户上创建事件订阅,我需要在其中侦听特定 blob 容器的 blob.created 和 blob.deleted 事件,并将它们发送到 Azure eventgrid 触发器函数应用程序。

functionapp 已经部署在 Azure https://myfunctionapp.azurewebsites.net 中,我很难尝试通过 ARM 模板创建事件订阅。请注意,我使用 API 版本 2020-01-01-preview 以将 AzureFunction 作为端点。模板如下:

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "appName": {
        "type": "string",
        "metadata": {
            "description": "Name of the function app"
        }
    },
    "functionName": {
        "type": "string",
        "defaultValue": "MyFunction",
        "metadata": {
            "description": "Name of the function"
        }
    },
    "eventSubName": {
        "type": "string",
        "defaultValue": "myfunctionappsub",
        "metadata": {
            "description": "The name of the event subscription to create."
        }
    },
    "storageName": {
        "type": "string",
        "metadata": {
            "description": "Storage account name"
        }
    },
    "location": {
        "type": "string",
        "defaultValue": "[resourceGroup().location]",
        "metadata": {
            "description": "Storage account location"
        }
    },
    "containerNamefilter": {
        "type": "string",
        "defaultValue": "-inputblob",
        "metadata": {
            "description": "Container name filter"
        }
    }
},
"variables": {
    "functionAppName": "[resourceId('Microsoft.Web/sites/functions/', parameters('appName'), parameters('functionName'))]"
},
"resources": [
    {
        "type": "Microsoft.Storage/storageAccounts/providers/eventSubscriptions",
        "name": "[concat(parameters('storageName'), '/Microsoft.EventGrid/', parameters('eventSubName'))]",
        "apiVersion": "2020-01-01-preview",
        "dependsOn": [
            "[parameters('storageName')]"
        ],
        "properties": {
            "destination": {
                "endpointType": "AzureFunction",
                "properties": {
                    "resourceId": "[variables('functionAppName')]"
                }
            },
            "filter": {
                "subjectBeginsWith": "",
                "subjectEndsWith": "",
                "isSubjectCaseSensitive": false,
                "includedEventTypes": [
                    "Microsoft.Storage.BlobCreated",
                    "Microsoft.Storage.BlobDeleted"
                ],
                "advancedFilters": [
                    {
                        "key": "subject",
                        "operatorType": "StringContains",
                        "value": "[parameters('containerfilter')]"
                    }
                ]
            }
        }
    }
]
}

这是我尝试在管道上运行它时遇到的错误:

2020-04-15T11:09:11.5347864Z Starting template validation.
2020-04-15T11:09:11.5368215Z Deployment name is azuredeploy-xxxxxxx-xxxxxx-xxxx
2020-04-15T11:09:13.1700166Z Template deployment validation was completed successfully.
2020-04-15T11:09:13.1700897Z Starting Deployment.
2020-04-15T11:09:13.1703528Z Deployment name is azuredeploy-xxxxxxx-xxxxxx-xxxx
2020-04-15T11:10:02.5842880Z There were errors in your deployment. Error code: DeploymentFailed.
2020-04-15T11:10:02.5893091Z ##[error]At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.
2020-04-15T11:10:02.5910677Z ##[error]Details:
2020-04-15T11:10:02.5915877Z ##[error]Conflict: {
  "status": "Failed",
  "error": {
    "code": "ResourceDeploymentFailure",
    "message": "The resource operation completed with terminal provisioning state 'Failed'.",
    "details": [
      {
        "code": "Internal error",
        "message": "The operation failed due to an internal server error. The initial state of the impacted resources (if any) are restored. Please try again in few minutes. If error still persists, report 72c636d4-6d09-4c50-8886-7153ddf2a4ee:4/15/2020 11:09:50 AM (UTC) to our forums for assistance or raise a support ticket ."
      }
    ]
  }
}
2020-04-15T11:10:02.5918976Z ##[error]Task failed while creating or updating the template deployment.
2020-04-15T11:10:02.5953000Z ##[section]Finishing: Create or update eventsubscription in RG

我在这里做错了吗?我对 ARM 模板非常陌生。

【问题讨论】:

标签: azure azure-functions arm-template azure-eventgrid


【解决方案1】:

如果您想为现有存储帐户创建事件网格订阅,请参考以下模板

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {

    "functionGroup": {
        "type": "string",
        "defaultValue" : "jimtest",
        "metadata": {
            "description": "he group name of  function app"
        }
    },
    "appName": {
        "type": "string",
        "defaultValue" : "testfunjs",
        "metadata": {
            "description": "Name of the function app"
        }
    },
    "functionName": {
        "type": "string",
        "defaultValue": "EventGridTrigger1",
        "metadata": {
            "description": "Name of the function"
        }
    },
    "eventSubName": {
        "type": "string",
        "defaultValue": "myfunctionappsub",
        "metadata": {
            "description": "The name of the event subscription to create."
        }
    },
    "storageName": {
        "type": "string",
        "defaultValue" : "andyprivate",
        "metadata": {
            "description": "Storage account name"
        }
    },
    "location": {
        "type": "string",
        "defaultValue": "[resourceGroup().location]",
        "metadata": {
            "description": "Storage account location"
        }
    },
    "containerNamefilter": {
        "type": "string",
        "defaultValue": "test",
        "metadata": {
            "description": "Container name filter"
        }
    }
},
"variables": {
    "functionId" :"[resourceId(parameters('functionGroup'),'Microsoft.Web/sites/functions/', parameters('appName'), parameters('functionName'))]",  
},
"resources": [
{
        "type": "Microsoft.Storage/storageAccounts/providers/eventSubscriptions",
        "name": "[concat(parameters('storageName'), '/Microsoft.EventGrid/', parameters('eventSubName'))]",
        "apiVersion": "2020-01-01-preview",
        "properties": {
            "destination": {
                "endpointType": "AzureFunction",
                "properties": {
                    "resourceId": "[variables('functionId')]"
                }
            },
            "filter": {
                "subjectBeginsWith": "",
                "subjectEndsWith": "",
                "isSubjectCaseSensitive": false,
                "includedEventTypes": [
                    "Microsoft.Storage.BlobCreated",
                    "Microsoft.Storage.BlobDeleted"
                ],
                "advancedFilters": [
                    {
                        "key": "subject",
                        "operatorType": "StringContains",
                        "value": "[parameters('containerNamefilter')]"
                    }
                ]
            }
        }
    }

]
}

【讨论】:

  • 非常感谢@JimXu,它成功了!但我注意到我添加的过滤器没有设置。它显示了键和运算符,但未设置值。我还应该在模板中添加什么来获取过滤器吗?
  • 通过添加 values 数组而不是单个 value 来修复它。谢谢!在此处为需要它的任何人添加 azure 文档参考。 link
猜你喜欢
  • 2020-05-29
  • 1970-01-01
  • 2019-11-05
  • 2017-03-28
  • 1970-01-01
  • 1970-01-01
  • 2018-04-19
  • 2019-12-13
  • 1970-01-01
相关资源
最近更新 更多