Debug An Event-Based Blob Storage Triggered Azure Function Using ngrok
Debugging an event-based Blob Storage triggered Azure Function may seem complicated at first glance. As you continue reading this post, you will see that it is actually very simple.
Prerequisites
- Visual Studio Code with Azure Resources extension;
- A Blob triggered function;
- An Azure Storage Account with a Blob container;
- An Event Grid system topic of type “Microsoft.Storage.StorageAccounts” with your storage account as source;
- A ngrok account and a ngrok auth token;
Why Use ngrok?
ngrok is needed to expose your locally running Azure Function to the public, so it can be triggered by Azure Event Grid webhooks event handlers.
IMPORTANT: there is already a very detailed tutorial on Microsoft Learn about this topic. As you can see in the official tutorial, you should use Azurite to emulate Azure Storage services when runnig locally. In my case, I had to attach to a real Azure Storage Account to debug an Azure Function that is triggered by blobs created by a third party service. I wanted to make this point because exposing development resources publicly is considered an unsafe practice.
Connect Your ngrok Account
ngrok config add-authtoken <your_auth_token>
Start ngrok
ngrok http 7071
Create An Event Grid Event Subscription Of Type Webhook
In your Event Grid System Topic, create an event subscription with the following characteristics:
- endpoint type: webhook
- event type filter: “Blob Created”
- subscriber endpoint: use the forwarding endpoint provided by ngrok at the previous step to create an event subscription in Event Grid System Topic:
https://{random_identifier}.ngrok-free.app/runtime/webhooks/EventGrid?functionName={function_name}
Note: for this guide, I created an event subscription with basic settings. Depending on the context, further configuration may be required (see “Filters”, “Additional Features”, “Delivery Properties” blades on event subscription creation wizard in Azure Portal).
IMPORTANT: You have to start your Azure Function locally to create the event subscription.
If the event subscription creation has been completed successfully, you will see a 200 status code in ngrok output:
Start Debugging
Now that you have the event subscription in place, you can finally debug your application.
Use the Azure Resources extension in Visual Studio Code to upload files to the blob container and debug the blob triggered function.
Thank you for reading and happy debugging!
Leave a comment