Azure event grid output binding using managed identity python v2, i keep getting 'EventGridAttribute.TopicEndpointUri'

91 views Asked by At

I'm using python v2 azure event grid output binding, using code from microsoft official documentation. I'm trying to use managed identity instead of the topic endpoint and keys. but here

@app.event_grid_output(
    arg_name="outputEvent",
    topic_endpoint_uri="MyEventGridTopicUriSetting",
    topic_key_setting="MyEventGridTopicKeySetting")

what should be done here? I keep getting the below error

Unable to resolve the value for property 'EventGridAttribute.TopicEndpointUri'.

import logging
import azure.functions as func
import datetime

@app.function_name(name="eventgrid_output")
@app.route(route="eventgrid_output")
@app.event_grid_output(
    arg_name="outputEvent",
    topic_endpoint_uri="MyEventGridTopicUriSetting",
    topic_key_setting="MyEventGridTopicKeySetting")
def eventgrid_output(eventGridEvent: func.EventGridEvent, 
         outputEvent: func.Out[func.EventGridOutputEvent]) -> None:

    logging.log("eventGridEvent: ", eventGridEvent)

    outputEvent.set(
        func.EventGridOutputEvent(
            id="test-id",
            data={"tag1": "value1", "tag2": "value2"},
            subject="test-subject",
            event_type="test-event-1",
            event_time=datetime.datetime.utcnow(),
            data_version="1.0"))
1

There are 1 answers

0
Pravallika KV On

To implement Event Grid Trigger Azure function using Managed Identity, you have to use the connection property.

Use the below function code:

app = func.FunctionApp()

@app.function_name(name="eventgrid_out")
@app.event_grid_trigger(arg_name="eventGridEvent")
@app.event_grid_output(
    arg_name="outputEvent",
connection="mytesteventgrid__topicEndpointUri")
def eventgrid_output(eventGridEvent: func.EventGridEvent, 
         outputEvent: func.Out[func.EventGridOutputEvent]) -> None:

    logging.info("eventGridEvent: %s", eventGridEvent)

    outputEvent.set(
        func.EventGridOutputEvent(
            id="test-id",
            data={"tag1": "value1", "tag2": "value2"},
            subject="test-subject",
            event_type="test-event-1",
            event_time=datetime.datetime.utcnow(),
            data_version="1.0"))

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "mytesteventgrid__topicEndpointUri":"https://<topic_name>.southindia-1.eventgrid.azure.net/api/events"
  }
}
  • Assign the roles EventGrid Contributor,EventGrid Data Senderto access Event Grid Topic at runtime when using Managed Identity.

Send the events:

enter image description here

enter image description here

References:

https://learn.microsoft.com/en-gb/answers/questions/1615439/does-eventgrid-output-binging-v2-support-managed-i