Connecting EventHubs to API Management
In the previous posts, we looked at:
- Authenticating callers using Groups in API Management Policies
- Exposing Strongly Typed APIs for Azure Queues with API Management Policies
In the third article of the Azure API Mangement series, my esteemed (highly esteemed) colleage Ali Baloch documented how to front-end Event Hubs with API Management with a next-to-no-code solution. Thank You, Ali.
We're assuming you already have:
Azure API Management allows you to take any backend and invoke any HTTP verb POST/GET/PUT/DELETE requests and also provide security, throttling and scaling capabilities.
EventHubs can be invoked using POST request which has following three main parts: (see MSDN)
- URL: Where to send the message.
-
Headers: Authentication/Authorization information using Shared Access Signatures (SAS)
-
Body: Actual message
Procedure:
Steps:
Create an API and specify web service URL of EH.
-
http{s}://{serviceNamespace}.servicebus.windows.net/{eventHubPath}/publishers/{deviceId}/messages
-
(Optional) create a product and add API in the product.
-
Create a policy for API. This is needed to add SAS security headers. For example
<policies>
<inbound>
<set-header name="Authorization" exists-action="override">
<value><![CDATA[SharedAccessSignature sr=sb%3a%2f%2fali-eh-ns.servicebus.windows.net%2fali-eh%2fpublishers%2fhome&sig=wnLzBidpLnsJBZQzNpHbRl%2bSdasfdsafa%3d&se=1787325230&skn=SendListenPolicy]]></value>
</set-header>
<set-header name="Content-Type" exists-action="override">
<value><![CDATA[application/atom+xml;type=entry;charset=utf-8]]></value>
</set-header>
<base />
</inbound>
<outbound>
<base />
</outbound>
</policies>
-
Make sure sr and sig are hex encoded and added as CDATA.
To test the API go to developer portal which looks like following:
Other useful links:
-