You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用托管身份配置Azure Python V2 Event Grid输出绑定遇错求助

解决Azure Functions Python V2 Event Grid输出绑定托管身份替代密钥的报错问题

问题场景

使用Azure Python V2版本的Event Grid输出绑定,尝试用托管身份替代主题端点与密钥,不清楚如何修改@app.event_grid_output的配置,持续报错:

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

原代码片段:

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

完整原代码:

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. 为你的Azure Function启用系统分配或用户分配的托管身份
  2. 给该托管身份分配Event Grid Data Sender角色到目标Event Grid主题

代码配置修改

  • 移除topic_key_setting参数:托管身份模式下无需密钥验证
  • 确保topic_endpoint_uri配置正确:
    • 可直接填写Event Grid主题的完整端点URL(如https://your-topic-name.region.eventgrid.azure.net/api/events)
    • 或使用配置键名(需确保local.settings.json/云端应用配置中存在对应键值)

修改后的完整代码

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="https://<你的主题名称>.eastus-1.eventgrid.azure.net/api/events"
)
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"))

额外注意事项

  • 本地调试时:确保开发账号拥有目标Event Grid主题的发送权限;若使用用户分配身份,需在local.settings.json中添加AZURE_CLIENT_ID配置
  • 云端部署时:确认托管身份已正确启用,且Event Grid Data Sender角色分配生效,主题端点配置无误

内容的提问来源于stack exchange,提问作者Natasha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 02:25:12