在 Terraform 中为 Azure 存储帐户创建事件订阅

Sou*_*jan 4 azure terraform terraform-provider-azure

我正在尝试使用TerraformTerraform provider for Azure在 Azure 中创建以下资源。

  • 为 Blob 存储创建存储帐户。
  • 创建将引发 Blob 活动事件的事件订阅。

运行 terraform 脚本时出现以下错误

错误:创建/更新 EventGrid 事件订阅“evtFileReceived”时出错(范围“/subscriptions/c17cf5ee-d3d7-4f64-b863-f2a4d6948594/resourceGroups/dominos-doodle”):eventgrid.EventSubscriptionsClient#CreateOrUpdate:发送请求失败:StatusCode=400 - - 原始错误:代码 =“InvalidRequest”消息 =“指定的主题属性与事件订阅范围中的预期主题不匹配。”

我该如何解决它?谷歌搜索没有给出任何结果。

生成错误的脚本如下。引发错误的步骤是terraform apply

显然,一种方法是使用 ARM 模板来实现此目的,但我正在尝试查看是否可以使用本机 Terraform 脚本创建它。我参考了Terraform Docs并创建了以下内容。

variable "inp_resource_group_name" { }
variable "inp_geo_location" { }
variable "inp_account_name" { }
variable "inp_az_subscription_id" { }
variable "inp_resource_group_id" { }

resource "azurerm_storage_account" "cave" {
  name                     = var.inp_account_name
  resource_group_name      = var.inp_resource_group_name
  location                 = var.inp_geo_location
  account_tier             = "Standard"
  account_replication_type = "LRS"
  account_kind             = "StorageV2"
}

resource "azurerm_storage_container" "validName" {
  name                  = validName"
  resource_group_name   = var.inp_resource_group_name
  storage_account_name  = var.inp_account_name
  container_access_type = "blob"
}

resource "azurerm_eventgrid_event_subscription" "evtFileReceived" {
  name  = "evtFileReceived"
  scope = var.inp_resource_group_id
  topic_name="/subscriptions/${var.inp_az_subscription_id}/resourceGroups/${var.inp_resource_group_name}/providers/Microsoft.Storage/storageAccounts/{var.inp_account_name}"
  webhook_endpoint {
    url = "https://myendpoint.that.works.well.across.all.osi.layers"
  }
}
Run Code Online (Sandbox Code Playgroud)

Rob*_*olo 5

我遇到了类似的问题,并通过将范围和 topic_name 设置为存储帐户 ID 来解决它。所以在你的例子中,我认为这应该有效;

resource "azurerm_eventgrid_event_subscription" "evtFileReceived" {
  name  = "evtFileReceived"
  scope = azurerm_storage_account.cave.id
  topic_name = azurerm_storage_account.cave.id
  webhook_endpoint {
    url = "https://myendpoint.that.works.well.across.all.osi.layers"
  }
}
Run Code Online (Sandbox Code Playgroud)