【Azure Function App】在Function App中使用System Managed Identity访问Storage Account

简介: 本文介绍了如何在Azure Function中使用托管身份(Managed Identity)替代AzureWebJobsStorage连接函数应用到存储账户,以提高安全性并减少Access Key的使用。具体步骤包括:1) 启用系统分配的身份;2) 为函数应用授予存储访问权限,添加必要角色(如Storage Blob Data Contributor);3) 配置`AzureWebJobsStorage__blobServiceUri`参数指定Blob Service Uri。完成后删除旧配置,即可通过Managed Identity访问Storage Account。

问题描述

在Azure Function中,如何使用托管身份(Managed Identity) 而不是 AzureWebJobsStorage 来连接函数应用到存储账户?

原因是为了Storage Account更安全,减少Access Key的使用场景。或当Storage Account禁用了Access Key方式后。

 

问题解答

在函数应用中,通常使用应用设置 AzureWebJobsStorage 来配置连接字符串连接到Storage Account。

当在Storage Account中关闭了“Allow storage account key access”功能后。连接字符串也就无法正常工作。

 

本文介绍了如何使用 Azure Active Directory 身份而不是秘密或连接字符串来配置函数应用。使用身份可以避免意外泄露敏感秘密,并且可以更好地了解数据的访问方式。

 

以下是配置步骤

第一步:在函数应用中启用系统分配的身份并保存

 

第二步:为函数应用授予存储访问权限

在Storaeg Account的添加权限页面,搜索 Storage Blob Data Contributor 并选择它。

如果配置了 blob 触发的函数应用,重复步第二步。添加 Storage Queue Data Contributor 和 Storage Table Data Contributor 角色,这些角色将用于 Funtcion 的触发。

返回访问控制 (IAM),点击角色分配,搜索函数应用名称以确认角色已成功添加。完善的权限列表又如下三个:

  1. Storage Queue Data Contributor
  2. Storage Blob Data Contributor
  3. Storage Table Data Contributor

 

第三步:配置 AzureWebJobsStorage__blobServiceUri 参数

因为中国区Azure Storage Account使用的终结点后缀与Global Azure不同,所以这是不能配置AzureWebJobsStorage__accountname 并把值指定为 Stroage Account Name。

而是需要指定具体的Blob Service Uri,所以需要使用AzureWebJobsStorage__blobServiceUri 参数,并包含正确的域名,如:https://<your-storage-account-name>.blob.core.chinacloudapi.cn 

注意:新设置使用双下划线 (__),这是应用设置中的特殊字符,如果需要使用Table 或 Queue,同理,配置它们的终结点:

  1. AzureWebJobsStorage__queueServiceUri,值为 https://<your-storage-account-name>.queue.core.chinacloudapi.cn/
  2. AzureWebJobsStorage__tableServiceUri,值为 https://<your-storage-account-name>.table.core.chinacloudapi.cn/

以上操作完成后,删除旧的配置 AzureWebJobsStorage。

 

Function App就能通过Managed Identity访问Storage Account。

 

参考文档

Use managed identity instead of AzureWebJobsStorage to connect a function app to a storage account : https://techcommunity.microsoft.com/blog/appsonazureblog/use-managed-identity-instead-of-azurewebjobsstorage-to-connect-a-function-app-to/3657606

Configure Durable Functions with managed identity :https://learn.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-configure-managed-identity

Azure Functions 的应用设置参考 : https://docs.azure.cn/zh-cn/azure-functions/functions-app-settings#azurewebjobsstorage

 

 



当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

相关文章
|
4月前
|
API C++
【Azure 环境】VS Code登录China Azure(Function)报错 An error occurred while signing in: invalid_request - AADSTS65002
An error occurred while signing in: invalid_request - AADSTS65002: Consent between first party application 'c27c220f-ce2f-4904-927d-333864217eeb' and first party resource '797f4846-ba00-4fd7-ba43-dac1f8f63013' must be configured via preauthorization - applications owned and operated by Microsoft mus
262 13
|
5月前
|
域名解析 网络协议 API
【Azure Container App】配置容器应用的缩放规则 Managed Identity 连接中国区 Azure Service Bus 问题
本文介绍了在 Azure Container Apps 中配置基于自定义 Azure Service Bus 的自动缩放规则时,因未指定云环境导致的域名解析错误问题。解决方案是在扩展规则中添加 `cloud=AzureChinaCloud` 参数,以适配中国区 Azure 环境。内容涵盖问题描述、原因分析、解决方法及配置示例,适用于使用 KEDA 实现事件驱动自动缩放的场景。
150 1
|
3月前
|
数据安全/隐私保护
【Azure Function App】PowerShell Function 执行 Get-AzAccessToken 的返回值类型问题:System.String 与 System.Security.SecureString
将PowerShell Function部署到Azure Function App后,Get-AzAccessToken返回值类型在不同环境中有差异。正常为SecureString类型,但部分情况下为System.String类型,导致后续处理出错。解决方法是在profile.ps1中设置环境变量$env:AZUREPS_OUTPUT_PLAINTEXT_AZACCESSTOKEN=false,以禁用明文输出。
144 0
|
4月前
|
JSON 数据格式
【Azure 环境】当一个Azure Function资源创建很久了,是否可以获取到它的创建时间呢?
用户在尝试获取 Azure Function App 的创建时间时发现,资源 JSON 中缺少 `createdTime` 字段,仅能查看到 `lastModifiedTime`。同时,活动日志和 Azure Resource Graph 查询也未能提供创建时间信息。经解答,Azure 并未为所有资源类型记录创建时间,建议在部署时将创建时间作为标签记录。若创建时间在 90 天内,可通过部署日志间接获取。
215 7
|
5月前
|
网络协议 API 网络安全
【Azure Function App】发现部分请求Function App遇见 403.72 报错(请求Body>100KB)
在调用Azure Function的HTTP Trigger时,发送POST请求偶尔出现403错误,且响应为空、Header信息少。经排查发现,当请求Body大于100KB时会触发403.72错误,原因是启用了“Client Certificate mode”为“Optional Interactive User”。解决方法是将该模式设置为“Ignore”。由于TLS重新协商机制限制,大请求体无法正常处理,导致此问题。
204 1
|
7月前
|
存储 数据可视化 开发工具
【Application Insights】Application Insights存储的Function App的日志存在"Operation Link" 为空的情况
在将 Azure Functions 升级到 .NET 8 和 Isolated Worker 模式后,Application Insights 的请求日志中 `operation_Link` 字段为空,导致分布式追踪无法正常关联。解决方法包括:确保引用正确的 SDK 包(如 `Microsoft.Azure.Functions.Worker.ApplicationInsights`),正确配置 Application Insights 服务,移除默认日志过滤规则,并使用最新依赖包以支持分布式追踪。通过这些步骤,可恢复端到端事务视图的可视化效果。
178 10
|
7月前
|
安全 Linux 开发工具
【Azure Function】分享把Function App从.NET 6.0升级到.NET 8.0 Isolated的步骤
本文介绍了将Azure Function App从.NET 6.0升级到.NET 8.0 Isolated的步骤。.NET 6.0作为长期支持版本,生命周期至2024年11月结束。为确保持续支持,建议升级至更新版本。升级步骤包括安装.NET 8 SDK、更新Azure Functions Core Tools、修改项目文件目标框架为net8.0、更新兼容的NuGet包、本地测试以及重新发布到Azure。更多详细信息可参考官方文档。
364 9
|
6月前
|
C++ Windows
【Function App】本地通过VS Code调试Function时候遇见无法加载文件错误Microsoft.Extensions.Diagnostics.Abstractions
在使用 VS Code 调试 Azure Functions 时,执行 `func host start` 可能因版本冲突报错。错误信息显示 Rpc Initialization Service 启动失败,可能是由于缺少文件或组件导致。解决方法包括:1) 使用 npm 卸载并重新安装 Azure Functions Core Tools;2) 若问题未解决,重新下载安装包(如 func-cli-x64.msi)修复旧版本工具;3) 退出并重启 VS Code,重新加载项目即可恢复正常运行。参考资料链接提供了更多背景信息。
271 1
|
7月前
【Function App】在PowerShell Function中指定特殊的Microsoft.Graph.Users版本
在Azure Function App中运行PowerShell Function时,通过Requirements.psd1文件管理模块版本。若需将“Microsoft.Graph.Users”从最新版2.26.0改回2.23.0以避免冲突,可通过以下步骤解决:1) 在requirements.psd1中明确指定版本为2.23.0 2) 在profile.ps1中添加`Import-Module Microsoft.Graph.Users -RequiredVersion 2.23.0`语句。此方法确保加载特定版本模块
163 19