Gijs Jansen
Gijs Jansen

Reputation: 40

How to access EventGrid trigger Event properties in Azure functions input bindings

I want to pull in cosmos documents into my azure function based on contents of eventgrid events that it triggers on (python worker runtime). Is it possible to do this?

I have below function.json:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "type": "eventGridTrigger",
      "name": "event",
      "direction": "in"
    },
    {
      "name": "documents",
      "type": "cosmosDB",
      "databaseName": "Metadata",
      "collectionName": "DataElementMappings",
      "sqlQuery" : "SELECT * from c where c.id = {subject}",
      "connectionStringSetting": "MyCosmosDBConnectionString",
      "direction": "in"
  }
  ]
}

I want to use properties of the event in the query for the cosmos input binding. I tried it with subject here. It fails with:

[6/4/2020 5:34:45 PM] System.Private.CoreLib: Exception while executing function: Functions.orch_taskchecker. System.Private.CoreLib: The given key 'subject' was not present in the dictionary.

So I am not using the right key name. The only key I got to work is {data} but unfortunately I was not able to access any properties inside the event data using the syntax {data.property} as it says in the docs. I would expect all of the event grid schema keys to be available to use in other bindings since an event is a JSON payload. I got none of them to work, e.g. eventTime, event_type, topic, etc.

Since I saw an example of the same idea for storage queues and they used for example {Queue.id} I tried things like {Event.subject}, {EventGridEvent.subject} all to no avail.

Nowhere in the docs can I find samples of event grid trigger data being used in other bindings. Can this be achieved?

Upvotes: 1

Views: 1647

Answers (2)

Cloghead
Cloghead

Reputation: 29

There is the event.get_json() method that you can use to access the data field in the python code, I did not look at the bindings

Upvotes: -1

Roman Kiss
Roman Kiss

Reputation: 8235

For EventGridTrigger (C# script) can be used for input bindings a custom data object of the EventGridEvent class.

The following example shows a blob input bindings for event storage account:

 {
    "bindings": [
    {
      "type": "eventGridTrigger",
      "name": "eventGridEvent",
      "direction": "in"
    },
    {
      "type": "blob",
      "name": "body",
      "path": "{data.url}",
      "connection": "rk2018ebstg_STORAGE",
      "direction": "in"
    }
  ],
  "disabled": false
}

Note, that only a property from the data object can be referenced.

Upvotes: 3

Related Questions