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 a new function

  1. Open your function app in Visual Studio Code.

  2. Press F1 to create a new blob trigger function. Make sure to use the connection string for your storage account.

  3. The default url for your event grid blob trigger is:

    http://localhost:7071/runtime/webhooks/blobs?functionName={functionname}
    

    Note your function app's name and that the trigger type is a blob trigger, which is indicated by blobs in the url. This will be needed when setting up endpoints later in the how to guide.

  4. 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");
    }
    
  5. Set a breakpoint in your function on the line that handles logging.

  6. Start a debugging session.

    Press F5 to 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:

Screenshot that shows the Command Prompt after starting the "ngrok" utility.

Copy the HTTPS URL generated when ngrok is run. This value is used when configuring the event grid event endpoint.

Add a storage event

  1. Open the Azure portal and navigate to your storage account and select the Events option from the left menu.

    Add storage account event

  2. In the Events window, select the Event Subscription button.

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

    Select subscription type

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

    Select endpoint type

    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:7071 and the function name replaces {functionname}.

  5. The following screenshot shows an example of how the final URL should look when using an Event Grid trigger type.

    Endpoint selection

  6. 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.

Next steps