Send Auth0 events to Azure Blob Storage

This article shows you how to send Auth0 events to Azure Blob Storage via Azure Event Grid by using Azure Functions.

Prerequisites

Create an Azure function

  1. Create an Azure function by following instructions from the Create a local project section of Quickstart: Create a JavaScript function in Azure using Visual Studio Code.

    1. Select Azure Event Grid trigger for the function template instead of HTTP trigger as mentioned in the quickstart.

    2. Continue to follow the steps, but use the following index.js and function.json files.

      Important

      Update the package.json to include @azure/storage-blob as a dependency.

      function.json

      {
        "bindings": [
          {
            "type": "eventGridTrigger",
            "name": "eventGridEvent",
            "direction": "in"
          },
          {
            "type": "blob",
            "name": "outputBlob",
            "path": "events/{rand-guid}.json",
            "connection": "OUTPUT_STORAGE_ACCOUNT",
            "direction": "out"
          }
        ]
      }
      

      index.js

      // Event Grid always sends an array of data and may send more
      // than one event in the array. The runtime invokes this function
      // once for each array element, so we are always dealing with one.
      // See: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-grid-trigger?tabs=
      module.exports = async function (context, eventGridEvent) {
          context.log(JSON.stringify(context.bindings));
          context.log(JSON.stringify(context.bindingData));
      
          context.bindings.outputBlob = JSON.stringify(eventGridEvent);
      };
      
  2. Create an Azure function app using instructions from Quick function app create.

  3. Deploy your function to the function app on Azure using instructions from Deploy project files.

Configure Azure function to use your blob storage

  1. Configure your Azure function to use your storage account.
    1. Select Configuration under Settings on the left menu.
    2. On the Application settings page, select + New connection string on the command bar.
    3. Set Name to AzureWebJobsOUTPUT_STORAGE_ACCOUNT.
    4. Set Value to the connection string to the storage account that you copied to the clipboard in the previous step.
    5. Select OK.

Create event subscription for partner topic using function

  1. In the Azure portal, navigate to the Event Grid partner topic created by your Auth0 log stream.

    Screenshot showing the Event Grid Partner Topics page with Add Event Subscription button selected.

  2. On the Create Event Subscription page, follow these steps:

    1. Enter a name for the event subscription.

    2. For Endpoint type, select Azure Function.

      Screenshot showing the Create Event Subscription page with Azure Functions selected as the endpoint type.

    3. Click Select an endpoint to specify details about the function.

  3. On the Select Azure Function page, follow these steps.

    1. Select the Azure subscription that contains the function.
    2. Select the resource group that contains the function.
    3. Select your function app.
    4. Select your Azure function.
    5. Then, select Confirm Selection.
  4. Now, back on the Create Event Subscription page, select Create to create the event subscription.

  5. After the event subscription is created successfully, you see the event subscription in the bottom pane of the Event Grid Partner Topic - Overview page.

  6. Select the link to your Azure function at the bottom of the page.

  7. On the Azure Function page, select Monitor and confirm data is successfully being sent. You may need to trigger logs from Auth0.

Verify that logs are stored in the storage account

  1. Locate your storage account in the Azure portal.

  2. Select Containers under Data Storage on the left menu.

  3. Confirm that you see a container named events.

  4. Select the container and verify that your Auth0 logs are being stored.

    Note

    You can use steps in the article to handle events from other event sources too. For a generic example of sending Event Grid events to Azure Blob Storage or Azure Monitor Application Insights, see this example on GitHub.

Next steps