Azure Functions provides the built-in binding extensions feature for Azure EventGrid, which makes event data easier to publish and consume. But, the binding extension doesn't support the CloudEvents format yet because it uses the current version of SDK that doesn't support the CloudEvents schema yet. I suspect the binding extension will be available for CloudEvents when a new version of SDK becomes GA. Therefore, it requires a few extra coding efforts to send and receive CloudEvents data via Azure Functions app for now. Throughout this post, I will discuss how to make it.
I used .NET SDK in this post. But there are other SDKs in different languages of your choice:
At the time of this writing, the new SDK has the version of
4.0.0-beta.4–still in preview. With this SDK, you can send and receive event data in the format of CloudEvents. Install the preview version of the SDK by entering the following command:
Let's deal with the EventGrid data.
Then, get the endpoint of your EventGrid Custom Topic:
And, get the access key:
With both endpoint and access key you got from the commands above, create the EventGrid publisher instance in your Azure Functions code:
You need more metadata details to send event data in the CloudEvents format.
source: Event Publisher–It's a URL format in general.
type: Event Type–It distinguishes events from each other. It usually has a format like
datacontenttype: Always provide
The SDK takes care of the rest of the metadata that CloudEvents needs.
If you want to know more about the CloudEvents data spec, visit this page and have a read.
With those metadata details, build up the CloudEvents instances and send them to Azure EvengGrid.
You can now send the event data in the CloudEvents format to EventGrid Custom Topic.
As mentioned above, Azure Functions has the EventGrid binding extension and it doesn't support CloudEvents yet. Therefore, to process the CloudEvents data, you should use HTTP Trigger. And the trigger MUST handle two different types of requests at the same time.
- Responding Event Handler Validation Requests
- Handling Event Data
According to the CloudEvents Webhook Spec, the validation requests uses the
OPTIONS method/verb and include a request header of
WebHook-Request-Origin (line #8). Therefore, to respond to the validation request, the header value MUST be sent back to the EventGrid Topic in the response header of
WebHook-Allowed-Origin (line #9).
Once the validation request is successful, the Azure Functions endpoint will receive the event data with the
POST method/verb from there. If you want to use the CloudEvents itself, use the
@event instance in the following code snippet (line #18). If you only need the
data part, deserialise it (line #19).
You can now receive and process the event data in the CloudEvents format within the Azure Functions code.
So far, we have walked through how Azure Functions app sends and receive event data formatted in CloudEvents for Azure EventGrid. Until the new version of the binding extension is released, we should stick on the approach like this. Hope the new one is released sooner rather than later.