Creating a JavaScript function triggered by a Webhook

Let's create a JavaScript function in Microsoft Azure using Azure Portal:

  1. Log in to the Azure portal and navigate to the function app, which we created in the previous chapter.
  2. Click on the + sign to add a new Azure Function. Set the language as Javascript and the template as Generic Webhook, as shown in the following screenshot:
  1. Now provide a name for the function and click on Create:
  1. A default template will be created, as shown in the following screenshot:
  1. Get the function URL and copy it. We will use it later in the chapter. Save it in a notepad:
  1. Now, we will create a Webhook endpoint in an activity log alert in Azure Monitor. Click on Monitor, as shown in the following screenshot:
  1. Now we need to create an alert. Click on Alerts and then on Add activity log alert:
  1. Provide the following details:
  1. Here's a description of the terms in the preceding screenshot:
    • * Activity log alert name: Name of the activity log alert.
    • * Subscription: Microsoft Azure subscription.
    • * Resource group: This is the resource group that the alert resources are deployed to. Using the same resource group as your function app makes it easier to clean up after you have finished, and it also helps when we want to assign role-based access to resources.
    • * Event category: This includes changes made to Azure resources.
    • * Resource type: This filters alerts to resource group activities.
    • * Resource group: This monitors all resources.
    • * Operation name: This filters alerts to create operations, for example, Get Resource Group, Create Resource Group, Delete Resource Group, Move Resource Group, and Validate Move Resource Group.
    • * Level: This includes informational-level alerts, for example, Information, Warning, and Error.
    • * Status: This filters alerts to actions that have completed successfully, for example, the resource group status will be Failed, Started, or Succeeded.
    • In criteria, we need to define the alert criteria. Since we are creating an alert on the resource group, we need to set the Resource type to Resource groups.
  2. Now scroll down and specify the Actions for the alert, as shown in the following screenshot:
  1. In Alert via, provide the following details:
  • * Action group: This creates a new action group, which defines the action to be taken when an alert is raised
  • * Action group name: This is the name to identify the action group with
  • * Short name: This is a short name for the action group
  1. In Actions, provide the following details:
  • NAME: This is the name of the action
  • ACTION TYPE: This is the response to the alert that a Webhook URL is called
  • DETAILS: You need to paste this in the Webhook URL of the function that you copied earlier
  1. Once the alert is created, it will be seen in the Monitor list, as shown in the following screenshot:
  1. Now, once again go to your Azure Function and modify the code as shown in the following screenshot:
  1. Now, click on the Save and run button and see the log. The Logs will look like the following screenshot:
  1. Now we will test our Azure Function by adding new resource group. Navigate to Resource groups and click on the + Add button, as shown in the following screenshot:
  1. Provide the Resource group name, Resource group location, and Subscription for the Resource group and click on the Create button to create the Resource group:
  1. Once the Resource group is created, go to the Azure Function that we have created and check the logs. The activity log alert triggers the Webhook and the function executes:
  1. Look at the activityLog object in the Logs window. It gives all the details related to the resource group. You can see the resourceGroupName, which we have created for testing, with the status as Succeeded, and the subStatus as Created:

  1. Now we have completed the example. It's always a good practice to delete resources that are no longer in use. So, delete the resource group that we have created for testing.
  2. To delete the resource group, navigate to Resource group and click on the Resource group name, and then on the Delete button.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.134.133