Serverless computing was out there for a long time. Microsoft Azure had adapted the concept with Azure Functions, Logic Apps and Event Grids.
We can use Azure Functions for event driven workloads with short lived processes. That means there should be a trigger that will initiate the function. For an example,
- Blog is added to your container
- Timer tick
- New item added to queue
- HTTP request
- etc..
This is the first post of a serious of articles on Azure Functions/Durable functions.
- Posting data to Azure Function using HTTP trigger with .NET 6
- Azure Durable Functions to implement function chaining with custom data
- Azure Durable Functions with retry options and retry operations
In this post I'll guide how to respond to HTTP post request with an Azure Function.
By the way shouldn't I be using a RESTful Web API in Azure Web App instead of Azure Function?
That can also be an option. But let's think about it's usage. If we have a small and well defined and short lived component, what's the harm of using a function. And I'll extend this article into another blog post to illustrate some cool features Azure Functions can bring to the table.
Let's start the journey
Step 1: Create Azure Function App in Azure portal. I use .Net as the runtime and 6 as the version
Step 2: Open the VSCode in your directory and install following extensions if you had not already installed
Step 3: Let's navigate to Azure section and login to your subscription. Then click on the Azure Function App we just created. You can see the Function App
Step 4: Let's add our first Function there. Press F1 key and select Azure Functions: Create Function option
Step 5: This is the generated code with the scaffolding
namespace Company.Function
{
public static class CustomerTrigger
{
[FunctionName("CustomerTrigger")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Function, "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);
}
}
}
Step 6: Let's add another class called Customer
Step 7: Let's modify the Function a bit now
public static class CustomerTrigger { [FunctionName("CustomerTrigger")] public static async Task
Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); Customer customer = JsonConvert.DeserializeObject (requestBody); return new OkObjectResult(customer.name); } } Step 8: Let's deploy the function to our Function App
Step 9: Great!! our function is deployed now. Let's get the URL
Step 10: Let's try this with Postman
Great!! Our sample is working perfectly !!