Open In App

How To Use Azure Functions For Event-Driven Architecture ?

Last Updated : 26 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Azure Function is a service provided by Microsoft Azure for running serverless workloads. It provides support for building and developing event-driven applications without explicitly provisioning or managing infrastructure. It also provided for the development of applications based on functions in the cloud. Functions can be triggered using various triggers provided Azure, such as HTTP, timers, Blob storage, etc. Let's dive deeper into using Azure functions for building event-driven architectures.

Primary Terminologies

  • Azure Function: It is a serverless component that runs code or business logic as a reaction to the trigger.
  • Trigger: It is an event that invokes an Azure function. for, e.g., HTTP requests, Message queue, GitHub Webhooks, etc.
  • Function App: It is a service that provides a platform for running Azure functions and their management.

Using Azure Functions in Azure

Step 1: Create a Function App

  • Before creating and using an Azure function, we must create an Azure function app.
  • Login to the Azure portal and search for Function app or Else from services go to Compute > Serverless > Function App.
  • On this page, click on the Create function app. Now specify the details for the function app.

Function App

  • Now in the next section specify your runtime stack. For this article we are using NodeJS with linux operating system.

Select Stack

  • Once that is done leave everything to default and click on review and create. Verify the configuration and create the app.

Step 2: Create Function

  • Once the app is successfully deployed go to its overview page.
  • On overview page below details for app select create in azure portal. This should open a window for creating a function.

Overview

  • Now specify the details as below . Select the trigger type as per your requirement. For now we will select Http Trigger.

Function

  • Specify name for function and click on create.

Specify Name

Step 3: Add Code to the function

  • Now let's add code to the function. On function overview page go to Code+Test from left pane.
  • Remove the default code from the editor and add your logic for handling the request. For this we will add following snippet.
module.exports = async function (context, req) {
const name = (req.query.name || (req.body && req.body.name));

const responseMessage = name
? "Hello " + name + ", From GFG" :
"Hello ? From GFG";

context.res = {
body : responseMessage
};
}

Sample Code

  • Now save the function which will redirect to overview page of function.
  • From overview page click on get function url and copy it .

Function Url

  • Now paste the URL in new browser tab to see output as below.

Verify

  • Modify the URL followed by parameter "name". The url should have format.
<FUNCTION URL>&name=<YOUR NAME>
  • You should see similar output as below.

Output

  • As we can see we have successfully deployed and ran our first event-driven function in azure.

Conclusion

Thus we have created an event-driven function using Azure functions. This function can be modified for more complex business requirements and logics. This is how azure functions are helpful in building event-driven architectures and serverless applications.


Next Article

Similar Reads