Introduction
This article explains how we can create Azure function apps from the Azure portal using C# and .NET Core. We are going to create “HttpTrigger” and “TimerTrigger” functions on the Azure portal.
In this article, we are not going to cover how we can subscribe to Windows Azure, but without only focusing on Azure Function App.
If a .NET Developer would like to start learning Microsoft Azure, then “Function Apps” is one of the best things to get started due to its simplicity. In the case of “Function App”, we need to write a very few lines of codes. So, we can complete a hands-on lab very easily and quickly. If we would like to do hand-on with any service of windows Azure, then we need an active subscription for Windows Azure, and for that, there are multiple types of subscriptions available, and we can choose a subscription as per our need.
Why we need an “Azure Function App”?
- Low Cost
Function App is a low-cost hosting option. Whenever we think about cloud hosting, then pricing is one of the first questions that arises in mind. As we know, we always need to watch pricing, so it is always better to choose your hosting features wisely.
- Serverless
Function App is serverless, and we do not need any Virtual machine for hosting the Functions app.
- Less Infrastructure Management
Function App does not require infrastructure management. We need to configure very few things.
- Easy to create
As it does not have any Virtual Machine and other sophisticated settings, a developer does not need to spend more time configuring it, but rather, he/she can focus on the application's core development.
Let us get started with “Function Apps”.
Step 1 - Open Azure Portal
Go to the URL https://portal.azure.com/#home to open the Azure portal. We need to log in before proceeding further.
Step 2 - Create a Resource
Click on the “Create a resource” option to create a new resource.
Step 3 - Select Function App
Select the “Compute” option from the left side menu and then “Function App”, as highlighted in the below screenshot.
Step 4 - Provide Basic Details for Function App
In the Basics tab, fill out the following details:
Step 5 - Review and Create
Click on the “Review + create” button.
We can see that there are a total of 5 tabs for filling the details about “Function App” which are as follows:
Basics Tab
Provides options for “Subscription”, “resource Group”, “Instance Details”, Function App name, Publish, Runtime Stack, and “Region”.
Resource Group
We can use the existing resource group, or we can create a new one as well. If there is no resource group, then click on the “Create new” link to create a new Resource group. Provide the resource group in the textbox.
App Name
The Azure function app name must be globally unique. We need to choose the name with allowed characters. It creates a subdomain name inside the web URL https://azurewebsites.net/, so the new URL for the app name is created as https://greeterfunction.azurewebsites.net/ along with URL suffix for API and function name. Later on, we can see how we can copy the complete URL.
Runtime stack
We are going to use Azure function App version 3.0. We use .NET Core with C# for coding purposes. There are other options available as well, and we can go with other languages as well.
Hosting Tab
The hosting tab provides options for “Storage account”, “Operating System”, and Plan type.
Monitoring Tab
It provides an option to choose if we would like to Create Application Insight or not. By default, the option for “Enable Application Insights” is set to “Yes”. So, it creates an application Insight as well. It helps us with monitoring the details. We can explore it later on once the Azure function app is created.
Tags Tab
Name/value pair to categorize resources.
Review + Create Tab
Final Review of all of the previous Tab.
Note
To keep it simple for the first time, we are not going to change any default options. Leave all the settings as-is, since we can modify those settings later on.
Step 6 - Click on the Create button as Highlighted in the previous screenshot
After that, it takes some to create the app. Meanwhile, it displays a message like “Your deployment is underway”.
Once the deployment completes, then it displays the completion message as well.
Step 7 - Once the deployment completes, click on the “Go to resource” tab.
As shown in the below screenshot:
Step 8 - Add New Function
Click on the “New Function” button. As we can see, the new URL is ready, and we can explore that URL as well. But until now, we have not written even a single line of code. So it is time to write code for our first function app. After clicking on the “new function” button, it creates a new function with the default code.
Step 9 - Edit Function Code Online
Azure provides multiple options to Edit code for function app. Below are 4 provides options. Select the “In-portal” option to Edit code online. It is effortless to edit code with “Visual Studio” and “VS Code” as well. We can see those options in other articles. Right now, focus on editing code online in the portal.
Select the “In-portal” option and click on the “Continue” button.
After selecting the option for the “In-portal”, it asks for choosing the type of function which we would like to create. Right now, we would like to create a function on HTTP based trigger, so let us choose the option “Webhook + API”. Then click on the create button.
Following is a default generated code sample. We can edit it, run it, or we can create another function as well.
In the above screenshot, we can see that it provides an option for copying the URL as well. We can copy this URL and then try to open this URL in a web browser to see the result.
So finally, we have created our first “Azure Function App,” and it is working perfectly fine.
Adding a new Azure Function
Let us add a new Azure Function App.
Step 1
Click on the “+” button, as highlighted in the below screenshot. We can click any of the two “+” options.
Step 2
Details for New Function:
- Type “HTTP trigger” in the search box
- Click on the “HTTP trigger.”
- Provide the name of New function as “DailyTips”.
- Select Authorization level: “Function”.
- Click on the “Create” button.
Step 3 - Change the code
Replace the existing code with the following code:
- #r "Newtonsoft.Json"
-
- using System.Net;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Primitives;
- using Newtonsoft.Json;
- using static System.DayOfWeek;
- using static System.DateTime;
-
- public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
- {
- log.LogInformation($"C# HTTP trigger function processed a request at {Now}.");
-
- string name = req.Query["name"];
-
- string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
- dynamic data = JsonConvert.DeserializeObject(requestBody);
- name = name ?? data?.name;
-
- return name != null
- ? (ActionResult)new OkObjectResult($"Hi, {name} it's {GetDailyMessage(Today.DayOfWeek)}")
- : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
-
- }
-
- public static string GetDailyMessage(DayOfWeek day) =>
- day switch
- {
- Monday => "Motivational Monday!",
- Tuesday => "Tech Tuesday!",
- Wednesday => "Achieve your goals!",
- Thursday => "Thoughtful Thursday!",
- Friday => "Fun Friday!",
- _ => "Happy Weekend!!!",
- };
Step 4 - Save and Run
Click on the “Save and run” button.
Step 5 - Test in a web browser
- Click on “</> Get function URL”
- Copy the URL
- Paste the URL in a browser
- Edit the URL to add name parameter in the query string
- Run the URL in the browser
We can test it on the Azure portal as well. The Azure portal itself provides an option for testing these functionalities.
Creating Timer Trigger
Step 1 - Select “Timer Trigger” Template
- Click on the “+” button to create a new function.
- Search “Timer Trigger” in the search box.
- Click on the “Timer trigger” option.
Step 2 - Provide Details for “Timer Trigger”.
- Name: “TimerTriggerEvery10Seconds”
- Schedule: "*/10 * * * * *"
- Click on the Create button
Step 3 - Run the code
Click on the “Run” button to run the code.
The above examples are some basic examples of Azure functions. We can do much more with the “Azure Function App”.
Deleting all the resources
If we learned anything on Azure, it's to remember to delete everything after your practice is completed. Let's do the same for Azure function Apps.
Step 1
Go to “Home” >> “All Resources.”
Step 2 - Select All >> Delete
Follow the on-screen instructions to delete it as shown in the below screenshot.
Make sure that all the resources have been deleted.