Azure Functions can be created using the Azure portal, PowerShell, Azure CLI, and REST APIs. The steps for for creating a Function using the ARM template are 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 Service. 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:
- After you have configured the App Service plan, filling in the details:
- Name: The name of app service. The name should be unique within .azurewebsites.netdomain.
- Location: The location for hosting the Azure Functions App Service.
- Hosting plan: This is also known as the pricing plan. Here, two options, as discussed previously, are available—the consumption plan and the App Service plan.
- Resource group name: The name of the resource group containing both the App Service plan and App Service.
- Storage account: Azure Functions need an Azure Storage account to store their internal data and logs.
- Enable application insights: Enable this to capture telemetry information from Azure Functions.
Once the details are filled, click on Create:
Creating an Azure function app will lead you to the following dashboard after provisioning:
- Clicking on the + button next to the Functions will show a wizard for creating a new function. This wizard shows the tools that can be used to create Azure Functions. Select In-Portal as an option and click on Continue button to navigate to next screen that displays multiple types of template like WebHook + API, Timer, and More Templates.... Select WebHook + API and click on Create button. This would create the function with scaffolding code and structure for getting started. This scaffolding code is also generated for default bindings and triggers.
- 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 the Azure runtime with incoming data (query string, form values, and so on). There could be multiple types of data within this incoming parameter,
and in this function a single value is extracted out of it. The value is converted from JSON to .NET object and based on whether the name parameter is present or absent, appropriate response is returned back to user. - This Function 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.
- To send parameters to this Function, additional query string parameters can be appended at the end of the URL. For example, to send 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 screenshot:
- For HTTP-based functions, the Azure Functions already provides triggers and binding within the function.json file, as shown here. 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 Functions and passes in the entire incoming data and payload as a parameter named as req. This parameter is available within the Azure Functions. The response from the Function is a binding that takes output from the res variable from the Azure Functions and sends it back to the HTTP channel as a response.