Tcheslav Tcheslav
Tcheslav Tcheslav

Reputation: 190

Azure Function App does not trigger by Event Hub

Hey Guys.

I have written Python function to handle JSON events that are coming to EventHub. Those events are generated by Debezium, and this part is working fine. My python code is also working fine when executed locally from Visual Studio Code. Problem starts when I deploy (using VSC) to Azure Function App. Seems like incoming events does not trigger function app execution.

What am I doing wrong?

My function code:

from typing import List
import logging
import json
import psycopg2

import azure.functions as func


def main(events: List[func.EventHubEvent]):
    #logging.info('Starting')
    conn = psycopg2.connect(database="RDSA", user='postgres', password='********', host='********.postgres.database.azure.com', port= '5432')
    #Creating a cursor object using the cursor() method
    cursor = conn.cursor()

    for event in events:
        row = json.loads(event.get_body().decode('utf-8'))
        #logging.info('Python EventHub trigger processed an event: %s',
        #                event.get_body().decode('utf-8'))
        rowDepartmentId=row["payload"]["after"]["departmentid"]
        rowDepartmentName=row["payload"]["after"]["name"]
        rowDepartmentGroupName=row["payload"]["after"]["groupname"]
        rowModifiedDate=row["payload"]["after"]["modifieddate"]

        SQL="""INSERT INTO CDCSTAGE.TF_DEPARTMENT (departmentid, \"name\", groupname, modifieddate)
             VALUES (%s, '%s', '%s', to_timestamp(%s / 1000000));""" % (rowDepartmentId, rowDepartmentName, rowDepartmentGroupName, rowModifiedDate)

        logging.info('=========== New record in DB: =============')
        logging.info('Department name: %s', row["payload"]["after"]["name"])
        logging.info('Department group: %s', row["payload"]["after"]["groupname"])
        logging.info('Modified date: %s', row["payload"]["after"]["modifieddate"])
        logging.info('SQL generated: %s', SQL)
        logging.info('===========================================')
            #Executing an MYSQL function using the execute() method
        cursor.execute(SQL)

    #Closing the connection
    conn.commit() 
    cursor.close()
    conn.close()

function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "events",
      "direction": "in",
      "eventHubName": "adventureworks.humanresources.department",
      "connection": "AazureEventhubKafka_RootManageSharedAccessKey_EVENTHUB",
      "cardinality": "many",
      "consumerGroup": "$Default",
      "dataType": "binary"
    }
  ]
}

Trigger enabled on Azure portal

Upvotes: 0

Views: 3518

Answers (1)

ChaitanyaN-MSFT
ChaitanyaN-MSFT

Reputation: 541

When Function App is running locally and not getting triggered when deployed to Azure the usual issue is due to a missing connection string in application settings. The settings in the local.settings.json file in your local project should be the same as the application settings in the function app in Azure. You can publish them to Azure when deploying via VS Code. You can add the missing connection via the portal as well via Configuration->New Application Settings. enter image description here

Upvotes: 1

Related Questions