What is Azure Function?
Tags: azure, azurefunctions, programming, tutorial
Published: 2023-09-24 06:08:19

In this blog post, I will write about the Azure function, a serverless computing service that allows you to run **event-driven** code without worrying about servers, infrastructure, or scaling. I will explain what is Azure function, when we can use it, and how to create and trigger it with an HTTP request. I will also provide a sample Azure function that uses C# language. At the end, I will give a conclusion about Azure function.

### What is Azure function?
Azure function is a part of Azure Functions, a serverless solution that lets you write less code, maintain less infrastructure, and save on costs. Azure function is a piece of code that runs in response to a variety of events, such as HTTP requests, timers, queues, blobs, and more. You can write Azure functions in multiple languages, such as C#, Java, JavaScript, PowerShell, Python, and others. You can also use different tools to develop and debug Azure functions, such as Visual Studio, Visual Studio Code, Maven, and command line.

### When can we use Azure function?
You can use Azure function for many scenarios that require event-driven logic and integration with other services. For example, you can use Azure function to:

- Process file uploads: Run code when a file is uploaded or changed in blob storage.
- Process data in real time: Capture and transform data from event and IoT source streams on the way to storage.
- Infer on data models: Pull text from a queue and present it to various AI services for analysis and classification.
- Run scheduled tasks: Execute data clean-up code on pre-defined timed intervals.
- Build a scalable web API: Implement a set of REST endpoints for your web applications using HTTP triggers.
- Build a serverless workflow: Create an event-driven workflow from a series of functions using Durable Functions.
- Respond to database changes: Run custom logic when a document is created or updated in Azure Cosmos DB.
- Create reliable message systems: Process message queues using Queue Storage, Service Bus, or Event Hubs.

### How to create and trigger an Azure function with an HTTP request?
To create an Azure function with an HTTP trigger, you need to follow these steps:

- Create a function app in Azure portal or using **Azure CLI**. A function app is a container that hosts one or more functions.
- Create a new function in the function app using the HTTP trigger template. You can choose C# as the language for your function code.
- Write the logic for your function in the code editor. You can use the `HttpRequestData` and `HttpResponseData` classes to access the HTTP request and response objects.
- Save and test your function using the Test/Run panel or using curl or Postman. You can copy the URL of your function from the Function URL field.

Here is an example of how to create a function app using **Azure CLI**:

```powershell
# Function app and storage account names must be unique.
# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="eastus"
resourceGroup="msdocs-azure-functions-rg-$randomIdentifier"
tag="create-function-app-consumption"
storage="msdocsaccount$randomIdentifier"
functionApp="msdocs-serverless-function-$randomIdentifier"
skuStorage="Standard_LRS"
functionsVersion="4"

# Create a resource group
echo "Creating $resourceGroup in "$location"..."
az group create --name $resourceGroup --location $location --tags $tag

# Create an Azure storage account
echo "Creating $storage storage account..."
az storage account create --name $storage --location $location --resource-group $resourceGroup --sku $skuStorage

# Create a serverless function app
echo "Creating $functionApp function app..."
az functionapp create --name $functionApp --storage-account $storage --consumption-plan-location $location --resource-group $resourceGroup --functions-version $functionsVersion
```

Here is an example of a simple C# Azure function that returns "Hello World" when triggered by an HTTP request:

```csharp
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;

namespace HttpExample
{
    public static class HttpTrigger
    {
        [Function("HttpTrigger")]
        public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequestData req,
            FunctionContext executionContext)
        {
            var response = req.CreateResponse(HttpStatusCode.OK);
            response.WriteString("Hello World");
            return response;
        }
    }
}
```

### Conclusion
Azure function is a powerful and flexible way to run event-driven code in the cloud without managing servers or infrastructure. You can use it for various scenarios that require integration with other services and data sources. You can also write it in your preferred language and tool. Azure function helps you to focus on the code that matters most to you and save on costs.