【Azure 应用服务】Azure Function集成虚拟网络,设置被同在虚拟网络中的Storage Account触发,遇见Function无法触发的问题

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: 【Azure 应用服务】Azure Function集成虚拟网络,设置被同在虚拟网络中的Storage Account触发,遇见Function无法触发的问题

一切为了安全,所有的云上资源如支持内网资源访问,则都可以加入虚拟网络

问题描述

使用Azure Function处理Storage Account中Blob 新增,更新,删除等情况。Storage Account启用虚拟网络中的服务终结点(Service Endpoint)后,可以实现只能从内网访问。同时,Azure Function也支持集成内网。并且可支持在虚拟网络中被Storage Account所触发。所以根据门户中一步一步的操作,配置完Azure Function和Storage Account的Virtul Network后,发现Function无法启动。在日志中发现问题是:

1

2

3

4

5

6

7

8

9

10

11

12

13

2021-06-30T10:56:00.895 [Error] An unhandled exception has occurred. Host is shutting down.

Microsoft.Azure.Storage.StorageException : This request is not authorized to perform this operation.

   at async Microsoft.Azure.Storage.Core.Executor.Executor.ExecuteAsync[T](RESTCommand`1 cmd,IRetryPolicy policy,OperationContext operationContext,CancellationToken token)

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at async Microsoft.Azure.WebJobs.Extensions.Storage.TimeoutHandler.ExecuteWithTimeout[T](String operationName,String clientRequestId,IWebJobsExceptionHandler exceptionHandler,ILogger logger,CancellationToken cancellationToken,Func`1 operation) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Extensions.Storage\TimeoutHandler.cs : 56

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at async Microsoft.Azure.WebJobs.Host.Queues.Listeners.QueueListener.ExecuteAsync(CancellationToken cancellationToken) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Extensions.Storage\Queues\Listeners\QueueListener.cs : 201

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at async Microsoft.Azure.WebJobs.Host.Timers.TaskSeriesTimer.RunAsync(CancellationToken cancellationToken) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Timers\TaskSeriesTimer.cs : 147

2021-06-30T10:56:00.921 [Information] Stopping JobHost

2021-06-30T10:56:00.921 [Information] Stopping the listener 'Microsoft.Azure.WebJobs.Host.Listeners.CompositeListener' for function 'BlobTrigger1'

2021-06-30T10:56:00.942 [Information] Stopped the listener 'Microsoft.Azure.WebJobs.Host.Listeners.CompositeListener' for function 'BlobTrigger1'

2021-06-30T10:56:00.943 [Information] Job host stopped

(日志可通过高级工具Kudu获取:Kudu地址为:https://<your function app name>.scm.chinacloudsites.cn/DebugConsole 日志路径为:D:\home\LogFiles\Application\Functions\Host\xxxxxxxx.log)

 

问题分析

根据日志发现了:

1) Function Host遇见了异常,导致它被关闭.

2)异常的原因是因为访问Storage没有权限 (Microsoft.Azure.Storage.StorageException : This request is not authorized to perform this operation.

那么问题根源就出现在Azure Function访问Stroage时,走的是公网访问,而公网的访问在Storage中式拒绝的。所以要修改Funciton的配置,让其通过内网的形式访问Storage。在查看官方说明文档后,有发现两个非常重要的配置:

  • WEBSITE_VNET_ROUTE_ALL:设置为1。它会将Azure Function的所有出站调用发送到 VNet。
  • WEBSITE_DNS_SERVER:默认值为168.63.129.16。用与虚拟网络集成时,它将使用与虚拟网络相同的 DNS 服务器。 函数应用需要此设置才能与 Azure DNS 专用区域配合使用。 此设置和 WEBSITE_VNET_ROUTE_ALL 会将应用中的所有出站调用发送到虚拟网络。

 

问题解决

在Azure Funciton的设置页面,添加WEBSITE_VNET_ROUTE_ALLWEBSITE_DNS_SERVER两个参数,配置如下:

 

修改完成后,再次查看Function Host的启动日志,就可见 “Loading functions metadata” 和 “1 functions loaded”等消息。这就表明,Function Host启动成功。

2021-06-30T10:57:00.006 [Information] Initializing Warmup Extension.
2021-06-30T10:57:00.069 [Information] Initializing Host. OperationId: '93cf4ed0-d598-4308-9241-dca5ba6a55ee'.
2021-06-30T10:57:00.073 [Information] Host initialization: ConsecutiveErrors=0, StartupCount=1, OperationId=93cf4ed0-d598-4308-9241-dca5ba6a55ee
2021-06-30T10:57:00.106 [Information] LoggerFilterOptions
{
  "MinLevel": "None",
  "Rules": [
    {
      "ProviderName": null,
      "CategoryName": null,
      "LogLevel": null,
      "Filter": "<AddFilter>b__0"
    },
    {
      "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
      "CategoryName": null,
      "LogLevel": "None",
      "Filter": null
    },
    {
      "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
      "CategoryName": null,
      "LogLevel": null,
      "Filter": "<AddFilter>b__0"
    }
  ]
}
2021-06-30T10:57:00.107 [Information] FunctionResultAggregatorOptions
{
  "BatchSize": 1000,
  "FlushTimeout": "00:00:30",
  "IsEnabled": true
}
2021-06-30T10:57:00.107 [Information] SingletonOptions
{
  "LockPeriod": "00:00:15",
  "ListenerLockPeriod": "00:00:15",
  "LockAcquisitionTimeout": "10675199.02:48:05.4775807",
  "LockAcquisitionPollingInterval": "00:00:05",
  "ListenerLockRecoveryPollingInterval": "00:01:00"
}
2021-06-30T10:57:00.107 [Information] QueuesOptions
{
  "BatchSize": 16,
  "NewBatchThreshold": 8,
  "MaxPollingInterval": "00:00:02",
  "MaxDequeueCount": 5,
  "VisibilityTimeout": "00:00:00"
}
2021-06-30T10:57:00.107 [Information] BlobsOptions
{
  "CentralizedPoisonQueue": false
}
2021-06-30T10:57:00.108 [Information] Starting JobHost
2021-06-30T10:57:00.110 [Information] Starting Host (HostId=lbfunctionforvnet01, InstanceId=05efd41a-013d-428c-b839-fb4b69127366, Version=3.0.15733.0, ProcessId=5480, AppDomainId=1, InDebugMode=True, InDiagnosticMode=False, FunctionsExtensionVersion=~3)
2021-06-30T10:57:00.121 [Information] Loading functions metadata
2021-06-30T10:57:00.137 [Information] 1 functions loaded
2021-06-30T10:57:01.348 [Information] Generating 1 job function(s)
2021-06-30T10:57:01.373 [Information] Found the following functions:
Host.Functions.BlobTrigger1

最后在Storage Blob中上传文件进行测试,Function能成功被触发及获取到正确的Blob信息

 

 

参考资料

具有虚拟网络触发器的高级计划 https://docs.azure.cn/zh-cn/azure-functions/functions-networking-options#premium-plan-with-virtual-network-triggers

配置函数应用设置https://docs.azure.cn/zh-cn/azure-functions/functions-create-vnet#configure-your-function-app-settings

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
18天前
|
Kubernetes Devops 持续交付
DevOps实践:使用Docker和Kubernetes实现持续集成和部署网络安全的守护盾:加密技术与安全意识的重要性
【8月更文挑战第27天】本文将引导读者理解并应用DevOps的核心理念,通过Docker和Kubernetes的实战案例,深入探讨如何在现代软件开发中实现自动化的持续集成和部署。文章不仅提供理论知识,还结合真实示例,旨在帮助开发者提升效率,优化工作流程。
|
10天前
|
网络协议 算法 网络性能优化
C语言 网络编程(十五)套接字选项设置
`setsockopt()`函数用于设置套接字选项,如重复使用地址(`SO_REUSEADDR`)、端口(`SO_REUSEPORT`)及超时时间(`SO_RCVTIMEO`)。其参数包括套接字描述符、协议级别、选项名称、选项值及其长度。成功返回0,失败返回-1并设置`errno`。示例展示了如何创建TCP服务器并设置相关选项。配套的`getsockopt()`函数用于获取这些选项的值。
|
20天前
|
JavaScript 网络协议 API
【Azure API 管理】Azure APIM服务集成在内部虚拟网络后,在内部环境中打开APIM门户使用APIs中的TEST功能失败
【Azure API 管理】Azure APIM服务集成在内部虚拟网络后,在内部环境中打开APIM门户使用APIs中的TEST功能失败
|
14天前
|
存储 C# 关系型数据库
“云端融合:WPF应用无缝对接Azure与AWS——从Blob存储到RDS数据库,全面解析跨平台云服务集成的最佳实践”
【8月更文挑战第31天】本文探讨了如何将Windows Presentation Foundation(WPF)应用与Microsoft Azure和Amazon Web Services(AWS)两大主流云平台无缝集成。通过具体示例代码展示了如何利用Azure Blob Storage存储非结构化数据、Azure Cosmos DB进行分布式数据库操作;同时介绍了如何借助Amazon S3实现大规模数据存储及通过Amazon RDS简化数据库管理。这不仅提升了WPF应用的可扩展性和可用性,还降低了基础设施成本。
34 0
|
14天前
|
开发工具 存储 开发者
Xamarin 与 Azure 竟然无缝集成,数据存储、身份验证、人工智能全涵盖,开启移动应用开发新境界!
【8月更文挑战第31天】Xamarin 是一款强大的跨平台移动应用开发工具,支持使用 C# 同时为 iOS、Android 和 Windows 开发应用。结合 Azure 云服务平台,Xamarin 能够提供数据存储、身份验证及人工智能等多种服务。示例代码展示了如何在 Xamarin.Forms 中使用 Entity Framework Core 连接 Azure SQL Database,并利用 Azure Active Directory 实现安全认证。这种集成让开发者能够构建出高效且功能丰富的移动应用。
24 0
|
20天前
【Azure App Service】列举为App Service集成虚拟网络(VNET)操作时所需要的最小权限
【Azure App Service】列举为App Service集成虚拟网络(VNET)操作时所需要的最小权限
|
20天前
|
Java Spring
【Azure Developer】Springboot 集成 中国区的Key Vault 报错 AADSTS90002: Tenant 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' not found
【Azure Developer】Springboot 集成 中国区的Key Vault 报错 AADSTS90002: Tenant 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' not found
|
20天前
|
网络协议 Linux 网络安全
【Azure 应用服务】更便捷的方式抓取Azure App Service for Windows的网络包
【Azure 应用服务】更便捷的方式抓取Azure App Service for Windows的网络包
|
20天前
|
JavaScript Linux API
【Azure 应用服务】NodeJS Express + MSAL 应用实现AAD集成登录并部署在App Service Linux环境中的实现步骤
【Azure 应用服务】NodeJS Express + MSAL 应用实现AAD集成登录并部署在App Service Linux环境中的实现步骤
|
20天前
|
Java Spring
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息