Search code examples
pythonazureazure-functionsguiduniqueidentifier

How to get unique instance id of azure function in python?


I am trying to save some information (multiple rows) in database from Azure function (Python env.).

For this, I am looking for a unique identifier corr. to each run of azure function.

I have came across the word GUID at few places, but I cannot find any documentation on how to use this function or attribute in Azure Function Python environment.

Any help at earliest is appreciated.


Solution

  • GUID technology is not unique to Python Azure Function, it is a commonly used unique identifier which is generated based on the current time and computing machine, you can do this in python to generate it:

    import uuid
    
    print(uuid.uuid4())
    

    In azure function, use below code to get it:

    import logging
    
    import azure.functions as func
    
    
    def main(req: func.HttpRequest,context: func.Context) -> func.HttpResponse:
        logging.info(context.invocation_id)
        return func.HttpResponse(
                "This is the GUID:" + context.invocation_id,
                status_code=200
        )
    

    Let me know whether this can help you.