0
0
AzureHow-ToBeginner · 4 min read

How to Create Azure Function: Simple Steps for Beginners

To create an Azure Function, use the Azure Portal or Azure CLI to set up a Function App, then add a function with a trigger like HTTP. This lets you run small pieces of code in the cloud without managing servers.
📐

Syntax

Creating an Azure Function involves these main parts:

  • Function App: The container for your functions.
  • Function: The actual code that runs on a trigger.
  • Trigger: What starts the function (e.g., HTTP request, timer).

Using Azure CLI, the basic commands are:

  • az functionapp create - creates the Function App.
  • az functionapp function create - adds a function (optional, often done in code).
bash
az functionapp create --resource-group <resource-group> --consumption-plan-location <location> --runtime dotnet --functions-version 4 --name <function-app-name> --storage-account <storage-account-name>
💻

Example

This example shows how to create a simple HTTP-triggered Azure Function using Azure CLI and C# code.

First, create the Function App with Azure CLI, then add a function that responds to HTTP requests.

bash and csharp
az group create --name MyResourceGroup --location eastus
az storage account create --name mystorageacct123 --location eastus --resource-group MyResourceGroup --sku Standard_LRS
az functionapp create --resource-group MyResourceGroup --consumption-plan-location eastus --runtime dotnet --functions-version 4 --name MyUniqueFunctionApp123 --storage-account mystorageacct123

// Example C# function code (HttpTrigger.cs):
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using System.Net;

public static class HttpTrigger
{
    [Function("HttpTrigger")]
    public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
    {
        var response = req.CreateResponse(HttpStatusCode.OK);
        response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
        response.WriteString("Hello from Azure Function!");
        return response;
    }
}
Output
HTTP 200 OK Content-Type: text/plain; charset=utf-8 Hello from Azure Function!
⚠️

Common Pitfalls

Common mistakes when creating Azure Functions include:

  • Not creating a storage account before the Function App, which is required.
  • Using a non-unique Function App name, causing creation failure.
  • Forgetting to set the correct runtime version matching your code.
  • Not setting proper authorization levels, leading to access issues.

Always check Azure CLI output for errors and verify resource names are unique.

bash
az functionapp create --resource-group MyResourceGroup --consumption-plan-location eastus --runtime dotnet --functions-version 4 --name MyFunctionApp --storage-account mystorageacct

# Wrong: storage account 'mystorageacct' might not exist or name is taken

# Right:
az storage account create --name mystorageacct123 --location eastus --resource-group MyResourceGroup --sku Standard_LRS
az functionapp create --resource-group MyResourceGroup --consumption-plan-location eastus --runtime dotnet --functions-version 4 --name MyFunctionApp123 --storage-account mystorageacct123
📊

Quick Reference

Summary tips for creating Azure Functions:

  • Always create a resource group first with az group create.
  • Create a storage account before the Function App.
  • Use unique names for Function Apps and storage accounts.
  • Choose the correct runtime and version for your code.
  • Deploy your function code using tools like VS Code or Azure CLI.

Key Takeaways

Create a resource group and storage account before the Function App.
Use Azure CLI or Portal to create a Function App with a unique name.
Choose the right runtime and version matching your function code.
Functions run on triggers like HTTP requests without managing servers.
Check for errors and naming conflicts during creation to avoid failures.