Creating Your first Azure Function

Azure functions can be created using the Azure portal, PowerShell, Azure CLI, and REST API's. Steps to create a function using the ARM template is already detailed at https://docs.microsoft.com/en-us/azure/azure-functions/functions-infrastructure-as-code. In this section, Azure functions will be provisioned using the portal.

Azure functions are hosted within Azure app services. Users create a new function app which in turn creates an app service plan and app service. The app service plan is configured based on the following:

  • Name: Name of app service. The name should be unique within the .azurewebsites.net domain.
  • Location: Location for hosting the Azure function app service.
  • Hosting plan: This is also known as the pricing plan. Here, two options, as discussed previously, are available--consumption and app services plan.
  • Resource group name: The name of the resource group containing both the app service plan and app service.
  • Storage account: Azure functions needs an Azure storage account to store their internal data and logs.
  • Enable application insights: To capture telemetry information from Azure functions.

Creating an Azure function app will provide a dashboard after provisioning.

Clicking on the plus button next to the functions will show a wizard for creating a new function. This wizard shows quick templates for creating the Webhook + API, Timer, and Data processing functions. It will create the necessary scaffolding code and structure for getting started. This scaffolding code is also generated for default bindings and triggers. It also allows for the selection of a language for implementation of the function. Azure functions allow the following:

  • C#
  • JavaScript
  • F#
  • PowerShell
  • Bash
  • Python languages

This wizard also allows for creation of custom functions from scratch.

There are many more options provided for creating a custom function.

In this section, a custom Azure function will be created using the PowerShell language, which can be executed whenever it receives a HTTP request. Provide an appropriate name, in this case FirstFunction, and choose Anonymous as the authorization level. Authorization levels will be discussed later in this chapter.

Creating this function provides a complete function authoring integrated environment along with some code. This code gets the raw content from the incoming req parameter, which is filled up by Azure runtime with incoming data (query string, form values, and so on), and it converts it in the PowerShell object. There could be multiple types of data within this incoming parameter, and a single name value is extracted out of it.

Azure functions runtime also creates a new variable for every incoming querystring with an HTTP request and appends it with $req_query_. So sending the name as querystring will provide an out-of-the-box variable, $req_query_name, containing the value for the name querystring and will be consumed within the function. The response for this function is written to a file that the Azure function runtime reads and sends back to the browser.

This function that can be invoked using an HTTP request from the browser. The URL for this function is available from the environment and is composed of the function app name along with the function name. The format is https://<<function app name>>.azurewebsites.net/api/<<function name>>. In this case, the URL will be https://azureforarchitects.azurewebsites.net/api/FirstFunction.

For sending parameters to this function, additional query string parameters can be appended at the end of the URL. For example, for sending name parameters to this function, the https://azureforarchitects.azurewebsites.net/api/FirstFunction?name=ritesh URL can be used. The output of the function is shown in the following figure:

For HTTP-based functions, the Azure function already provides triggers and binding within the function.json file. This file is used for defining all function level triggers and bindings and there is one associated with every function.

The HTTP template creates a trigger for all incoming requests. The trigger invokes the Azure function and passes in the entire incoming data and payload as a parameter named as req. This parameter is available within the Azure function. The response from the function is a binding that takes output from the res variable from the Azure function and sends it back to the HTTP channel as a response.

..................Content has been hidden....................

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