'Azure Function Python Environment Variables
I hope somebody can help to clarify this issue. I have an azure function running some python code. The function itself is pretty simple, as I just need to loop over resource groups and list all the storage accounts inside each resource group.
Locally as python code is running just fine and I can access the resources with my service principle.
So I decided to automate this in an azure function as follow:
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}")
When I trigger manually this function, it run successfully but in the logs, I don't see the last 2 line of code. it seems that the forloop is not reached.
I checked the output of the function, and I found bizarre this:
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
The redacted is not really clear to me what it means and if that is the cause why the for loop is not running.
Here is my question to overcome this. I have a service principle that has the right permission to run this task, and I would like to configure this credential in a json file in the function project, so after the deployment I can just set those variables in the configuration blade of the azure function itself.
At the root folder of the project I have a file called local.settings.json with a set of configuration to run the function locally. And as far as I know in c# you can have a upsetting.[environemt].json and you can configure specific variables to be used in specific environments. Can anyone help understand how to solve this?
Basically I want to have appsettings.staging.json file with specific variables, like:
client_id: "client_id"
tenant_id: "tenant_id"
and set the values in the azure function configuration in the portal.
Is there a way to do this in python function?
I hope I explained my problem well, and please if I didn't do not hesitate to ask more details.
Thank you so much for any help or hint that you can provide me with.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
