Randy Minder
Randy Minder

Reputation: 48392

How to send a message to an event hub from an Azure Function?

I have an Azure Function that fires anytime a message is placed on an IoT hub. I want the function to extract some information from the message and then place it on another event hub. Here is my code:

#r "Microsoft.ServiceBus"
using System;
using System.Text;
using Microsoft.ServiceBus.Messaging;

public static void Run(EventData eventData, out string outputEventHubMessage, TraceWriter log)
{
    // Get some system properties from the SystemProperties dictionary
    var deviceId = eventData.SystemProperties["iothub-connection-device-id"].ToString();
    var messageSource = eventData.SystemProperties["iothub-message-source"].ToString();
    var enqueuedTime = eventData.SystemProperties["iothub-enqueuedtime"].ToString();
    var sequenceNumber = eventData.SystemProperties["SequenceNumber"].ToString();
    var offset = eventData.SystemProperties["Offset"].ToString();

    var data = Encoding.UTF8.GetString(eventData.GetBytes());
    var message = string.Format("Message Source: {0}; Enqueued Time: {1}; Sequence Number: {2}; Offset: {3}; DeviceId: {4}; Data: {5}", messageSource, enqueuedTime, sequenceNumber, offset, deviceId, data);

    outputEventHubMessage = message;

    log.Info($"{message}");
}

The 'outputEventHubMessage' is defined as an output parameter pointing to an event hub I have setup for this purpose. I am getting the following error when the function fires:

2018-04-11T14:23:50.295 [Error] Exception while executing function: Functions.MonitorHub. Microsoft.Azure.WebJobs.Host: Error while handling parameter outputEventHubMessage after function returned:. 
    Microsoft.ServiceBus: Unauthorized access. 'Send' claim(s) are required to perform this operation. Resource: 'sb://iothub-ns-monitorhub-419050-c0a1f3eb71.servicebus.windows.net/monitorhub'. TrackingId:cd503b7c674e4806a64b592bfa3d51f2_G9, SystemTracker:gateway5, Timestamp:4/11/2018 2:23:50 PM.

I don't have any clue what this means or why Azure wouldn't have set this up for me when I created the output parameter

Upvotes: 1

Views: 6898

Answers (1)

Mikhail Shilkov
Mikhail Shilkov

Reputation: 35124

It looks like the connection string that you are using does not have Send permission.

To check that, go to your Event Hubs namespace -> Settings -> Shared access policies and make sure that Send is listed against the policy that you use:

enter image description here

Upvotes: 3

Related Questions