【问题标题】:Creating an event subscription for Azure storage account in Terraform在 Terraform 中为 Azure 存储帐户创建事件订阅
【发布时间】:2020-04-29 06:43:24
【问题描述】:

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

  • 为 Blob 存储创建一个存储帐户。
  • 创建一个事件订阅,该订阅将引发有关 blob 活动的事件。

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

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

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

产生错误的脚本如下。抛出错误的步骤是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"
  }
}

【问题讨论】:

    标签: azure terraform terraform-provider-azure


    【解决方案1】:

    根据错误信息,表明资源azurerm_eventgrid_event_subscription中的topic_name属性与事件订阅scope中预期的主题不匹配。

    在这种情况下,应在存储帐户级别创建范围,因为主题与存储帐户资源相关联。它会是这样的:

    resource "azurerm_eventgrid_event_subscription" "evtFileReceived" {
      name  = "evtFileReceived"
      scope = ${azurerm_storage_account.cave.id}
      topic_name="/subscriptions/${var.inp_az_subscription_id}/resourceGroups/${var.inp_resource_group_name}/providers/Microsoft.Storage/storageAccounts/{azurerm_storage_account.cave.name}"
      webhook_endpoint {
        url = "https://myendpoint.that.works.well.across.all.osi.layers"
      }
    }
    

    或者,参考这个GitHub issue,你可以使用 scope 和 id eventgrid 主题。

    意识到这种情况下的资源组是一个主题的艺术 订阅类型,而不是创建订阅的参考 资源。似乎“topic_name”和“resource_group_name”是 不推荐使用的参数。使用“范围”代替 eventgrid 主题。

    会是这样的:

    resource "azurerm_eventgrid_topic" "example" {
      name                = "my-eventgrid-topic"
      location            = "${azurerm_resource_group.default.location}"
      resource_group_name = "${azurerm_resource_group.default.name}"
    
    }
    
    resource "azurerm_eventgrid_event_subscription" "evtFileReceived" {
      name  = "evtFileReceived"
      scope = "${azurerm_eventgrid_topic.example.id}"
    
     webhook_endpoint {
            url = "https://myendpoint.that.works.well.across.all.osi.layers"
          }
    
    }
    

    如果这可行或需要进一步帮助,请告诉我。

    【讨论】:

    • 嗨,在这种情况下,事件网格订阅/事件网格主题在哪里与存储帐户相关联。我的兴趣是在
    【解决方案2】:

    我遇到了类似的问题,并通过将范围和 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"
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-11-07
      • 1970-01-01
      • 2020-02-22
      • 2020-07-18
      • 2020-11-09
      • 1970-01-01
      • 2021-10-29
      • 2016-07-17
      • 2016-08-16
      相关资源
      最近更新 更多