Azure functions (like AWS lambdas and GCP cloud functions) allow us to write serverless code literally just as functions, i.e. no need to fire up a web application or VM. Ofcourse just like Azure containers, there is a server component but we, the developer, need not concerns ourselves with handling configuration etc.
Azure functions will be spun up as and when required, meaning we will only be charged when they’re used. The downside of this is they have to spin up from a “cold” state. In other words the first person to hit your function will likely incur a performance hit whilst the function is started then invoked.
The other thing to remember is Azure functions are stateless. You might store state with a DB like CosmoDB, but essentially a function is invoked, does something then after a timeout period it’s shut back down.
Let’s create an example function and see how things work…
- Create a new Azure Functions project
- When you get to the options for the Function, select Http trigger and select Amonymous Authorization level
- Complete the wizard by clicking the Create button
The Authorization level allows the function to be triggered without providing a key. The HTTP trigger, as it sounds, means the function is triggered by an HTTP request.
The following is basically the code that’s created from the Azure Function template
public static class ExampleFunction { [FunctionName("Example")] public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("HTTP trigger function processed a request."); string name = req.Query["name"]; var requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); name = name ?? data?.name; var responseMessage = string.IsNullOrEmpty(name) ? "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); } }
We can actually run this and debug via Visual Studio in the normal way. We’ll get a URL supplied, something like this http://localhost:7071/api/Example to access our function.
As you can see from the above code, we’ll get passed an ILogger and an HttpRequest. From this we can get query parameters, so this URL above would be used like this http://localhost:7071/api/Example?name=PutridParrot
Ofcourse the whole purpose of the Azure Function is for it to run on Azure. To publish it…
- From Visual Studio, right mouse click on the project and select Publish
- For the target, select Azure. Click Next
- Select Azure Function App (Windows) or Linux if you prefer. Click Next again
- Either select a Function instance if one already exist or you can create a new instance from this wizard page
If you’re creating a new instance, select the resource group etc. as usual and then click Create when ready.
Note: I chose Consumption plan, which is the default when creating an Azure Functions instance. This is basically a “pay only for executions of your functions app”, so should be the cheapest plan.
The next step is to Finish the publish process. If all went well you’ll see everything configures and you can close the Publish dialog.
From the Azure dashboard you can simply type into the search textbox Function App and you should see the published function with a status of Running. If you click on the function name it will show you the current status of the function as well as it’s URL which we can access like we did with localhost, i.e.
https://myfunctionssomewhere.azurewebsites.net/api/Example?name=PutridParrot