Getting Started with Azure Functions Tutorial [Example

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

In this article, we will explore a few examples of how to create Azure Functions and deploy them. We’ll cover the basics- such as using F# or JavaScript with an HTTP trigger function – but also some more advanced topics like invoking WebHooks from your functions

The “azure function app tutorial” is a step-by-step tutorial on how to get started with Azure Functions. It provides an example of the process for creating, deploying and running a function in Azure.

Getting Started with Azure Functions Tutorial [Example

Are you a developer who wants to build a cloud-native software but doesn’t know where to begin? Maybe you’re a system administrator who needs to automate Azure resource management. This Azure Functions lesson might be precisely what you need!

Azure Functions is a serverless computing platform that allows you to execute code snippets on Azure. Azure Functions provide you more options for deploying apps and automating processes. Upgrade to Azure Functions if you’re updating your application from on-premises server architecture.

You’ll learn about serverless principles, Azure Function components, and how to construct your first Azure Function in this post.

Understanding Cloud Computing Without Servers

So, what exactly is serverless cloud computing? In the cloud, serverless computing hosts and executes programs. It is “serverless” in that it does not need the installation or maintenance of servers to execute programs.

No Infrastructure Issues

The cloud provider’s servers host the code. Developers may build and distribute code without having to worry about the infrastructure needed to run it.

When a programmer wants to execute code, they usually have to set up and manage their own server. Configuring servers for apps takes time and is sometimes not a developer’s strong suit.

For developers, serverless computing removes this infrastructural hurdle. Servers continue to execute the code, but developers no longer need to be concerned. Without having to wait for system administrators to install servers, patch operating systems, or setup networking, developers may instantly publish code.

Only pay for what you use.

Consider the servers in your on-premises data center. You have servers that host apps, but they aren’t constantly in use. However, they are constantly using resources.

You just pay for the computing resources required to run the code while using serverless. By only employing computational resources when they are needed, this consumption model saves money.

Scaling by itself

You can scale up or down rapidly with auto-scaling now that you’re only paying for the resources you use. Auto-scaling adjusts computational resources to match demand fluctuations. CPU, memory, and networking are examples of compute resources.

Assume you’re the owner of an e-commerce shop with a major sale coming up. You have no idea how much more site traffic will be generated during the sale. As more people visit your website, serverless will scale up to match the demand.

As the number of visitors drops as the sale finishes, serverless will consume less resources. Developers and administrators don’t have to worry about adding extra servers or deploying code to keep up with the increased demand.

Understanding Azure Functions

Microsoft, not wanting to be left behind, created their own serverless solution called Azure Functions. This Azure Functions lesson is all about Microsoft’s event-driven serverless solution, Azure Functions.

With Azure Functions, you can turn application components into code blocks that can be executed locally or via the Azure Portal at any time.

Functions vs. Azure Functions Functions vs. Apps

When dealing with Azure Functions, you’ll come across a lot of ‘functions.’ This terminology might be perplexing for a newcomer. You should be aware of three different ‘function’ names.

  • Azure Functions (capitalized) — The service’s name, such as Azure Automation, Azure Virtual Machines, or Azure Storage Accounts.
  • Function App — This is an Azure Functions “instance.” There is just one application.
  • A function is a programmatically defined function. A single Function App may include many functions (lowercase).

To add to the confusion, some educational materials use these names interchangeably, so be cautious!

Triggers

Azure Functions is a “event-driven” platform. This characteristic describes how Azure Functions begins to run code. An event will initiate a function. That event may take numerous forms, each with its own set of circumstances.

The trigger is in charge of launching an Azure function, and there are plenty to pick from. The following are examples of trigger events:

  • Receiving an HTTP request, such as communicating with an API to receive application data, is an HTTP trigger.
  • Timer trigger: Sending reminders about impending appointments on a regular basis, such as once a day.
  • Running on blob storage object creation, such as creating thumbnails of uploaded photos, is an Azure Blob Storage trigger.
  • A new event in an Event Grid subscription triggers an Azure Event Grid trigger, such as delivering a message when a new virtual machine is created in a resource group.

Bindings

Azure Functions is a collection of different functions. A function may connect to other Azure services while it is executing. A function needs a binding to do this. A binding is a piece of code that “connects” two Azure services and has a purpose (in or out). The binding direction specifies how the “connected” Azure service transmits and receives data from the function.

Bindings include the following:

  • Access blobs, tables, and queues in a storage account, for example, to save records to a table.
  • Azure Cosmos DB: Update or create new documents by interacting with entries in Cosmos DB.
  • Connect to third-party programs for sending text messages, such as Twilio.

Hosting Packages

Even though you don’t have to manage the server running the code in a function, you must still tell Azure a little bit about how you intend to use it. Azure Functions’ infrastructure is defined via Hosting Packages. Hosting Packages dictates what operating system the code runs on, its scaling ability, and availability.

The three main Hosting Packages include:

  • Consumption: Provides Scaling by itself up and down to meet changes in demand. You only pay for compute resources when the function is running.

The basic hosting plan, Consumption, is ideal for development and testing environments.

  • Premium: Useful for continually running Function Apps, having more CPU and memory choices, and having virtual network connection than the Consumption plan. Premium subscriptions provide longer runtimes and quicker startup times.
  • Dedicated: Run functions in App Service plans for predictive scaling, allowing you to scale up or down manually without having to wait for auto-scaling to kick in. When you require a function to operate continually, choose Dedicated plans.

Hosts at Runtime

Each function executes some code. To guarantee that the code executes as intended, it must be written in a language stated in the function. Every function must have a declared runtime host in order to execute that code.

Hosts at Runtime determine what languages and versions are available for coding your function. Azure Functions currently support three versions of the runtime host: 1.x, 2.x, and 3.x.

Microsoft recommended that you use the most recent version of the Function runtime to execute your functions. The most recent runtime has greater functionality and compatibility, while earlier runtimes are getting updates.

When you create Functions in the Azure Portal or with the Azure CLI, Azure sets the runtime version to 3.x by default.

The language used by all functions in a Function App is the same. In version 3.x, Azure Functions supports the following programming languages:

  • .NET Core 3.1 with C#
  • Nodes 10, 12, and 14 in JavaScript
  • .NET Core 3.1 with F#
  • Java version 8 & 11
  • Core 6 and 7 are two versions of PowerShell.
  • Versions 3.6, 3.7, and 3.8 of Python

Logging and Monitoring

Finally, Azure Functions leverages Application Insights to monitor your Function Apps. Application Insights collects data from the function app, such as application traces and events that you create.

Function Apps do not need Application Insights to be deployed. You may still see the current logs while the Function App runs without it, but you won’t be able to see historical data on your functions’ performance and troubleshooting.

Creating an Event Logger Azure Function Tutorial

Let’s get your hands dirty with Azure Functions now that you’ve gotten the idea of it!

This Azure Functions article will show you how to build a PowerShell Function App that logs events to a storage account table. Imagine utilizing a custom PowerShell script to deploy new software to hundreds of PCs. This logger function is called by the script to keep track of the rollout’s progress and failures.

An HTTP trigger will be used to call the function from your fictitious software deployment script. After being triggered, the Function App will use an output binding to save a message to a storage account table.

For storing structured data such as log messages, storage account tables are a low-cost choice.

Prerequisites

If you want to follow along with this Azure Functions lesson step by step, you’ll need the following:

  • A resource group in Azure. This tutorial will make use of the rg-atafunctions-westus2 resource group.
  • A storage account in Azure. This tutorial will utilize the atafunctionsdemo storage account.
  • A subscription account with Contributor or Owner rights

Make a function app.

After you’ve completed all of the criteria, you’ll need to develop a Function App. All code execution is encapsulated in the Function App component. The Function App runs functions and combines them together to make resource sharing and administration simpler. This tutorial makes use of a PowerShell-based Function App.

1. Log into the Azure Portal using a web browser.

2. Search for “function app” in the Azure Portal and choose the appropriate service.

Looking for a service called Function App?Looking for a service called Function App?

3. To build a new Function App, go to the Function App service and click + Create. On the Basics tab of the Create Function App box, fill in the following information:

  • Select your Azure subscription from the drop-down menu.
  • Choose a resource group to work with. rg-atafunctions-westus2 will be used in this lesson.
  • Function App name: Give the Function App a globally unique name. The domain azurewebsites.net is used in the Function App URL.
  • Publish: Choose Code to run the function code using the built-in Azure image. Select Docker Container if your project needs a certain language version or another requirement not included in the built-in image.
  • Select the programming language that will be used by all functions in the app. Select PowerShell Core for this demonstration.
  • Version: To run the code, choose the programming language’s runtime version. Because it is the only version available, this article utilizes PowerShell 7.0.
  • Select an Azure region in which the Function App will be stored. Select a location that is nearest to the users of the app or other services that the function uses. The West US 2 area will be used in this lesson.

Entering basic details for a Function App deploymentEntering basic details for a Function App deployment

4. Change the storage option in the Hosting tab to the storage account where the function will store messages. This tutorial will utilize the atafunctionsdemo storage account.

Consumption is the default plan type (Serverless). While you use the Consumption plan, you will only be charged when the Function App runs. Consider the Premium or Dedicated plans if your Function App needs more powerful instances or long-running situations.

For launching a Function App, choose your hosting choices.For launching a Function App, choose your hosting choices.

5. Select the Monitoring option.

6. Select Yes for the option to enable Application Insights. Application Insights collects additional historical data for performance analysis and troubleshooting. Select an existing Application Insights deployment from the dropdown menu. Otherwise, Azure will build a new Application Insights resource to store the performance logs for the Function App.

7. When you’ve finished configuring all of the options, click Review + Create.

8. Now, click Create. Azure will Make a function app. and associate it with the configured components.

9. Congratulations, your Function App has been successfully deployed! Select Go to resource now.

After deployment, accessing the resourceAfter deployment, accessing the resource

Construct the Function

After you’ve created your Function App, you’ll need to create a container to specify the trigger type, binding, and ultimately your code. All of this is accomplished via the use of a function (lowercase).

1. In the Function App, navigate to Functions —> Functions and select the + Add button to create your first function.

Adding a new featureAdding a new feature

2. Select the HTTP trigger template in the Add function window, which will provide further information. An HTTP Trigger enables you to invoke the function from another application by launching an HTTP request.

3. Type a new name for the function in the New Function entry box. LoggerFunction is the name of this function.

4. Set the authorisation level to Function. The Function permission level generates a unique access key for performing this function. When you visit the HTTP URL to activate the function, you’ll see the function key.

5. Finally, click Add to Construct the Function.

For a function, creating an HTTP TriggerFor a function, creating an HTTP Trigger

Bind the Table Output

After you’ve finished building the function, you’ll need to decide where to send the result. You’ll be using a storage table for this project. Create an output binding for logging messages to the storage account table.

The function may access the table storage without caring about authentication or connection strings thanks to the output binding. The output binding is referenced in the code, and the function connects to the service for you.

  1. Select Integration on the left side of the function window. Azure shows a diagram of the function’s integrations.

The trigger, inputs, outputs, and functions themselves are all referred to as “integrations” by Azure.

Below, you can see the HTTP trigger input that was generated in the previous stage. The function includes an HTTP response as an output since it already has an HTTP trigger configured.

When you do the web request later, you’ll see the output message.

2. Select + Add output in the Outputs box to establish a storage account table output integration for the function.

Integrating functions and seeing themIntegrating functions and seeing them

3. Change the Binding Type to Azure Table Storage in the Create Output box. Replace the table parameter name with a new one. outputLogTable is the name of the parameter in this tutorial.

Take note of the name of the output binding. It’ll come in handy later.

4. Next, enter the name of the table where the data is stored by the function. The function will construct the table if it does not already exist. This Azure Functions tutorial’s table is called atalogs.

5. To link a storage account, click New, then pick the account. This tutorial makes use of the atafunctionsdemo storage account.

6. Click OK.

Creating an output binding for Azure Table StorageCreating an output binding for Azure Table Storage

Calculate the Function

You’ve finished building the Function App, which contains the trigger and output binding. It’s time to get serious about coding!

1. Select Code + test from the function window. You selected PowerShell as the language when establishing the Function App; the function will already have a PowerShell code sample for an HTTP trigger.

PowerShell code for the default HTTP TriggerPowerShell code for the default HTTP Trigger

2. Substitute the following for the default function code. Comments regarding the code are inlined to guarantee that the instruction follows you when you paste this code into the function.

3. When you’ve finished entering all of the code, click Save.

Activating the Function

Now that the function code is ready, it’s time to run it. You must activate all functions to do this. You’ll use an HTTP request in this situation.

You’ll need the function’s URL to target the HTTP request before you can activate it from your calling script. To locate this:

1. Click the Get function URL button in the function window.

Getting the URL for the functionGetting the URL for the function

2. Check that the default key is chosen in the function URL window. The function key generated previously is the default key. To save the URL to your clipboard, click the copy symbol.

The URL is copied to the clipboard.The URL is copied to the clipboard.

3. Open your preferred PowerShell editor after copying the trigger URL to your clipboard. It’s time to start working on the PowerShell code that will call the HTTP trigger.

4. In a new script, copy and paste the code below. Every remark is inline.

You may make HTTP requests with any command or language, however for the sake of this tutorial, PowerShell 7.1.2 is used.

Check explore the post Wrangling REST APIs with PowerShell and JSON (Four Demos!) for more information on PowerShell and JSON.

5. Save the PowerShell script as ata-functions-demo.ps1 and execute it.

If the script is successful, it will call the function’s HTTP trigger and return a message confirming that the item was successfully recorded to the table.

Validating Logs of Functions

Now that you’ve called the function (hopefully), let’s make sure it really worked. To do so, first:

1. Return to your function by opening your web browser (if you closed it).

2. Click the Logs icon to the right of the code editor. During execution, the output of the function is shown on the log console.

Accessing the function's Log consoleUsing the Log console of the function

The Write-Information command in the function code on the Logs page should show the INFORMATION banner.

If you don’t see entries indicating the function code’s execution, refresh the console log window by clicking the Stop and Start buttons at the top.

The Logs console and function output may be seen.The Logs console and function output may be seen.

3. Go to your storage account to verify that the message has been saved in your table storage.

Enter the name of the storage account in the search box at the top of the portal to swiftly switch to it.

4. Expand TABLES in Storage Explorer (preview) on the left.

The table name specified while constructing the output binding is shown below (atalogs). The entries generated while executing the function through a web request should be shown in the table.

The partition key, a unique row key, the log severity, and the log message are all included in each entry.

The timestamp column is automatically included in the table.

Viewing the function-generated table storage entriesViewing the function-generated table storage entries

Check out the article How to Export Teams PSTN Usage Records with Azure Functions for an example of an Azure Function with a timed trigger.

Conclusion

You learnt about Azure Functions and used PowerShell to create your first function in this Azure Functions lesson. Azure Functions is a great serverless solution with a lot of flexibility.

With multiple triggers and output bindings, you may design a whole web application and other automation. Any developer may construct their own serverless solution with the help of many language possibilities.

Check out the Azure Serverless Community Library for a different project. The collection provides community-created solutions that use various Azure serverless choices and languages.

The “azure functions c# example github” is a tutorial that will teach you how to get started with Azure Functions. The blog post includes an example of the code used in the tutorial.

Related Tags

  • azure functions tutorial c#
  • azure functions example
  • how to create function app in azure
  • create azure function in visual studio code
  • when to use azure functions

Table of Content