Azure python function app cant load azure-identity module

226 views Asked by At

I have built an azure python function app which used to run fine. However it now is unable to load the azure-identity module without timing out. It will not crash, but will simply never make it past any line such as from azure.identity import DefaultAzureCredential. After 5 minutes the function times out. Does anyone have any idea what could cause this? Thanks!

1

There are 1 answers

0
Pravallika KV On

I have reproduced the same in my environment with below steps:

  • Created Python Azure function App with the below code:
import azure.functions as func
import logging
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient

app = func.FunctionApp()
container_name = "testkp" 
blob_name = "Blob.txt" 
@app.route(route="http_trigger", auth_level=func.AuthLevel.ANONYMOUS)
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

  # Use DefaultAzureCredential to authenticate
    credential = DefaultAzureCredential()

    # Initializing BlobServiceClient with DefaultAzureCredential
    blob_service_client = BlobServiceClient(account_url="https://<storage_account_name>.blob.core.windows.net", credential=credential)

    # Access a container
    container_client = blob_service_client.get_container_client(container_name)

    # Access a blob
    blob_client = container_client.get_blob_client(blob_name)
    blob_content = blob_client.download_blob().content_as_text()

    return func.HttpResponse(f"Hello, {name}! Blob Content: {blob_content}")

requirements.txt:

Add Packages required:

azure-functions
azure-identity
azure-storage-blob
  • Create and activate Virtual environment with the below commands:
python3 -m venv .venv
.\.venv\Scripts\activate

enter image description here

  • Run pip install -r requirements.txt to install the dependencies:

enter image description here

Note: You should have Storage Blob Data Contributor role assigned in your storage account.

Portal:

  • Create a container in storage account and upload a Blob.

enter image description here

  • Run the function with the command func host start:

enter image description here

enter image description here

Response:

enter image description here