Serverless Computing is Platform as a Service (PaaS) in Cloud Services. It allows us to deploy and manage individual code functions without a need to deploy and manage on the individual Virtual Machine (VM). Azure Functions is a serverless compute service that runs our code on-demand without needing to host it on the server and managing infrastructure. Azure Functions can be trigger by a variety of events. It allows us to write code in various languages, such as C#, F#, Node.js, Java, or PHP.
- What is Webhooks?
- Implementation of Azure Functions using HTTP Trigger
- How to Create Function App in Azure portal, Visual Studio, VS code.
Glimpse of Post
Prerequisite
- Beginner level knowledge of Microsoft Azure
- Azure portal Account & Subscription
- Visual Studio or VS code
- .NET SDK
- C# Extension for VS code
What is Webhooks?
- Webhooks are user-defined HTTP callbacks. They’re triggered by some event, such as pushing code to a repo or updating a wiki page.
- When the event occurs, the source site makes an HTTP request to the URL configured for the webhook.
- With Azure Functions, we can define logic in a function that can be run when a webhook message is received.
- One common use of webhooks in a DevOps environment is to notify an Azure function that the code or configuration for an application has changed in GitHub.
- The payload of the message sent through the webhook contains the details of the event.
- You can use the webhook with a function to perform a task such as deploying the updated version of the application.
How can we create Azure Functions in Azure portal?
- Sign in Azure portal account, if you don’t have account, you can create Trial or free account through https://portal.azure.com/
- Under App Services, Select Create Resource and then Choose Function App Create option.
- Under Basics, you have to Select Subscription and Resource group from the dropdown list or you can create new Resource group.
- Then, Function App name should be Globally unique with no case sensitive.
- Publish, what publishing type want to publish here we select code.
- Runtime stack and version is to build functions on which language or platform, here we select .Net and version 6.0
- Select your Region, deploying Operating system.
- Select Plan Type as Consumption (Serverless) from options.
- Click Next: Hosting
- Select Storage account or create new storage account to host the u.
- Click Monitoring.
- If you build a function app for production level, then you should enable application Insights, For testing no need to enable app sights, here we select No.
- Review Details given under Review + create tab and click create
- Wait for couple of minutes, Deployment is in process. After deployment is successful Click Go to Resource.
- Click Functions from left launch bar and then click Create to Create function
- Select Development Environment as Develop in Portal and give new function name and Authorization level.
- For testing select authorization level as Anonymous, Click Create.
- Now your function created in your created Function app.
- Click Function, In Left launch menu click Code + test.
- Click Get Function URL, then copy to clipboard option to copy the app publishing url.
- Open new tab and paste the URL and followed by url ?name=your name, once you press enter the function trigger the HTTP trigger (GET and POST) event.
- If you gave Authorization level as Function, you need to give &name= your name.
- You can see the logs of result executing in Logs section.
- We successfully built and ran a functions using function app in Azure Portal
Build Functions in Visual Studio:
- You should have azure packages in visual studio, To install the Azure package open the visual studio installer and install the Azure cloud platform.
- After installing, select create new project and search as Azure you will find the Azure Functions. Click Next.
- In Next Page, you have to give Name for the Project and as well as Solution. Then click Create.
- Visual studio scaffolds the functions for our function app.
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace HttpTriggerFunctionApp
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
}
}
- In Function.cs, you can see the Authorization Level and Request Methods with the Routing of HTTP Trigger method, so it can perform GET and POST methods.
- Which you return the data is return as a response by Function (which the implementation is a Event).
- Run the function app by pressing F5, if you need to debugging then press ctrl + F5.
- Your function app will logs the functions status and details in command prompt, if there any updated.
- It shows the localhost port number and URL.
- Copy the URL and hit Enter. It will hit the debugger and we can see the name as given name (?name=__)
- Click Continue to see the output result, it will give the response body as what we returned with given name.
- You successfully ran the Serverless computing using Azure functions in Visual Studio.
Build Functions in VS code:
- To run Azure function in VS code install Azure Function or Azure Tools Extension by Microsoft.
- Click Azure Icon on Launch Tab and then Expand Workspace -> click + -> create Resource.
{
"profiles": {
"Functions": {
"commandName": "Project",
"commandLineArgs": "--port 7131",
"launchBrowser": false
}
}
}
- Basics Detail Pane will appear, Function app name, Subscription, Resource group, Platform, Version, Plan Type, Authorization Level all the details have to filled in prompt field.
- At Finally Your Function app is ready to use Function app.
- In launchSettings.json you can get the application localhost port number.
- Cli to run function app => Azure Functions: Execute function now in terminal (or) press F5 (or) go to Run -> Run without debugging
- Note : You must have .NET SDK and runtime tools and C# extension in VS code.
- Now, you can see the response body sent by the function app.
Conclusion
- Hope, You Learnt about Serverless computing, Azure Functions and Webhooks. Share to other people to know about Serverless computing using functions.
Next post will Concentrate on Azure Core Concepts and Services.
Surround yourself with people who challenge you, teach you, and push you to be your best self
Bill Gates
No Comment! Be the first one.