Event Hub Trigger with Managed Identity not reading events
I have created an Azure Event Hub and an Azure Function with a system-assigned Managed Identity. The Managed Identity has been granted the following roles on the Event Hub: "Azure Event Hubs Data Sender", "Azure Event Hubs Data Receiver", "Azure Event Hubs Data Owner". I have configured the following app settings:
-
EVENTHUB_NAME
: my_eventhub_name -
EventHubConnection__fullyQualifiedNamespace
: my_eventhub_namespace.servicebus.windows.net
Here is the code to write to the Event Hub, which works fine when invoked via HTTP trigger:
import azure.functions as func
import json
import logging
import os
from azure.eventhub import EventHubProducerClient, EventData
from azure.identity import DefaultAzureCredential
app = func.FunctionApp()
# Debugging to check environment variables
logging.info(f"EventHubConnection__fullyQualifiedNamespace: {os.environ.get('EventHubConnection__fullyQualifiedNamespace')}")
logging.info(f"EVENTHUB_NAME: {os.environ.get('EVENTHUB_NAME')}")
EVENTHUBNAMESPACE = os.environ["EventHubConnection__fullyQualifiedNamespace"]
EVENTHUBNAME = os.environ["EVENTHUB_NAME"]
credential = DefaultAzureCredential()
@app.function_name(name="myfunc1")
@app.route(route="myfunc1", auth_level=func.AuthLevel.FUNCTION)
def dora(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
try:
event_data = req.get_json()
event_json = json.dumps(event_data)
producer = EventHubProducerClient(
fully_qualified_namespace=EVENTHUBNAMESPACE,
eventhub_name=EVENTHUBNAME,
credential=credential,
)
with producer:
event_data_batch = producer.create_batch()
event_data_batch.add(EventData(event_json))
producer.send_batch(event_data_batch)
return func.HttpResponse(f"Message '{event_json}' sent successfully to Event Hub.", status_code=200)
except ValueError:
return func.HttpResponse("Please pass a valid JSON in the request body with a 'message' key.", status_code=400)
except Exception as e:
logging.error(f"Error sending message: {str(e)}")
return func.HttpResponse(f"Error sending message: {str(e)}", status_code=500)
However, when I deploy the following Event Hub trigger function, it does not read any events unless I replace EventHubConnection__fullyQualifiedNamespace
with a connection string in the app settings:
import azure.functions as func
import json
import logging
import os
from azure.eventhub import EventHubProducerClient, EventData
from azure.identity import DefaultAzureCredential
app = func.FunctionApp()
# Debugging to check environment variables
logging.info(f"EventHubConnection__fullyQualifiedNamespace: {os.environ.get('EventHubConnection__fullyQualifiedNamespace')}")
logging.info(f"EVENTHUB_NAME: {os.environ.get('EVENTHUB_NAME')}")
EVENTHUBNAMESPACE = os.environ["EventHubConnection__fullyQualifiedNamespace"]
EVENTHUBNAME = os.environ["EVENTHUB_NAME"]
credential = DefaultAzureCredential()
@app.function_name(name="eventhub")
@app.event_hub_message_trigger(
arg_name="azeventhub",
event_hub_name="EVENTHUB_NAME",
connection="EventHubConnection__fullyQualifiedNamespace",
consumer_group="function",
cardinality="one"
)
def eventhub_trigger(azeventhub: func.EventHubEvent):
logging.info('Python EventHub trigger processed an event: %s',
azeventhub.get_body().decode('utf-8'))
What could be causing the Event Hub trigger function to fail when using Managed Identity, and how can I resolve this?
Thank you.