hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.62k stars 4.66k forks source link

azurerm_eventgrid_system_topic_event_subscription is returning error "Failed to enable publisher notifications." for azure function endpoint #24839

Closed TBaxo closed 9 months ago

TBaxo commented 9 months ago

Is there an existing issue for this?

Community Note

Terraform Version

1.4.6

AzureRM Provider Version

3.90.0

Affected Resource(s)/Data Source(s)

azurerm_eventgrid_system_topic_event_subscription

Terraform Configuration Files

# module 1
resource "azurerm_resource_group" "rg" {
  name     = "${local.resource_prefix}-iothub-rg"
  location = var.location
}

resource "azurerm_iothub" "iot" {
  name                = "${local.resource_prefix}-device-iot"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location

  sku {
    name     = var.iothub_settings.sku_name
    capacity = var.iothub_settings.capacity
  }

  identity {
    type = "SystemAssigned"
  }

  tags                = var.environment_default_tags

  provisioner "local-exec" {
    command = "az iot dps linked-hub create --dps-name ${data.azurerm_iothub_dps.dps.name} --resource-group ${data.azurerm_iothub_dps.dps.resource_group_name} --hn ${azurerm_iothub.iot.name} --hrg ${azurerm_iothub.iot.resource_group_name} --allocation-weight ${var.dps_settings.link_settings.allocation_weight} --apply-allocation-policy ${var.dps_settings.link_settings.apply_allocation_policy}"
  }
}

resource "azurerm_eventgrid_system_topic" "iot" {
  name                   = "${local.resource_prefix}-iothub-egst"
  location               = azurerm_resource_group.rg.location
  resource_group_name    = azurerm_resource_group.rg.name
  source_arm_resource_id = azurerm_iothub.iot.id
  topic_type             = "Microsoft.Devices.IotHubs"

  identity {
    type = "SystemAssigned"
  }
}

# module 2
data azurerm_linux_function_app example {
  name                = var.example_function.name
  resource_group_name = var.example_function.resource_group_name
}

resource "azurerm_eventgrid_system_topic_event_subscription" "serverless" {
  name                = "${local.resource_prefix}-serverless-iot-evgs"
  resource_group_name = var.eventgrid_system_topic_resource_group_name
  system_topic        = var.eventgrid_system_topic_name

  azure_function_endpoint {
    function_id = "${data.azurerm_linux_function_app.example.id}/functions/IoTTelemetryTrigger"
    max_events_per_batch = 1
    preferred_batch_size_in_kilobytes = 64
  }

  retry_policy {
    event_time_to_live    = 1440
    max_delivery_attempts = 30
  }

  included_event_types = ["Device Telemetry"]

  advanced_filtering_on_arrays_enabled = true
}

Debug Output/Panic Output

│ Error: creating/updating System Topic Event Subscription (Subscription: "7f218e53-808c-45ef-bde7-5363734ceca2"
│ Resource Group Name: "sp-dev-uks-iothub-rg"
│ System Topic Name: "sp-dev-uks-iothub-egst"
│ Event Subscription Name: "sp-dev-uks-serverless-iot-evgs"): polling after SystemTopicEventSubscriptionsCreateOrUpdate: polling failed: the Azure API returned the following error:
│
│ Status: "Failed"
│ Code: "Publisher Notification Error"
│ Message: "Failed to enable publisher notifications."
│ Activity Id: ""
│
│ ---
│
│ API Response:
│
│ ----[start]----
│ {"id":"https://management.azure.com/subscriptions/7F218E53-808C-45EF-BDE7-5363734CECA2/providers/Microsoft.EventGrid/locations/uksouth/operationsStatus/A79CA9C7-5D23-406F-A4C9-B543E8CB8E64?api-version=2022-06-15","name":"a79ca
9c7-5d23-406f-a4c9-b543e8cb8e64","status":"Failed","error":{"code":"Publisher Notification Error","message":"Failed to enable publisher notifications.","details":[{"code":"Publisher Provider Error","message":"PUT request for htt
ps://management.azure.com/subscriptions/7f218e53-808c-45ef-bde7-5363734ceca2/resourceGroups/sp-dev-uks-iothub-rg/providers/microsoft.devices/iothubs/sp-dev-uks-device-iot/eventGridFilters/sp-dev-uks-serverless-iot-evgs?api-version=2018-01-15-preview failed with status code: BadRequest, code:  and message: ."}]}}
│ -----[end]-----
│
│
│   with azurerm_eventgrid_system_topic_event_subscription.serverless,
│   on main.tf line 6, in resource "azurerm_eventgrid_system_topic_event_subscription" "serverless":
│    6: resource "azurerm_eventgrid_system_topic_event_subscription" "serverless" {
│
╵

Expected Behaviour

It should create the subscription for the system topic provided

Actual Behaviour

Errors and doesn't create the event subscription

Steps to Reproduce

  1. type "yes"
  2. wait for error to occur

Important Factoids

Using IotHub System Topic

References

No response

TBaxo commented 9 months ago

Some extra info:

TBaxo commented 9 months ago

I'm an idiot. I just noticed that the specific error mentions the the specific resource it's trying to place in the url. In this case the eventGridFilters :

https://management.azure.com/subscriptions/7f218e53-808c-45ef-bde7-5363734ceca2/resourceGroups/sp-dev-uks-iothub-rg/providers/microsoft.devices/iothubs/sp-dev-uks-device-iot/eventGridFilters/sp-dev-uks-serverless-iot-evgs?api-version=2018-01-15-preview

After referring back to the original ARM template I noticed that I had messed up the filters, or in my case the included_event_types. So my terraform should have been this:

resource "azurerm_eventgrid_system_topic_event_subscription" "serverless" {
  name                = "${local.resource_prefix}-serverless-iot-evgs"
  resource_group_name = var.eventgrid_system_topic_resource_group_name
  system_topic        = var.eventgrid_system_topic_name

  azure_function_endpoint {
    function_id = "${data.azurerm_linux_function_app.example.id}/functions/IoTTelemetryTrigger"
    max_events_per_batch = 1
    preferred_batch_size_in_kilobytes = 64
  }

  retry_policy {
    event_time_to_live    = 1440
    max_delivery_attempts = 30
  }

  included_event_types = ["Microsoft.Devices.DeviceTelemetry"]

  advanced_filtering_on_arrays_enabled = true
}
github-actions[bot] commented 7 months ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.