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: An active Azure account. Node.js installed on your local machine. Visual Studio Code (VS Code) with the Azure Functions extension. Azure Functions Core Tools installed (follow the installation guide. Step 1: Create a Resource Group Log in to the Azure Portal. Search for Resource groups in the top search bar and select it. Click + Create. Fill in the required fields: Subscription: Select your subscription. Resource group: Enter a name like EventGridLabRG. Region: Choose a region (e.g., East US). Click Review + Create, then Create. Step 2: Create a Storage Account In the Azure Portal, search for Storage accounts and select it. Click + Create. 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). Click Review + Create, then Create. Step 3: Create an Azure Function App In the Azure Portal, search for Function App and select it. Click + Create. Fill in the basis details required Click Next: Hosting, then select the storage account created earlier (eventgridlabstorage). Click Review + Create, then Create. Step 4: Create the Azure Function (Event Grid Trigger) Open VS Code and install the Azure Functions extension if not already installed. Press F1 or type > in the search bar, then select Azure Functions: Create New Project. Select a working folder and choose JavaScript as the runtime. Select Event Grid Trigger as the function template. Name the function (e.g., EventGridTrigger). Deploy the function to Azure using the command: func azure functionapp publish eventgridfunctionapp Step 5: Write the Function Code In the Azure Portal, go to your Function App. Select the EventGridTrigger function from the Functions list. Navigate to Code + Test. 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)}`); }; 5.Click Save. Step 6: Create an Event Grid Topic In the Azure Portal, search for Event Grid Topics and select it. Click + Create. Fill the required basis Click Review + Create, then Create. Step 7: Link the Event Grid Topic to the Function Go to the Event Grid Topic (eventgridlabtopic) in the Azure Portal. In the left-hand menu, select Event Subscriptions, then click + Event Subscription. 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. Click Create. Step 8: Test the Function Go to your Function App in the Azure Portal. Navigate to Code + Test and click Test/Run. 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" } 4.Click Run. Step 9: Verify the Function Execution Go to your Function App in the Azure Portal. Check the Invocation logs to confirm the function processed the event and logged the data. Step 10: Clean Up Resources To avoid unnecessary charges, delete the resource group: In the Azure Portal, search for Resource groups and select it. Select EventGridLabRG. 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.

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:
- An active Azure account.
- Node.js installed on your local machine.
- Visual Studio Code (VS Code) with the Azure Functions extension.
- Azure Functions Core Tools installed (follow the installation guide.
Step 1: Create a Resource Group
- Log in to the Azure Portal.
- Search for Resource groups in the top search bar and select it.
- Click + Create.
- Fill in the required fields:
- Subscription: Select your subscription.
-
Resource group: Enter a name like
EventGridLabRG
. -
Region: Choose a region (e.g.,
East US
).
- Click Review + Create, then Create.
Step 2: Create a Storage Account
- In the Azure Portal, search for Storage accounts and select it.
- Click + Create.
- 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).
-
Resource group: Select
- Click Review + Create, then Create.
Step 3: Create an Azure Function App
- In the Azure Portal, search for Function App and select it.
- Click + Create.
- Fill in the basis details required
- Click Next: Hosting, then select the storage account created earlier (
eventgridlabstorage
). - Click Review + Create, then Create.
Step 4: Create the Azure Function (Event Grid Trigger)
- Open VS Code and install the Azure Functions extension if not already installed.
- Press
F1
or type>
in the search bar, then select Azure Functions: Create New Project. - Select a working folder and choose JavaScript as the runtime.
- Select Event Grid Trigger as the function template.
- Name the function (e.g.,
EventGridTrigger
). - Deploy the function to Azure using the command:
func azure functionapp publish eventgridfunctionapp
Step 5: Write the Function Code
- In the Azure Portal, go to your Function App.
- Select the
EventGridTrigger
function from the Functions list. - Navigate to Code + Test.
- 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)}`);
};
Step 6: Create an Event Grid Topic
- In the Azure Portal, search for Event Grid Topics and select it.
- Click + Create.
- Fill the required basis
- Click Review + Create, then Create.
Step 7: Link the Event Grid Topic to the Function
- Go to the Event Grid Topic (
eventgridlabtopic
) in the Azure Portal. - In the left-hand menu, select Event Subscriptions, then click + Event Subscription.
- 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.
- Name: Enter a name (e.g.,
- Click Create.
Step 8: Test the Function
- Go to your Function App in the Azure Portal.
- Navigate to Code + Test and click Test/Run.
- 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"
}
Step 9: Verify the Function Execution
- Go to your Function App in the Azure Portal.
- Check the Invocation logs to confirm the function processed the event and logged the data.
Step 10: Clean Up Resources
To avoid unnecessary charges, delete the resource group:
- In the Azure Portal, search for Resource groups and select it.
- Select
EventGridLabRG
. - 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.