This post was most recently updated on June 13th, 2022.2 min read.
One more article before Christmas! 😁 This time, I’ll be fixing a function that’s triggering based on new messages received through my IoT Hub in my Azure Functions project. Technically speaking, this is super simple to implement – your function needs to define an IoTHubTrigger, with an Event Hub name and usually a Connection String (I don’t know why I’m Sentence Casing that, too, but I’ll stick to it!)
You can tap a practically unlimited number of Azure Function triggers to any Event Hubs (or Azure IoT Hubs), right? Well – yeah, that’s what it looks like, but actually, you can’t. Not without an additional configuration step, that is!
But before getting into the configuration, let’s take a better look at the actual error we’re running into!
So while trying to run your Azure Functions project with debugging enabled, you’ll run into an annoying issue where the function fails to start and throws an error somewhat like the below:
[2021-07-02T09:55:05.076Z] EventProcessorHost error (Action='Checking Leases', HostName='8cc55fad-d313-434d-b663-2b96bbc9efa1', PartitionId='1'). [2021-07-02T09:55:05.078Z] Microsoft.WindowsAzure.Storage: Server encountered an internal error. Please try again after some time. [2021-07-02T09:55:08.673Z] Host lock lease acquired by instance ID '000000000000000000000000D3CDE7D8'.
Or in the CLI itself:
Your Azure Functions are trying to tap into an IoT Hub to fire on an EventHubTrigger – and failing due to a conflict in Consumer Groups. See – each Azure Function trigger essentially needs a separate Consumer Group.
Yeah, it’s an absolutely abysmal error message. But that’s ok – you’re here now, and I’ve got the solution! ;)
Actually, I’ve got 2 solutions (because it could also be your storage emulator messing up). So listen up.
Below, I’m outlining the steps that have always helped me to fix this particular issue. The first step is just to restart your Azure Storage Emulator/Explorer (if you’re using one), and if that doesn’t help, (re)configure your IoT Hub’s Consumer Groups.
That said, let’s get to it!
Time needed: 30 minutes.
How to fix “Microsoft.WindowsAzure.Storage: Server encountered an internal error.”?
- Restart your Azure Storage Explorer
Let’s get the obvious one first – if this works, you don’t need to go through all the rest of the steps!
If your Azure Storage Explorer (or Azurite, if that’s what you’re using) is borked, restart it. Maybe it’ll fix the issue and you’ll be good!
It didn’t? Thought it might not – check out the next steps, then!
- Create a new Consumer Group for your Iot Hub
Navigate to Azure IoT Hub, and “Built-in Endpoints” under it. On that blade/tab, you should see something like below – create a new consumer group by entering a new alphanumeric value (hyphens supported) in the field and saving the changes.
- Add a new application setting for your Consumer Group
Now you’ll need to tell your Azure Function the name of the consumer group you want it to use.
Something like this:
- Add the “ConsumerGroup” parameter to your trigger (if it doesn’t exist already)
In code, this will look somewhat like the below:
public static Task Run(
Connection = "EventHubConnectionAppSetting",
ConsumerGroup = "%AzureIoTHubConsumerGroupName%"
)] EventData myEventHubMessage)
And that should be it! Did it resolve your issue? Still, having issues? Let me know in the comments -section below!
- Automatically get version number from project dependencies in Azure DevOps - March 28, 2023
- Solving “AADSTS90097: An error has occurred during admin consent processing.” issues in your app - March 21, 2023
- How to fix your MAUI Blazor app crashing on startup with “Failed to compare two elements in the array.” ? - March 14, 2023