Azure function Not getting triggered or invocated. Getting 401 unauthorized on testing locally

Subash Konar 20 Reputation points Microsoft Employee

I am using Python SDK and Cosmos Db Trigger to monitor the Change feed and write to Blob in Azure Function. In between I am also updating the Stats.json blob(which has a key Stats as 1 or 0).

I am using Visual Studio code to push changes in Azure Function. While testing locally, I am getting the error Unexpected status code: 401 and in the Azure portal, it's not getting triggered. I am not using any HTTP trigger.

import azure.functions as func
from import BlobServiceClient, BlobClient
import json
import logging
import os
import time

def get_recent_state_from_blob() -> int:
        blob_client=connect_storage()"Connected to blob storage")
        state_data = blob_client.download_blob().readall().decode("utf-8")
        state_json = json.loads(state_data)
        Stats = state_json.get("Stats", 0)
        return Stats
    except Exception as e:
        logging.error(f"Error while getting recent state from blob:{e}")
        return 0

def connect_storage() -> BlobClient:
    connection_string= os.environ['AzureWebJobsStorage']
        blob_service_client = BlobServiceClient.from_connection_string(connection_string)
        container_client = blob_service_client.get_container_client(container_name)
        blob_client = container_client.get_blob_client(blob_name)
        return blob_client
    except Exception as e:
        raise e
def update_state_in_blob(new_state: int) -> None:
        blob_client=connect_storage()"Connected to blob storage")
        blob_data = blob_client.download_blob()
        blob_content = blob_data.readall()

        # Decode the existing JSON data
        data = json.loads(blob_content)

        # Update the 'State' key with value 1
        data['Stats'] = new_state

        # Add/Update the 'Time Updated' key with the current timestamp
        from datetime import datetime
        data['Time Updated'] = datetime.utcnow().isoformat()

        # Encode the data back to JSON format
        updated_content = json.dumps(data)

        # Upload the updated content to the blob
        blob_client.upload_blob(updated_content, overwrite=True)"Blob updated successfully.")
    except Exception as e:
        raise e

def main(
    documents: func.DocumentList, outputBlob: func.Out[str]
) -> None:
    consolidated_data = []

    if documents:
        for document in documents:
  "id: %s", document["id"])
  "SwitchNum: %s", document["SwitchNum"])
  "FileNum: %s", document["FileNum"])
  "CallingNum: %s", document["CallingNum"])
                    "id": document["id"],
                    "SwitchNum": document["SwitchNum"],
                    "FileNum": document["FileNum"],
                    "CallingNum": document["CallingNum"],

    data = {"consolidated_data": consolidated_data}
    json_data = json.dumps(data, indent=4)

    state = get_recent_state_from_blob()

    if state == 2:
        update_state_in_blob(0)"Record written successfully")
    else:"State is Active. Skipping...")


      "scriptFile": "",
      "bindings": [
          "name": "documents",
          "connectionStringSetting": "CosmosChangeFeedTrigger_ConnectionString",
          "databaseName": "UI_Trigger",
          "collectionName": "AF_Changefeed",
          "leaseCollectionName": "leases",
          "createLeaseCollectionIfNotExists": true,
          "direction": "in",
          "type": "cosmosDBTrigger"
          "connection": "AzureStorageConnection",
          "name": "outputBlob",
          "path": "changedata/files",
          "direction": "out",
          "type": "blob"

My folder structure below:

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,017 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,283 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,365 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Subash Konar 20 Reputation points Microsoft Employee

    I was able to resolve the issue by making changes in the host.json.Initially ,when the changes are being pushed from VS code it somehow got changed to belowUser's image

    After updating to the below, it started working

    User's image