【问题标题】:Terraform azurerm_eventgrid_event_subscription dynamic moduleTerraform azurerm_eventgrid_event_subscription 动态模块
【发布时间】:2021-05-12 09:29:28
【问题描述】:

我正在尝试使用webhook_endpointservice_bus_queue_endpoint_id 为terraform 资源azurerm_eventgrid_event_subscription 创建一个可重用模块

这是我的资源

resource "azurerm_eventgrid_event_subscription" "eventgrid_event_subscription" {
  for_each = { for event_grid_subscription in var.event_grid_subscriptions : event_grid_subscription.eventgrid_subscription_name => event_grid_subscription }

  name                  = lookup(each.value, "eventgrid_subscription_name")
  scope                 = azurerm_eventgrid_topic.eventgrid_topic.id
  event_delivery_schema = var.eventgrid_input_schema

  service_bus_queue_endpoint_id = lookup(each.value, "service_bus_queue_endpoint_id", null)

  webhook_endpoint {
    max_events_per_batch              = lookup(each.value, "max_events_per_batch", null)
    preferred_batch_size_in_kilobytes = lookup(each.value, "preferred_batch_size_in_kilobytes", null)
    url                               = lookup(each.value, "endpoint_url", null)
  }

  included_event_types = lookup(each.value, "included_event_types", null)

  retry_policy {
    max_delivery_attempts = var.max_delivery_attempts
    event_time_to_live    = var.event_time_to_live
  }

  storage_blob_dead_letter_destination {
    storage_account_id          = var.storage_account_id
    storage_blob_container_name = var.storage_blob_container_name
  }
}

模块代码

module "eventgrid" {
  source = "../eventgrid" 

  eventgrid_topic_name = "${var.project_prefix}-${var.integration}-egt-${var.environment}-${local.location_abbreviation}"
  resource_group_name  = module.resource_group.resource_group_name
  location             = module.resource_group.resource_group_location

  event_grid_subscriptions = [
    {
      eventgrid_subscription_name = "${var.project_prefix}-${var.integration}-commercetools-algolia-sync-${var.environment}-${local.location_abbreviation}"
      endpoint_url                = "https://${local.function_app_name}.azurewebsites.net/api/commercetools-algolia-sync?code=${data.azurerm_function_app_host_keys.function_app_host_keys.default_function_key}"
      max_events_per_batch              = var.max_events_per_batch
      preferred_batch_size_in_kilobytes = var.preferred_batch_size_in_kilobytes
    },
    {
      eventgrid_subscription_name = "${var.project_prefix}-${var.integration}-order-created-email-${var.environment}-${local.location_abbreviation}"
      endpoint_url                = "https://${local.function_app_name}.azurewebsites.net/api/order-created-email?code=${data.azurerm_function_app_host_keys.function_app_host_keys.default_function_key}"
      max_events_per_batch              = var.max_events_per_batch
      preferred_batch_size_in_kilobytes = var.preferred_batch_size_in_kilobytes
    },
    {
      eventgrid_subscription_name   = "${var.project_prefix}-${var.integration}-order-created-${var.environment}-${local.location_abbreviation}"
      service_bus_queue_endpoint_id = module.servicebus.servicebus_queue_ids[0]
    },
  ]

  storage_account_id          = module.storage_account.storage_account_id
  storage_blob_container_name = module.storage_container_dlq.storage_container_name

  tags = local.tags

  depends_on = [
    module.function_app
  ]
}

模块代码在块中混合了 webhook 端点和 servicebus 队列 id

这仅适用于 webhook,但是当我添加到 servicebus 端点时,我得到了错误

Error: "webhook_endpoint.0.url": required field is not set

有没有办法使用相同的资源并使用来自同一模块块的 webhook 端点或 servicebus 队列 id 创建订阅?在我调用 servicebus 队列而不是 webhook 端点的部分中。它仍在尝试创建 webhook 端点订阅者,即使它为空

【问题讨论】:

  • 使用条件为dynamic "webhook_endpoint" { for_each = lookup(each.value, "webhook_endpoint_url") != null ? [1] : [] content { max_events_per_batch = lookup(each.value, "max_events_per_batch", var.max_events_per_batch) preferred_batch_size_in_kilobytes = lookup(each.value, "preferred_batch_size_in_kilobytes", var.preferred_batch_size_in_kilobytes) url = lookup(each.value, "webhook_endpoint_url") } }的动态块

标签: azure terraform terraform-provider-azure


【解决方案1】:

也许,当您使用带条件的动态块时有一个错字,请尝试将webhook_endpoint_url替换为endpoint_url

dynamic "webhook_endpoint" {     
  for_each = lookup(each.value, "endpoint_url") != null ? [1] : []     
  content {       
    max_events_per_batch              = lookup(each.value, "max_events_per_batch", null)       
    preferred_batch_size_in_kilobytes = lookup(each.value, "preferred_batch_size_in_kilobytes", null)       
    url                               = lookup(each.value, "endpoint_url", null)   
    }   
    }

【讨论】:

    猜你喜欢
    • 2020-08-18
    • 2022-01-14
    • 1970-01-01
    • 2021-06-12
    • 2020-12-25
    • 2021-09-24
    • 1970-01-01
    • 2022-07-07
    相关资源
    最近更新 更多