Creating Your First Azure Function
This guide will walk you through the process of creating a simple Azure Function using the Azure portal. Azure Functions is a serverless compute service that enables you to run code on demand without explicitly provisioning or managing infrastructure.
Prerequisites
- An Azure account. If you don't have one, you can sign up for a free account.
- An Azure Resource Group. You can create one in the Azure portal.
Log in to the Azure portal. In the search bar at the top, type "Functions" and select "Functions" from the results.
On the Functions page, click the + Create button.
Fill out the following details:
- Subscription: Select your Azure subscription.
- Resource Group: Choose an existing resource group or create a new one.
- Function App name: Enter a globally unique name for your function app.
- Runtime stack: Select your preferred programming language (e.g., Node.js, C#, Python).
- Version: Choose the version of the runtime.
- Region: Select the Azure region closest to you or your users.
- Operating System: Select Windows or Linux.
- Hosting Plan: Choose Consumption (serverless) for pay-per-execution, or App Service Plan for more predictable pricing. For this guide, Consumption is recommended.
Click Review + create, then Create.
Once your Function App is deployed, navigate to it. From the Function App menu, select Functions under "Functions". Then, click + Create.
You'll be presented with several template options. For a simple "Hello World" example, choose the HTTP trigger template.
Configure the function:
- New Function: Give your function a descriptive name (e.g., HttpTriggerExample).
- Authorization level: For testing, select Anonymous. In production, consider Function or Admin.
Click Create.
After the function is created, you'll see its details page. Click on the Get Function URL button. This will provide you with the URL to trigger your function.
Copy the URL and open it in your web browser or use a tool like cURL or Postman. You can also add a query string parameter like ?name=World to the URL.
For example, if your function URL is https://your-function-app-name.azurewebsites.net/api/HttpTriggerExample, you can test it by visiting:
https://your-function-app-name.azurewebsites.net/api/HttpTriggerExample?name=Azure
You should see a response similar to:
Hello, Azure. This HTTP triggered function executed successfully.Understanding the Code
The default HTTP trigger template usually includes code that checks for a name parameter in the request. If found, it uses it in the response; otherwise, it prompts you to provide one.
Here's a simplified example of what the C# code might look like:
using System.IO;
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 Company.Function
{
    public static class HttpTriggerExample
    {
        [FunctionName("HttpTriggerExample")]
        public static 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 = new StreamReader(req.Body).ReadToEnd();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;
            return name != null
                ? (ActionResult)new OkObjectResult($"Hello, {name}. This HTTP triggered function executed successfully.")
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
        }
    }
}Now that you've created your first function, explore other trigger types like Timer or Blob trigger, and learn how to integrate with other Azure services using bindings.