Over a million developers have joined DZone.

Azure Functions [Snippet]

DZone's Guide to

Azure Functions [Snippet]

Azure Functions is Microsoft Azure's serverless offering. Here, we set up a simple function to check a URL on a given schedule.

· Cloud Zone ·
Free Resource

Learn how to migrate and modernize stateless applications and run them in a Kubernetes cluster.

I recently blogged about using Azure Web Jobs. Well, Azure Functions is another way of doing the same thing. Let's look at how they work.

To create an Azure Function, go to the Azure portal, click "Add new", and search for "Function App".

Give your app a name and select the usual resource group and location settings.

Now when you open Function Apps, you should see your new app.

I want my Function App to run on a schedule, so I clicked the + next to functions and select TimerTrigger. I am a C# programmer, so I selected this option as well.

Give your function a name and specify, using the usual Cron notation, how often it should run. I want mine to run at 9:30 p.m. each night, so use 0 30 21 * * *

Now comes the code bit. By default, you get a window with the following code in it:

using System;

public static void Run(TimerInfo myTimer, TraceWriter log)
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");

It is entirely up to you what you get your function to do. In my case, I just wanted to call a URL on a schedule, so I created some code that used HttpClient.

using System;
using System.Net.Http;

public static async Task Run(TimerInfo myTimer, TraceWriter log)
    log.Info($"Buffer 0 function executed at: {DateTime.Now}");
    HttpClient client = new HttpClient();
    var result = await client.GetAsync("URL");
    string resultContent = await result.Content.ReadAsStringAsync();

Once you have created your app and it has run, you can use the monitor section to view success and failures.

There is loads more you can do with Azure Functions, but this is a good place to start.

Join us in exploring application and infrastructure changes required for running scalable, observable, and portable apps on Kubernetes.

cloud ,azure functions ,serverless functions ,c#

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}