Search code examples
pythonazure-functionsazure-cosmosdbazure-durable-functionsazure-eventgrid

How to bind CosmosDB and Event Grid to Azure Durable Functions


I would like to use Durable functions to connect to services such as CosmosDB and Event Grid.

I created the code to connect to CosmosDB and Event Grid by referring to the official MS documentation. The following is the code.

However, it does not work because I get an error like the one shown in the picture. Please let me know the cause and solution.

I am using Python programming model v2.

import azure.functions as func
import azure.durable_functions as df
import logging
import numpy as np
import pandas as pd
import time

app = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)
@app.route(route="orchestrators/client_function")
@app.durable_client_input(client_name="client")
async def client_function(req: func.HttpRequest, client: df.DurableOrchestrationClient) -> func.HttpResponse:
    instance_id = await client.start_new("orchestrator", None)
    logging.info(f"Started orchestration with ID = '{instance_id}'.")
    await client.wait_for_completion_or_create_check_status_response(req, instance_id)
    status = await client.get_status(instance_id)
    return f"runtime: {status.runtime_status}\n\noutput:\n {status.output}" 

@app.orchestration_trigger(context_name="context")
def orchestrator(context: df.DurableOrchestrationContext) -> dict:
    test = yield context.call_activity('main', '')
    return {"result", connected}


@app.cosmos_db_output(arg_name="outputDocument", 
                      database_name="MyDatabase",
                      collection_name="MyCollection",
                      connection_string_setting="MyAccount_COSMOSDB")
@app.event_grid_output(arg_name="outputEvent",
                       topic_endpoint_uri="MyEventGridTopicUriSetting",
                       topic_key_setting="MyEventGridTopicKeySetting")
@app.activity_trigger(input_name="blank")
def main(blank: str, 
         outputblob: func.Out[str], 
         outputDocument: func.Out[func.Document]):

    return "ok"

https://i.sstatic.net/6Qk1e.png


Solution

  • You need to use container_name instead of collection_name and for connection, use connection instead of connection_string_setting for extension version 4.x. You can refer to this ms docs.

    I have used the below code and getting expected result.

    import  azure.functions  as  func
    import  azure.durable_functions  as  df
    import  logging
    
    
    app  =  df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)  
    
    ### client function ###
    
    @app.route(route="orchestrators/client_function")
    @app.durable_client_input(client_name="client")
    async  def  client_function(req: func.HttpRequest, client: df.DurableOrchestrationClient) -> func.HttpResponse:
    instance_id  =  await  client.start_new("orchestrator", None, {})
    logging.info(f"Started orchestration with ID = '{instance_id}'.")
    await  client.wait_for_completion_or_create_check_status_response(req, instance_id)
    status  =  await  client.get_status(instance_id)
    return  f"runtime: {status.runtime_status}\n\noutput: {status.output}"
    
    ### orchestrator function ###
    
    @app.orchestration_trigger(context_name="context")
    def  orchestrator(context: df.DurableOrchestrationContext) -> dict:
    test  =  yield  context.call_activity("main", "")
    return {"Connected": test}  
    
    ### activity function ###
    # I want to calculate the value and output it to the bound service.
    
    @app.cosmos_db_output(arg_name="outputDocument",
    database_name="MyDatabase",
    container_name="MyCollection",
    connection="MyAccount_COSMOSDB")
    
    @app.event_grid_output(arg_name="outputEvent",
    topic_endpoint_uri="MyEventGridTopicUriSetting",
    topic_key_setting="MyEventGridTopicKeySetting")
    
    @app.activity_trigger(input_name="blank")
    def  main(blank: str,
    outputEvent: func.Out[str],
    outputDocument: func.Out[func.Document]):
    return  "OK"
    

    Output:

    enter image description here

    enter image description here