Replace your old cronjobs with free Azure Functions within 5 minutes

For some of my older projects, I had some repetitive tasks where a simple job triggers a script to run and loadsome data from the web. For this, I used good old cronjobs like the ones cronjob.de offers. As this was expensive und quite unsatisfying in terms of flexibility, I decided to move them over to Azure, which just took me five minutes and cost me nothing.

My old cronjobs just did a super simple thing: They called a PHP script every 15 minutes, which was hosted at a hosting provider and did some work for me. So the cronjobs were simple HTTP calls and I had no feedback about results, success or failure. So it was time to move the over to Azure Functions.

Create an Azure Function App

Azure Functions are the Serverless solution that Microsoft offer in their cloud. In a Serverless context, you even go one step further than PaaS does and only provide some code to the cloud that should run, when you want it to. You don’t care about the architecture beneathit and just want your code to run. Perfect for recurring jobs.

To create a new Azure Function, log into the Azure Portal, select Add> Compute> Function App and follow the wizard to create a servicefor your functions](http://pumpingco.de/content/images/2017/01/create-azuere-function.png)

Add your first function

Once Azure has finished creating your Function App, you can create your first function in it. The quickstart guides you through the creation of your first one. As we want to create a cronjob equivalent, the Timerscenario looks like the best one for us. As a .NET guy, I choose C# as my language, but you can also pick JavaScript is you like.

As soon as the function got created, we can start coding. To replace my existing cronjobs, I wrote the following lines.

using System; using System.Net; using System.Net.Http; public static async void Run(TimerInfo myTimer, TraceWriter log) { log.Info($"Job started at: {DateTime.Now}"); using (var httpClient = new HttpClient()) { var result = await httpClient.GetAsync(""); if (result.StatusCode == System.Net.HttpStatusCode.OK) log.Info("Job ran successfully"); else log.Error("Job failed. Status Code: " + result.StatusCode); } }

Define the trigger

Now that the logic is created, we have to define a trigger for the function. At the side menu, we can find several settings. At the Integratetab, we can define triggers. Because we chose the Timer template earlier, a timer trigger has already been created for us. In the Schedulebox, we can define the trigger time formatted as aCron expression. The predefined0 */5 * * * * will trigger the function every 5 minutes, so let’s increase it to 15 minutes:0 */15 * * * *](http://pumpingco.de/content/images/2017/01/azure-function-timer.png)

Test your function

To test what we created, get back to the Developtab and open the Logsby clicking at the according button at the top. Now we can Runthe function and follow its process in the Logs window. For more details, you can visit the Monitortab in the menu](http://pumpingco.de/content/images/2017/01/azure-function-runlogs.png)

What does it cost?

Nothing. For the moment, at least. As we chose the Consumption Plan as the Hosting plan for our function when creating them, we will be billed on a pay-per-use mode](http://pumpingco.de/content/images/2017/01/azure-function-pricing-new.png)

The price for execution time is $0.000016/GB-s and $0.20 per Million executions. For us it is still free, as the first 400,000 GB-s or the first 1 Million executions come forfree everymonth. If your function got 1 GB of RAM attached and runs for one second, Microsoft counts this as a GB-s.

Does it scale?

It does and this is one of the coolestthings about Azure Functions. Up to a specific (very large) size, these functions scale automatically and provision the best possible compute base automatically to keep them performant. But please keep in mind that the programming model for Azure Functions is stateless and there are VMs running below, that are not visible for you. So state-inconsistency and VM startup time is something you might have to consider.

What’s next?

If you liked the concept of Azure Functions, why don’t you create more? There are many more triggers beside the timer like WebHooks, EventHubs, or Blob Storage changes. Of yourse, you can alos trigger a function manually, with the REST API. You can also define outputs forthe results of your functionsto use them for processing data.

So give it a try and fall in love with Serverless Computing!