Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Discover a centralized approach to monitor your virtual infrastructure, on-premise IT environment, and cloud infrastructure – all on a single platform.

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();
    log.Info(resultContent);
}


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.

Learn how to auto-discover your containers and monitor their performance, capture Docker host and container metrics to allocate host resources, and provision containers.

Topics:
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 }}