Azure Function Event Grid Blob Trigger
This article demonstrates how to debug and deploy a local Event Grid Blob triggered function that handles events raised by a storage account.
Note
The Event Grid Blob trigger is in preview.
Prerequisites
- Create or use an existing function app
- Create or use an existing storage account
- Have version 5.0+ of the Microsoft.Azure.WebJobs.Extensions.Storage extension installed
- Download ngrok to allow Azure to call your local function
Create a new function
Open your function app in Visual Studio Code.
Press F1 to create a new blob trigger function. Make sure to use the connection string for your storage account.
The default url for your event grid blob trigger is:
Note your function app's name and that the trigger type is a blob trigger, which is indicated by
blobsin the url. This will be needed when setting up endpoints later in the how to guide.Once the function is created, add the Event Grid source parameter.
Add Source = BlobTriggerSource.EventGrid to the function parameters.
[FunctionName("BlobTriggerCSharp")] public static void Run([BlobTrigger("samples-workitems/{name}", Source = BlobTriggerSource.EventGrid, Connection = "connection")]Stream myBlob, string name, ILogger log) { log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes"); }Set a breakpoint in your function on the line that handles logging.
Start a debugging session.
Allow Azure to call your local function
To break into a function being debugged on your machine, you must enable a way for Azure to communicate with your local function from the cloud.
The ngrok utility provides a way for Azure to call the function running on your machine. Start ngrok using the following command:
ngrok http -host-header=localhost 7071
As the utility is set up, the command window should look similar to the following screenshot:

Copy the HTTPS URL generated when ngrok is run. This value is used when configuring the event grid event endpoint.
Add a storage event
Open the Azure portal and navigate to your storage account and select the Events option from the left menu.

In the Events window, select the Event Subscription button.
In the Event Subscription window, select the Endpoint Type dropdown and select Web Hook.

Once the endpoint type is configured, click on Select an endpoint to configure the endpoint value.

The Subscriber Endpoint value is made up from three different values. The prefix is the HTTPS URL generated by ngrok. The remainder of the URL comes from the localhost URL copied earlier in the how to guide, with the function name added at the end. Starting with the localhost URL, the ngrok URL replaces
http://localhost:7071and the function name replaces{functionname}.The following screenshot shows an example of how the final URL should look when using an
Event Gridtrigger type.
Once you've entered the appropriate value, click Confirm Selection.
Important
Every time you start ngrok, the HTTPS URL is regenerated and the value changes. Therefore you must create a new Event Subscription each time you expose your function to Azure via ngrok.
Upload a file
Now you can upload a file to your storage account to trigger an Event Grid event for your local function to handle.
Open Storage Explorer and connect it to your storage account.
- Expand Blob Containers
- Right-click and select Create Blob Container.
- Name the container samples-workitems
- Select the samples-workitems container
- Click the Upload button
- Click Upload Files
- Select a file and upload it to the blob container
Debug the function
Once the Blob Trigger recognizes a new file is uploaded to the storage container, the break point is hit in your local function.
Deployment
As you deploy the function app to Azure, update the webhook endpoint from your local endpoint to your deployed app endpoint. To update an endpoint, follow the steps in Add a storage event and use the below for the webhook URL in step 5. The <BLOB-EXTENSION-KEY> can be found in the App Keys section from the left menu of your Function App.
https://<FUNCTION-APP-NAME>.azurewebsites.net/runtime/webhooks/blobs?functionName=<FUNCTION-NAME>&code=<BLOB-EXTENSION-KEY>
Clean up resources
To clean up the resources created in this article, delete the event grid subscription you created in this tutorial.