Create Your First Azure Function: Code Walkthrough
This guide will walk you through creating a simple Azure Function using code, demonstrating the core concepts and development workflow.
Note: This tutorial assumes you have the Azure Functions Core Tools installed and a preferred code editor ready.
Step 1: Initialize Your Project
Open your terminal or command prompt and navigate to the directory where you want to create your function app.
func init MyFirstFunctionApp --worker-runtime dotnet --docker
This command initializes a new function app project named MyFirstFunctionApp
using the .NET runtime. The --docker
flag adds a Dockerfile for containerization.
Step 2: Create Your First Function
Navigate into your project directory and create a new HTTP-triggered function:
cd MyFirstFunctionApp
func new --name HttpTriggerFunction --template "HTTP trigger" --authlevel "anonymous"
func new
: Command to create a new function.--name HttpTriggerFunction
: Sets the name of your function.--template "HTTP trigger"
: Specifies the type of trigger.--authlevel "anonymous"
: Configures the authentication level (anonymous means no API key is required).
Step 3: Explore the Generated Code
Open the HttpTriggerFunction
folder in your code editor. You'll find a run.csx
(for C# Script) or HttpTriggerFunction.cs
(for compiled C#) file containing the function logic.
C# Example (Compiled)
HttpTriggerFunction.cs
:
using System;
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 MyFirstFunctionApp
{
public static class HttpTriggerFunction
{
[FunctionName("HttpTriggerFunction")]
public static async Task 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);
}
}
}
Step 4: Run Your Function Locally
From the root of your function app project, run the following command:
func start
This will start the local Functions runtime. You'll see output indicating your function is running and the URL it's listening on, typically something like http://localhost:7071/api/HttpTriggerFunction
.
Step 5: Test Your Function
You can test your function using a web browser, curl
, or tools like Postman.
Using a Web Browser:
Navigate to http://localhost:7071/api/HttpTriggerFunction?name=AzureUser
.
Using curl
:
curl "http://localhost:7071/api/HttpTriggerFunction?name=World"
For a POST request with a JSON body:
curl -X POST "http://localhost:7071/api/HttpTriggerFunction" -H "Content-Type: application/json" -d '{"name": "Developer"}'
Tip: Experiment with sending different names in the query string or request body to see the personalized responses.
Next Steps
Congratulations! You've created and run your first Azure Function. Explore other triggers and bindings, and learn how to deploy your function to Azure.