我在我的环境中通过在同一个函数类文件中使用HTTP和Timer触发器函数进行了复制-Python版本3.9.13
本地执行
:
配置
:
-
创建了Azure功能应用程序(Python)和密钥库。
-
已在Azure功能应用程序中启用系统分配的托管身份。
-
在密钥库中,添加了具有值的密钥,也在访问配置中>添加策略>密钥和秘密管理,主功能应用程序系统分配的托管身份对象Id。
-
添加名为的密钥库名称应用程序设置和Python v2模型应用程序设置
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing"
功能应用程序配置。
Azure云门户-功能应用程序执行
:
-
使用以下命令发布到Azure门户功能应用程序:
func azure functionapp publish <your_FunctionApp_Name_inTheAzurePortal>
-
发布到Azure门户功能应用程序后,我在Azure功能应用程序的配置菜单中添加了2个应用程序设置:
注意:您可以在发布到Azure门户之前或之后添加它们。
我可以看到两个函数:
Http触发器函数运行
:
计时器触发功能运行:
代码段
:
函数_app.py
:
import azure.functions as func
import logging
import datetime
import os
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential
app = func.FunctionApp()
@app.function_name(name="HttpTrigger1") #Http_Trigger
@app.route(route="hello")
def test_function(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
utc_timestamp = datetime.datetime.utcnow().replace(
tzinfo=datetime.timezone.utc).isoformat()
keyVaultName = os.environ["KEY_VAULT_NAME"]
logging.info(f'TestEnvFromKeyVault: {keyVaultName}')
logging.info('Python Http trigger function ran at %s', utc_timestamp)
return func.HttpResponse("Hello Krishna, This HTTP triggered function executed successfully.",
status_code=200
)
# Timer_Trigger_in_same_function
@app.function_name(name="mytimer")
@app.schedule(schedule="0 */1 * * * *", arg_name="mytimer", run_on_startup=True,
use_monitor=False)
def test_function(mytimer: func.TimerRequest) -> None:
utc_timestamp = datetime.datetime.utcnow().replace(
tzinfo=datetime.timezone.utc).isoformat()
if mytimer.past_due:
logging.info('The timer is past due!')
logging.info('Python timer trigger function ran at %s', utc_timestamp)
host.json
:
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.15.0, 4.0.0)"
}
}
requirements.txt文件
:
azure-functions
azure.keyvault.secrets
azure.identity
azure-keyvault
local.settings.json
:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=krishfunappstorage;AccountKey=<Storage-Account-Access_Key>;EndpointSuffix=core.windows.net",
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
"KEY_VAULT_NAME":"krishkv4funapp"
}
}
代码示例的以上部分取自以下参考文献
:
-
来自此的Python Azure函数代码
MS Doc
。
-
来自此MS的功能应用程序发布命令
Doc
参考