Search code examples
azure-functions

Exception: AttributeError: Anonymous when starting a basic function app


I'm very new to azure function apps and I'm following the documentation below to create a basic function using the CLI

https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-python?tabs=windows%2Cbash%2Cazure-cli%2Cbrowser

When starting function app from the documentation example, I'm seeing the following error

[2024-06-25T12:12:35.599Z]   File "/afh/projects/abhijeet-project/azure-functions/demo/function_app.py", line 8, in <module>
[2024-06-25T12:12:35.599Z]     @app.route(route="HttpExample", auth_level=func.AuthLevel.Anonymous)
[2024-06-25T12:12:35.599Z]   File "/usr/local/lib/python3.10/enum.py", line 437, in __getattr__
[2024-06-25T12:12:35.599Z]     raise AttributeError(name) from None
[2024-06-25T12:12:35.599Z] .
[2024-06-25T12:12:36.846Z] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.). 

Can anyone guide how to fix this error? I have not made any changes to the artifacts crated when invoking a function app project. I'm running this code on compute instance created in azure ai studio. TIA.

Here is the full log

vscode ➜ .../code/azure-functions/demo $ func start
Found Python version 3.10.13 (python3).

Azure Functions Core Tools
Core Tools Version:       4.0.5801 Commit hash: N/A +5ac2f09758b98257e728dd1b5576ce5ea9ef68ff (64-bit)
Function Runtime Version: 4.34.1.22669

[2024-06-25T12:12:35.594Z] Worker failed to index functions
[2024-06-25T12:12:35.596Z] Result: Failure
[2024-06-25T12:12:35.596Z] Exception: AttributeError: Anonymous
[2024-06-25T12:12:35.597Z] Stack:   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 413, in _handle__functions_metadata_request
[2024-06-25T12:12:35.597Z]     self.load_function_metadata(
[2024-06-25T12:12:35.597Z]   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 393, in load_function_metadata
[2024-06-25T12:12:35.597Z]     self.index_functions(function_path, function_app_directory)) \
[2024-06-25T12:12:35.597Z]   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 765, in index_functions
[2024-06-25T12:12:35.597Z]     indexed_functions = loader.index_function_app(function_path)
[2024-06-25T12:12:35.597Z]   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.10/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 44, in call
[2024-06-25T12:12:35.597Z]     return func(*args, **kwargs)
[2024-06-25T12:12:35.597Z]   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.10/LINUX/X64/azure_functions_worker/loader.py", line 238, in index_function_app
[2024-06-25T12:12:35.597Z]     imported_module = importlib.import_module(module_name)
[2024-06-25T12:12:35.598Z]   File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
[2024-06-25T12:12:35.598Z]     return _bootstrap._gcd_import(name[level:], package, level)
[2024-06-25T12:12:35.598Z]   File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
[2024-06-25T12:12:35.598Z]   File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
[2024-06-25T12:12:35.598Z]   File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
[2024-06-25T12:12:35.598Z]   File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
[2024-06-25T12:12:35.598Z]   File "<frozen importlib._bootstrap_external>", line 883, in exec_module
[2024-06-25T12:12:35.598Z]   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
[2024-06-25T12:12:35.599Z]   File "/afh/projects/abhijeet-project/code/azure-functions/demo/function_app.py", line 8, in <module>
[2024-06-25T12:12:35.599Z]     @app.route(route="HttpExample", auth_level=func.AuthLevel.Anonymous)
[2024-06-25T12:12:35.599Z]   File "/usr/local/lib/python3.10/enum.py", line 437, in __getattr__
[2024-06-25T12:12:35.599Z]     raise AttributeError(name) from None
[2024-06-25T12:12:35.599Z] .
[2024-06-25T12:12:36.846Z] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
For detailed output, run func with --verbose flag.
[2024-06-25T12:12:41.718Z] Host lock lease acquired by instance ID '000000000000000000000000E2AC0C59'.

I have tried researching about the problem on number of forums but I did not find any resolution to this particular issue. My thinking is that the error "AttributeError: Anonymous" when starting an Azure Function typically indicates that there's an issue with the function's code or configuration. But the thing is, I did not make any changes to the autogenerated artifacts created after I ran func new ... command as instructed in the documentation.

I was hoping func start would spin up a localhost.


Solution

  • AttributeError: Anonymous when starting a basic function app

    • To resolve the AttributeError func.AuthLevel.ANONYMOUS is used instead of func.AuthLevel.Anonymous.
    • I have retried the same CLI commands to create an anonymous function, and it was successfully created and running. check below code:
    import azure.functions as func
    import datetime
    import json
    import logging
    
    app = func.FunctionApp()
    
    @app.route(route="HttpExample", auth_level=func.AuthLevel.ANONYMOUS)
    def HttpExample(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')
    
        if name:
            return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
        else:
            return func.HttpResponse(
                "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
                status_code=200
            )
    

    Output:

    enter image description here

    enter image description here

    Another approach to create the anonymous function in visual studio code. check below steps:

    enter image description here

    enter image description here

    enter image description here