Updated March 17, 2023
Introduction to Azure Functions Example
Azure Functions Example will give an overview of what Azure function examples are, the concepts under Azure Functions, along with examples and Azure Function features. The article focuses on concepts and how Azure Function can be monitored and managed using Serverless 360. Azure Function is a serverless compute service that enables users to run event-triggered code without managing infrastructure.
Overview of Azure Functions
Azure Function is a separate App Service that runs in App Service Plan. It can be triggered with the configured trigger but does not run jobs continuously. Azure Functions are event-driven serverless platforms for computing used to run code snippets in Azure. These Azure functions provide flexibility in creating automation and deploying applications. If the user is modernizing the application from a premise server, then the user needs to consider upgrading to Azure Functions. Before moving to Azure Functions and its examples, one should have an idea of Serverless Cloud Computing concepts.
Serverless Cloud Computing
Serverless computing runs and hosts the code in the cloud; Serverless means that users need not install or maintain servers to run the code. Developers write and deploy the code without any worry about the infrastructure required for execution, and the code runs on the servers that are hosted by the cloud provider.
Generally, when a developer needs to run code, they have to set up and maintain their own servers, and hence the configuration for applications is a time-consuming activity. Hence, Serverless computing comes into the picture; it eliminates infrastructure barriers for developers. Developers can deploy the code quickly without even waiting for system administrators to patch operating systems, install servers, or configure networks.
The user has servers hosting applications, but the applications are not always in use and consume resources. By using Serverless computing, users can pay only for compute resources that are needed to execute code. This consumption mode saves user costs by using compute resources only when required.
Creating Azure Function App
With Azure functions, user can build application components into blocks of code in Azure Portal or locally that executes when needed. The user has to provide the function name, subscription, resource group, and operating system details.
Select Hosting plan; Azure infrastructure is defined with hosting plans; it dictates what operating system the code runs, its availability, and scaling ability. There are 3 main hosting plans Premium, Consumption, and Dedicated.
- Premium: It is useful to run Functional applications continuously, access memory options and CPU, and Virtual network connectivity provided in the consumption plan. Premium plans can be chosen for faster start time and longer runtime.
- Consumption: It provides auto-scaling up and down to meet the demand changes. Users can pay only for the compute resources when the function is running.
- Dedicated: It is used to run functions in App Service for predictive scaling where users can scale up and down manually without even waiting for auto-scaling to be in effect.
Azure function is event-driven; the function starts with an event with a specific trigger. There are a few triggers listed below:
- Timer Trigger: Running on schedule, like sending reminders once a day on upcoming appointments.
- HTTP Trigger: Receiving HTTP requests like interaction with API to retrieve application’s information.
- Azure Event Grid Trigger: Running on a new event in an Event Grid subscription, like sending a notification when a new virtual machine is created in the resource group.
- Azure Blob Storage Trigger: Running on Blob storage object creation, like generating thumbnails of the uploaded images
When the Azure function is running, it can connect to other Azure Services. This requires Binding, which is a code that links one Azure Service to another and has direction. The binding direction requires how linked Azure Service receives or sends information from the function. Binding includes Azure Cosmos DB, Storage Account, and Third-Party Applications.
- Azure Cosmos DB: It interacts with the records Cosmos DB like creating or updating new documents.
- Storage Account: Tables, Queues, Access Blobs in the storage account save records to the table.
- Third-Party Applications: Connecting to outside applications such as Twilio to send text messages.
Runtime host determines the language and versions available for coding functions; currently, 3 versions of runtime hosts are supported. Microsoft supports running functions in the latest version of Function runtime, as the latest one provides much more features compared to older runtime.
To Monitor Functional Applications, the Azure function uses Application Insights. It gathers data generated by the function app that includes events and traces the user writes within the app.
Azure Functions Examples
Till now, we have got a brief about Azure Function, so let’s look into an example for a good hands-on.
Here, we shall create PowerShell Function application that logs events in the storage account table. To trigger the function from a fictional software script, use HTTP trigger, and once it is triggered, the function application will store the message to the storage account table using an output binding.
Before processing, there are a few prerequisites to move further. Having knowledge of Azure resource group, Azure storage account, and account with Owner permissions on subscription.
The below example uses a function app based on PowerShell language.
Click on login and search for Function App.
Step 1: Click on https://portal.azure.com/ link. Login/Sign in and search for Function App.
Step 2: In the Function app, click on Create to create a new Function app. In Create Function App, provide the required information on Basics Tab.
Select Azure Subscription, resource group, function app name, it uses azurewebsites.net domain, and select publish as code. Runtime is the language that functions will use; here, select PowerShell and its version. Also, select the region to store the function app.
Step 3: Then move to Hosting Tab, and change storage to the storage account. Plan type as Consumption – Serverless and select the Operating System.
Step 4: Then move to Monitoring tab, and set Application Insight to “Yes”. If any existing insights are available, the user can select them from the dropdown; else, Azure will create new insights for hosting Function App logs. Once done, click on “Review+Create.” Then click Create, which creates the Function app with configured components.
Deployment is completed,
Step 5: Function Creation
Once the Function app is deployed, now you can build a container to define trigger type, binding, and final actual code.
In the Function app, move to Functions – Functions – Add to create a function.
Step 6: In Add, select HTTP trigger. In the new function input, provide the function name, select the function as authorization level, then select Add.
Once the function is built, we need to define where the function output should be sent. Hence create output binding to storage account table for message log. In Output, select Add output to create output integration for the function to connect to the storage account table.
Step 7: In the output window, change the Binding type to Azure storage. Provide a new table name where the function can store data. For the storage account, click New – Select storage account – Ok.
Step 8: Then comes the Code part. In the function window – select code+test. The programming language selected before should be the programming language to write code, i.e., PowerShell as of here. The function will default to have PowerShell code as the sample for HTTP Trigger.
Replace the default code as required, then Save.
Step 9: Trigger Function via an HTTP request. Before triggering, you need to find the function URL to target the request. Get the function URL and in the function URL window, verify if the default key has been selected. Copy the URL to place it on a clipboard. Paste the copied URL in the PowerShell code script where required.
Save – Run PowerShell code. It will invoke the HTTP Trigger attached to the function and return a success message.
Step 10: Validate function logs by selecting the logs icon, and it will display the functions output. Confirm if the message is stored in table storage in a storage account. Expand the storage table and preview. If the table name has been found, entries can be seen while invoking the function.
- Azure functions version 1.0 is available to host on Windows, and updated runtime is available in version 2.0.
- Azure functions have performance issues on a range of languages or scenarios for version 1.0, whereas Azure 2.0 has the advantage of quickly starting new functions with a selected programming language.
- Deployment Code is run using the package, with a lack of User Experience for production.
- Azure Function application is pricing based depends upon the Time of Execution and Total Execution. Inclusive of 1 million requests and 400000 GB of resource consumption each month.
- Azure Premium plan users get enhanced performance as it is billed on the basis of CPUs and GBs.
Conclusion – Azure Functions Example
In Azure Functions Example, we have seen Azure Functions and had a hands-on for example, using PowerShell scripting. Azure Function is a serverless solution for many options and can create an entire web app and automation with triggers an output binding. Having multiple language options makes the function accessible to create a serverless solution. We have also seen a few of the Azure Function features and a complete Overview of Azure.
This is a guide to Azure Functions Example. Here we discuss the introduction, serverless cloud computing, and features, respectively. You may also look at the following articles to learn more –