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.6k stars 4.64k forks source link

azurerm_eventgrid_event_subscription creation with azure function endpoint returns internal server error #10569

Open PetkoGotsov opened 3 years ago

PetkoGotsov commented 3 years ago

Community Note

Terraform (and AzureRM Provider) Version

TF.ver: 0.0.142 provider.ver: 2.21.0

Affected Resource(s)

azurerm_eventgrid_event_subscription_resource

Terraform Configuration Files

#event subscription azure function
resource "azurerm_function_app" "subscriptionfunction" {
    name = "${var.resourceprefix}-${var.environment}-sub-function"
    location = module.resource_group.location
    resource_group_name = module.resource_group.name
    app_service_plan_id = "${azurerm_app_service_plan.asp.id}"
    storage_connection_string = "${azurerm_storage_account.azure_function_storage.primary_connection_string}"
    version = "~2"

    app_settings = {
        https_only = true
        FUNCTIONS_WORKER_RUNTIME = "dotnet"
        FUNCTION_APP_EDIT_MODE = "readonly"
    HASH = "${base64encode(filesha256("${var.subscription_azure_function_zip}"))}"
        WEBSITE_RUN_FROM_PACKAGE = "https://${azurerm_storage_account.azure_function_storage.name}.blob.core.windows.net/${azurerm_storage_container.deployments.name}/${azurerm_storage_blob.subscriptionappcode.name}${data.azurerm_storage_account_sas.sas.sas}"
    }
}
#event grid custom topic
resource "azurerm_eventgrid_topic" "topic" {
  name                = "${var.resourceprefix}-topic"
  location            = module.resource_group.location
  resource_group_name = module.resource_group.name
}
#event grid topic event subscription
resource "azurerm_eventgrid_event_subscription" "topicsubscription" {
  name  = "${var.resourceprefix}-subscription"
  scope = "${azurerm_eventgrid_topic.topic.id}"
  azure_function_endpoint {
    function_id = "${azurerm_function_app.subscriptionfunction.id}/functions/EventSubscriptionAzureFunction"
  }
  storage_blob_dead_letter_destination {
    storage_account_id = "${azurerm_storage_account.azure_function_storage.id}"
    storage_blob_container_name = "${azurerm_storage_container.deadletterevents.name}"
  }
}

Expected Behaviour

A event subscription should be created for the function app with an AzureFunction endpoint type to the domain grid topic.

Actual Behaviour

Terraform errors with the following:

*Error: Error waiting for EventGrid Event Subscription "systemmanager-subscription" (Scope "/subscriptions//resourceGroups/systemmanager-dev/providers/Microsoft.EventGrid/topics/systemmanager-topic") to become available: Code="Internal error" Message="The operation failed due to an internal server error. The initial state of the impacted resources (if any) are restored. Please try again in few minutes. If error still persists, report 42b51f3d-c8b9-4fbf-9307-eb384032f065:2/12/2021 2:48:24 PM (UTC) to our forums for assistance or raise a support ticket ."**

I can confirm that the event source resource id is the one that is present in the */subscriptions//resourceGroups/systemmanager-dev/providers/Microsoft.Web/sites/systemmanager-dev-sub-function/functions/EventSubscriptionAzureFunction** properties and that by the time terraform attempts to create the event subscription, the aforementioned azure function is already deployed and running (accepts default validation request and returns validationResponse).

tohov commented 3 years ago

@PetkoGotsov: I'm not 100% sure but I think your function (not function app, but the actual function that will subscribe to the topic) has to be EventGrid Trigger type, maybe?

image

PinakiKundu commented 1 year ago

I am having similar issue with different error

[error]

Error: waiting for creation/update of System Topic Event Subscription: (Event Subscription Name " " / System Topic Name " " / Resource Group " "): Code="Endpoint validation" Message="Destination endpoint not found. Resource details: resourceId: /subscriptions/ /resourceGroups/ /providers/Microsoft.Web/sites/ /functions/ . Resource should pre-exist before attempting this operation. Activity id:, timestamp: 3/21/2023 1:13:23 PM (UTC)."

Although the function under the function app is running fine: Screenshot 2023-03-21 185055

bnguyen-bgs commented 1 year ago

Also have this issue, and we suspect that Terraform sends the async request to Azure, returns with a 202 without waiting for Azure to return and fully stand up with the Function code, and assumes that the Function endpoint is ready but in reality it's still initializing when Terraform gets to the event_grid resource. It would make sense if there was a data resource for Function_App_Function so that the eventgrid resource would also assume that the function will be there.

Neutrino-Sunset commented 10 months ago

A two stage deployment as described here is one solution to this issue.

https://learn.microsoft.com/en-us/samples/azure-samples/azure-functions-event-grid-terraform/subscribing-an-azure-function-to-event-grid-events-via-terraform/