使用 Terraform 按 Microsoft.Storage.BlobCreated 事件创建 Azure 事件订阅过滤

Creating an Azure event-subscription filtering by Microsoft.Storage.BlobCreated event using Terraform

使用 Terraform Azure 提供程序 2.19.0 的这个 Azure Cli 示例 (from Azure doc) 的等价物是什么

az eventgrid resource event-subscription create -g myResourceGroup \
--provider-namespace Microsoft.Storage --resource-type storageAccounts \
--resource-name myblobstorage12345 --name myFuncSub  \
--included-event-types Microsoft.Storage.BlobCreated \
--subject-begins-with /blobServices/default/containers/images/blobs/ \
--endpoint https://mystoragetriggeredfunction.azurewebsites.net/runtime/webhooks/eventgrid?functionName=imageresizefunc&code=<key>

注意: 在此之后 Terraform Github Issueresource_group_nametopic_name 已弃用。

恐怕您使用了错误的 Azure CLI 命令,只有事件网格的 CLI 命令,如 az eventgrid event-subscription create,它没有资源组的参数。所以你在Terraform代码中也不需要关心它。

更新:

更注意参数scope:

Specifies the scope at which the EventGrid Event Subscription should be created.

您也可以从参数--source-resource-id Azure CLI 命令中了解它:

--source-resource-id

Fully qualified identifier of the Azure resource to which the event subscription needs to be created.

Terraform没有描述清楚。所以我们需要从 CLI 或 Azure REST 来理解它API,这是你在 Azure 中创建资源时最终使用的实际东西。

而且 Terraform 也不支持 Azure 支持的所有东西。有时我也对 Terraform 感到困惑。这个时候,我推荐你直接使用CLI命令或者运行Terraform代码里面的CLI命令。

所以看起来 Terraform 正在使用范围参数来推断部分参数。

所以这在 Terraform 中是等价的:

resource "azurerm_eventgrid_event_subscription" "my_func_sub" {
  name  = "myFuncSub"
  scope = azurerm_storage_account.images.id

  included_event_types = [
    "Microsoft.Storage.BlobCreated"
  ]

  subject_filter {
    subject_begins_with = "/blobServices/default/containers/${azurerm_storage_container.images.name}/blobs/"
  }

  webhook_endpoint {
    url = "https://mystoragetriggeredfunction.azurewebsites.net/runtime/webhooks/eventgrid?functionName=imageresizefunc&code=<key>"
  }

}

当然,您需要将 azurerm_storage_container.images 和 webhook url 替换为适合您的情况的值。

重要的是要注意scope。它应该是将发布事件的资源的 id。在我们的例子中,它是一个存储容器。