다음을 통해 공유


Azure Event Grid With Logic Apps and Functions

Introduction

With the cloud adoption and server-less solution design there has been rapid shift the way modern application are connecting to each other.Integration is becoming more and more important with large number of connecting enterprises ,software spanning over cloud and on-premise ,consumer choices and customer changing demand etc  . Server-less technologies like Logic Apps ,Azure functions ,Azure service bus ,API management join together to build a robust integration framework for any enterprise in the cloud .  These  technologies  has simplified the event based messaging pattern and reactive programming .

In this TechNet wiki we will go through one of the newest addition to Azure called  Azure Event Grid along with Logic Apps and Azure Functions .Azure Event Grid Topic provide router mechanism and based on reactive programming ,event based messaging pattern with features of multiple listeners ,scale on demand and consumption based pricing plan and many more  .Azure Event Grid Topic routes message to different destination system based on subscription and filter expression with push model. When you use Azure Event Grid Topic within your enterprise, it will limit the multiple polling mechanism were you look for new events and messages  at regular interval of time.

For more information about Event Grid Topic features and its pricing plan look at the below Microsoft documentation
https://azure.microsoft.com/en-us/pricing/details/event-grid/

Solution Design

In this solution we are developing interface for fictitious company Millennium car service . Millennium car service provides vehicle repair and cleaning services to the client and uses Dynamic 365 to manage customers relational data . It has payroll system which will generate the invoices once the service is completed and the vehicle is ready to be delivered.

In this solution we will use Logic apps,Azure Functions along with Event grid to build a reactive messaging pattern to notify each system to take appropriate action  and provide seamless experience to the client and to the enterprise .

Prerequisite

To work with the scenario, you need to have a valid subscription to Microsoft Azure.
 You can register for free subscription at link: https://azure.microsoft.com/en-us/free/

Azure Event Grid Topic

Lets start with the first step and create a  Azure Event Grid which will act as router for all the logic apps workflow.As of now to create Azure Event Grid you require a organisation account. If you do not have  organisational account and using your personal email address to login through azure portal then create a tenant user and assign global rights to your subscription .  The Steps are listed over Microsoft documentation below

/en-us/azure/active-directory/add-users-azure-active-directory#locate-your-default-directory-in-the-azure-classic-portal

Once done, go to your resource group and click on Add new item and search for Event Grid Topic.This will open Event Grid Topic blade .Click on Create Topic  and enter the topic Name,select the subscription and resource group. If you do not have any resource group or you want to create one for the sample then click on Create new resource group and click Ok to Create New Azure Event Grid Topic .

                        

This will hardly take second and your new Event Grid Topic will be   provisioned  .Once done open the Azure Event Grid resource and  look at the different properties available for you . The overview section will be like below -

                            

The important things to note here are Event Grid subscription, topic endpoint and Keys. Copy the topic endpoint and key in notepad and move ahead to build event driven logic apps 1 which will be fired once an email is received in the mail folder.

The Logic App workflow is very simple ,it will read mail content and pass it to the azure function, azure function will strip the HTML and read the content line by line to create valid JSON payload based on the object notation for client specific data model. After that Logic App will call Azure functions to Get Authentication token which will return valid aeg-sas-token token required to publish a message on to the event grid. We will pass aeg-sas-token token will be passed in the header of the HTTP request.

      
 

The important consideration here is the message format what we need to send to the Event Grid.  If you look at the compose action it will show how you need to construct valid message for Event Grid

      
 

[
  {
    "data": "@body('emailparser')",
    "eventTime": "2017-06-26T18:41:00.9584103Z",
    "eventType": "createnewaccount",
    "id": "@guid()",
    "subject": "/myapp/vehicles/motorcycles",
    "topic": "/subscriptions/subscriptionid12345/resourceGroups/TechNetWiki/providers/Microsoft.EventGrid/topics/technetwikitopic"
  }
]

If you are using .Net Client to publish message then look at the JSON string format which is well defined over Microsoft documentation site

/en-us/azure/event-grid/event-schema

To Generate the aeg-sas-token we have used Azure functions. We have stored the Key and Event Grid Topic details within Application config file of Azure functions and creating the token after each day and retrieved the value using config manager and passed to the custom method to generate aeg-sas-token.
The sample code will be like below code snippet 

public static  async Task<string> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info($"Webhook was triggered!");
 
    string jsonContent = await req.Content.ReadAsStringAsync();
    dynamic data = JsonConvert.DeserializeObject(jsonContent);
 
    string eventgriduri =ConfigurationManager.AppSettings["EventGridURL"];
     string eventgridkey =ConfigurationManager.AppSettings["EventGridKey"];
     DateTime expirationUtc = DateTime.UtcNow.AddDays(10);
 
     string authtoken= GetAuthToken(eventgriduri,expirationUtc,eventgridkey);
     
     return authtoken;
}

 
The function call logs within Azure function App for Get Authentication Token. For Better monitoring you can setup application Insights within Function app and
you can setup the OMS to read Application Insights .

                  

 Now coming back to the solution,  another option is to use aeg-sas-key which can be taken from Azure portal and navigating through Azure Event Grid Topic .

you can look at Event Grid documentation which is having nice description over various security options to invoke the Custom Event Grid Topic API Endpoint.

/en-us/azure/event-grid/security-authentication

Moving ahead with Logic App workflow, the final design will consist of one trigger and four actions. Below screen shot describe the final workflow design.

                                                          

To note in this sample we are using HTTP connector to publish message within Azure Event Grid. Going forward we will see specific connector will be available within logic apps to publish message.

The Execution of above workflow will be like below 

             

Once a message is successfully posted, Event Grid will trigger second logic App workflow based on the filter condition set on the second logic App. Once triggered the second logic App will create a new customer record send an update message along with customer id to the Event Grid to fire .

Once an update message is received within Event Grid it will fire two logic apps, one will set Customer Appointment within CRM and send email to a customer along with appointment details while the other logic App will create a new record in SQL as an opportunity for invoicing. Here we can set same filter condition on both the logic apps .

The workflow for the first one will be like on  an event is triggered over Azure Event Grid list the customer city and create a new record within the CRM.   Once a customer is successfully listed within CRM send the customer number and appointment details and pass on another event to Event Grid which will fire resourcing application to look for a specific technician.

             

Now if you look into the execution of the logic app run you can find that new record for customer has been created and new event is fired with customer details which is stored with Dynamics 365 

        

The third logic App will be also triggered by the same message flowing through Event Grid based on the filter condition we are subscribing  the logic . This Logic App will update the SQL with customer and appointment  details which will be used for final invoicing and future opportunity. These are very clear adoption of new technologies to build modern application .

And the execution of logic app will be like below

        

Conclusion

 As enterprise is going ahead with next generation of services available as  Software as Services like Dynamics 365, Sales force etc  Server-less solution design is one of the key aspect every enterprise should focus.This will not only benefit them to scale up on demand but it has  which will help them to focus on business rather then looking at infrastructure.
With Server less deign the changes are easy and quick, think of a case where you need to add a new listener endpoint for your partner. In a primitive world you might set up sftp or open port etc etc . Now in server-less world you just need to add a subscription for your end partner and an automatically message will start to flow .

See Also

An important place to find a huge amount of BizTalk related articles is the TechNet Wiki itself. The best entry point is BizTalk Server Resources on the TechNet Wiki. Another important place to find Logic App related articles is the TechNet Wiki itself. The best entry point is Logic App Resources on the TechNet Wiki.