Search code examples
azure-functionsazure-cliazure-data-explorerazure-python-sdk

How to use AZ Cli or Bash inside Azure Function App?


Is it possible to call AZ Cli / Bash inside an Azure Function App?

My goal is to automatically shut down an ADX Cluster after office times as you can save costs by this.

Would be nice to call something like that where the authentification method is using an User Assigned Identity (UAI):

https://learn.microsoft.com/en-us/cli/azure/kusto/cluster?view=azure-cli-latest#az_kusto_cluster_stop

I would go with the python.mgmt.kusto API but it lacks connections with UAI:

https://learn.microsoft.com/de-de/python/api/azure-mgmt-kusto/azure.mgmt.kusto.kustomanagementclient?view=azure-python

Edit: It is not true that there is no interface in the KustoManagementClient. You need to use the package python-identity which has interfaces (ManagedIdentityCredential, DefaultAzureCredential) to generate credential instances from default identification methods or explicitly by passing a UAI id. The credentials can be used by KustoManagementClients.

Edit2: I would like to share my experiences and my final solution with you.

Generating a credentials instance is the best way if you want to use for the same code for local tests or in the cloud, e.g. during development of a function app. Usually the API DefaultAzureCredential should do the job but it didn't work properly. In my case, and for local tests, I explicitly had to tell it to use my CLI login:

from azure.identity import DefaultAzureCredential

credentials = DefaultAzureCredential(
                            exclude_environment_credential=True, 
                            exclude_managed_identity_credential=True,
                            exclude_powershell_credential=True,
                            exclude_visual_studio_code_credential=True,
                            exclude_shared_token_cache_credential=True,
                            exclude_interactive_browser_credential=True,
                            exclude_cli_credential=False
                        )

On the cloud site I had to go with

credentials = ManagedIdentityCredential(client_id="******")

At the end my cluster finally shuts down:

mclient = KustoManagementClient(credentials, SUBSCRIPTION_ID)
cluster_operations = mclient.clusters
cluster_operations.begin_start(RG, ADX)

I must admit that I haven't checked the python CLI interface at the end but the accepted answer in this thread shows how you could do this.

Thanks!


Solution

  • Yes you can run Azure CLI inside a function app. Azure CLI is an executable Python package which you can find it from its PyPI page.

    If you follow the official tutorial Quickstart: Create an HTTP triggered Python function in Azure to create an Azure Function App for Python, you can try to install Azure CLI as a Python package via pip install azure-cli, and then refer to the other SO thread Azure Function - trigger Python script containing Azure CLI commands to invoke it with your parameters to create a resource as you want.

    Alternatively, To install the Azure CLI, you can copy the whole Azure CLI directory from local machine to kudu console as suggested below SO reference thread.