Azure函数Python环境变量



我希望有人能帮助澄清这个问题。我有一个azure函数运行一些python代码。函数本身非常简单,因为我只需要循环遍历资源组并列出每个资源组内的所有存储帐户。

在本地作为python代码运行良好,我可以使用我的服务原则访问资源。

所以我决定在azure函数中自动执行此操作,如下所示:

def main(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!')
credentials = DefaultAzureCredential()
logging.info('Python timer trigger function ran at %s', utc_timestamp)
KeyVault_Url = f'KeyVault connection String'


client_keyvault = SecretClient(vault_url=KeyVault_Url, credential=credentials)
subscription_id = client_keyvault.get_secret("subscribed").value
resource_client = ResourceManagementClient(credentials, subscription_id)
storage_client = StorageManagementClient(credentials, subscription_id)
logging.info('This should run')
for resource_group in resource_client.resource_groups.list():
logging.info(resource_group.name)
for storage in storage_client.storage_accounts.list_by_resource_group(resource_group.name):
logging.info(f"Storage Account is: {storage.name}")
print(f"tStorage Account: {storage.name}")

当我手动触发此函数时,它成功运行,但在日志中,我没有看到最后2行代码。似乎没有达到forloop

我检查了函数的输出,我发现奇怪的是:

Request URL: 'https://management.azure.com/subscriptions/subscription/resourcegroups?api-version=REDACTED' Request method: 'GET' Request headers: 'Accept': 'application/json' 'x-ms-client-request-id': 'id' 'User-Agent': 'azsdk-python-azure-mgmt-resource/21.1.0 Python/3.9.12 (Linux-5.4.81-microsoft-standard-x86_64-with-glibc2.31)' 'Authorization': 'REDACTED' No body was attached to the request

我不太清楚这是什么意思,如果这就是for loop不运行的原因。

这是我的问题来克服这一点。我有一个服务原则,具有运行此任务的正确权限,并且我想在函数项目的json文件中配置此凭据,因此在部署后,我可以在azure函数本身的配置刀片中设置这些变量。

在项目的根文件夹中,我有一个名为local.settings.json的文件,其中包含一组用于在本地运行该函数的配置。据我所知,在c#中你可以有一个upsetting.[environemt].json,你可以配置特定的变量在特定的环境中使用。有人能帮我解决这个问题吗?

基本上我想有appsettings.staging.json文件与特定的变量,如:

client_id: "client_id"
tenant_id: "tenant_id"

并在门户中的azure功能配置中设置值。

是否有一种方法可以在python函数中做到这一点?

我希望我很好地解释了我的问题,如果我没有犹豫,请问更多的细节。

非常感谢你给我提供的任何帮助或提示。

我们使用Azure Keyvault存储特定于环境的变量和基于每个环境的查询。

{
"AZURE_CLIENT_ID": "***",
"AZURE_TENANT_ID": "***",
"AZURE_CLIENT_SECRET": "***",
}

最新更新