Home » AzureFunctions

Tag: AzureFunctions

Weekly Azure billing report per mail with Azure functions

I already blogged about Azure functions, the billing API and a few other things. In this blog post, I’ll combine some of my previous blog posts to build an Azure function that creates a weekly billing report of an Azure subscription. To build this solution, the following steps are required:

  1. Create an Azure function
  2. Configure the CRON schedule for the Azure function
  3. Read data from the Azure Billing API
  4. Create a HTML page with the billing data
  5. Send the report via email

To implement it, I’ll use Visual Studio 2017, C# and the AzureBillingAPI NuGet package that I created.

The final solution can be found on GitHub: https://github.com/codehollow/AzureBillingFunction

Read more

Return JSON from C# Azure function

In this blog post I’ll build a simple C# Azure function that returns an object as JSON. That’s useful if you want to build a simple “API” or if you just want to return some information in a structured format. Such a function could read data from an on-premise environment and provide this data to a logic app, because it’s much easier to connect an Azure function to on-premise than a logic app.

Create a C# Azure Function

First step is to create a new C# function. I’ll use the HttpTriggerWithParameters-CSharp template and I’ll use the authorization level ‘Anonymous’ (that’s okay for this demo):
Read more

Return HTML or file content from C# Azure function

I’m currently implementing different Azure functions and these days I wanted to return a simple HTML document via an Azure function. So I did it at first for a simple (“hello world”) Azure function and used the code later in my real function.

I developed the sample online (in the Azure portal), but you can also use Visual Studio for it. Just add a new “HttpTrigger – C#” function and set the authorization level (I used anonymous – so no key is required):

After that, I changed the code of the function:

using System.Net;
using System.Net.Http.Headers;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");

    string html = @"<html>
<head><title>Hello world!</title></head>
<body>
<h1>Hello World!</h1>
<p>from my <strong>Azure Function</strong></p>
</body>
</html>";
    
    var response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = new StringContent(html);
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
    return response;
}

Read more

Azure Functions – Time Trigger (CRON) Cheat Sheet

This is a cheat sheet for CRON expressions that are used in the time triggers for Azure functions. They define how often a trigger/the Azure function should be executed (daily, hourly, every 3 months, …).

The basic format of the CRON expressions in Azure is:
{second} {minute} {hour} {day} {month} {day of the week}
e.g. 0 * * * * * (=every minute)

The following values are allowed for the different placeholders:

Value Allowed Values Description
{second} 0-59; * {second} when the trigger will be fired
{minute} 0-59; * {minute} when the trigger will be fired
{hour} 0-23; * {hour} when the trigger will be fired
{day} 1-31; * {day} when the trigger will be fired
{month} 1-12; * {month} when the trigger will be fired
{day of the week} 0-6; MON-SUN; * {day of the week} when the trigger will be fired

e.g. 3 5 * * * * defines a trigger that runs every time when the clock is at second 3 and minute 5 (e.g. at 09:05:03, 10:05:03, 11:05:03, …).

The trigger executes at UTC timezone. So for Vienna (UTC+1), a trigger at 18:00 (UTC) executes at 19:00 Vienna time (UTC+1).

Read more

Working with Azure functions (part 2 – C#)

In my first blog post about Azure functions, I created an Azure function app and a function that uses Powershell to read data from RSS and writes it to Azure Table Storage. In this post, I’ll create a C# function that reads all upcoming events of my https://www.meetup.com groups and creates an iCal file out of it.
Unfortunately it’s not possible to do that at the meetup site. What you can do is, that you (manually) subscribe to each iCal calendar of each group that you have, but that results in a lot of calendars and if you join or leave a group, you also have to add/remove the calendar subscription.

Building the C# application

I’ll at first create a simple C# application in VisualStudio and move the code later on to the Azure function. The application itself is simple and does the following steps:

  1. Read data from the meetup API
  2. Transform the data to an event object
  3. Create an iCal file

To achieve that, I’ll at first add the NuGet packages “Ical.Net” and “Newtonsoft.Json” to my console application.

20161109_01_nugetpackages

Read more

Working with Azure functions (part 1 – Powershell)

Azure functions, also called Azure function apps, are a great way to build simple components – functions – and run them in the cloud (also called “serverless computing” or FaaS). Those functions are triggered via timer, http trigger, webhooks and many others. The functions itself can be implemented in one of the following languages: C#, F#, JavaScript/Node.js, PHP, Powershell, Python, Batch, Bash.

It’s important to mention, that functions have a timeout of 5 minutes – so if you have an endless running job, then you should go for webjobs. The idea behind Azure functions is, that you execute just a small piece of code. That’s why there is this timeout. Running those small pieces is very cheap. The first 1.000.000 executions and the first 400.000 GB/s of execution are for free. 400.000 GB/s means: Let’s assume you have a memory size for your function app of 512 MB: 400.000*1024 / 512 = 800.000 seconds are for free. So you can execute your function 1 Mio times with an average execution time of 1.25 seconds and it’s still free.

I will use the Azure functions to build two “applications”/functions. One of them will read data from my RSS feed and write it to my table storage. The second one will read all my upcoming events from https://www.meetup.com/ and create an iCal file out of it so that I can add it to my calendar.

Read more