DEV Community

Cover image for Building an Event-Driven Azure Function with Node.js and Event Grid
Oluwatobiloba Akinbobola
Oluwatobiloba Akinbobola

Posted on

Building an Event-Driven Azure Function with Node.js and Event Grid

INTRODUCTION

Azure Functions, combined with Azure Event Grid, provide a powerful serverless solution for building event-driven applications. This guide will create an Azure Function in JavaScript (Node.js) that listens to Event Grid events and logs the event data. This is a great starting point for integrating event-driven architectures into your Azure solutions.

Prerequisites

Before starting, ensure you have the following:

  1. An active Azure account.
  2. Node.js installed on your local machine.
  3. Visual Studio Code (VS Code) with the Azure Functions extension.
  4. Azure Functions Core Tools installed (follow the installation guide.

Step 1: Create a Resource Group

  1. Log in to the Azure Portal.
  2. Search for Resource groups in the top search bar and select it.
  3. Click + Create.
  4. Fill in the required fields:
    • Subscription: Select your subscription.
    • Resource group: Enter a name like EventGridLabRG.
    • Region: Choose a region (e.g., East US).
  5. Click Review + Create, then Create.

Step 2: Create a Storage Account

  1. In the Azure Portal, search for Storage accounts and select it.
  2. Click + Create.
  3. Fill in the details:
    • Resource group: Select EventGridLabRG.
    • Storage account name: Enter a globally unique name (e.g., eventgridlabstorage).
    • Region: Choose the same region (e.g., East US).
    • Performance: Standard.
    • Replication: Locally-redundant storage (LRS).
  4. Click Review + Create, then Create.

Step 3: Create an Azure Function App

  1. In the Azure Portal, search for Function App and select it. Function App
  2. Click + Create. + Create
  3. Fill in the basis details required basis details monitoring
  4. Click Next: Hosting, then select the storage account created earlier (eventgridlabstorage).
  5. Click Review + Create, then Create.

Step 4: Create the Azure Function (Event Grid Trigger)

  1. Open VS Code and install the Azure Functions extension if not already installed.
  2. Press F1 or type > in the search bar, then select Azure Functions: Create New Project.
  3. Select a working folder and choose JavaScript as the runtime.
  4. Select Event Grid Trigger as the function template.
  5. Name the function (e.g., EventGridTrigger).
  6. Deploy the function to Azure using the command:
func azure functionapp publish eventgridfunctionapp
Enter fullscreen mode Exit fullscreen mode

eventgridfunctionapp
Step 5: Write the Function Code

  1. In the Azure Portal, go to your Function App.
  2. Select the EventGridTrigger function from the Functions list.
  3. Navigate to Code + Test.
  4. Replace the default index.js code with the following:
module.exports = async function (context, eventGridEvent) {
    context.log("Event Grid Event received:");
    context.log(JSON.stringify(eventGridEvent, null, 2));

    // Extract and log details
    const { subject, eventType, data } = eventGridEvent;
    context.log(`Subject: ${subject}`);
    context.log(`Event Type: ${eventType}`);
    context.log(`Data: ${JSON.stringify(data)}`);
};
Enter fullscreen mode Exit fullscreen mode

EventGridTrigger01
5.Click Save.
Save

Step 6: Create an Event Grid Topic

  1. In the Azure Portal, search for Event Grid Topics and select it.
  2. Click + Create.
  3. Fill the required basis
  4. Click Review + Create, then Create.

Step 7: Link the Event Grid Topic to the Function

  1. Go to the Event Grid Topic (eventgridlabtopic) in the Azure Portal.
  2. In the left-hand menu, select Event Subscriptions, then click + Event Subscription.
  3. Fill the required basis.
    • Name: Enter a name (e.g., EventGridFunctionSubscription).
    • Event Schema: Leave as default (Event Grid Schema).
    • Endpoint Type: Select Azure Function.
    • Endpoint: Select your Function App and the EventGridTrigger function.
  4. Click Create.

Step 8: Test the Function

  1. Go to your Function App in the Azure Portal.
  2. Navigate to Code + Test and click Test/Run.
  3. Enter sample JSON data:
{
  "id": "test-event-id",
  "eventType": "TestEvent",
  "subject": "/example/test",
  "data": {
    "property1": "hello",
    "property2": "world"
  },
  "eventTime": "2025-01-25T12:00:00.000Z",
  "dataVersion": "1.0"
}
Enter fullscreen mode Exit fullscreen mode

Test/Run
4.Click Run.

Step 9: Verify the Function Execution

  1. Go to your Function App in the Azure Portal.
  2. Check the Invocation logs to confirm the function processed the event and logged the data. Function App

Step 10: Clean Up Resources
To avoid unnecessary charges, delete the resource group:

  1. In the Azure Portal, search for Resource groups and select it.
  2. Select EventGridLabRG.
  3. Click Delete resource group, confirm the name, and click Delete.

CONCLUSION

By following this guide, you have successfully created an Event Grid-triggered Azure Function using Node.js. This setup allows you to process and log Event Grid events, enabling seamless integration of event-driven solutions in Azure. This approach is scalable, cost-effective, and ideal for modern cloud applications.

Top comments (0)