Handling your local directory in Azure Functions

Since Azure Functions are not a typical(and least in terms how they're executed) application, you may from time to time face a problems, which are not a case in more common solutions. One of those is a problem with storing local files. Consider following snippet:

/
using System.Net;
using System.IO;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    File.WriteAllText("test.txt", "Foo");

    return req.CreateResponse(HttpStatusCode.OK, "Hello ");
}

when you run this code in as your function you will get following error:

/
2017-05-17T06:28:37.775 Exception while executing function: Functions.HttpTriggerCSharp1. mscorlib: Access to the path 'D:\Windows\system32\test.txt' is denied.

This is because if you don't provide a full path to your file, the default working directory of the process, inside which your functions runs, is D:\Windows\system32. Fortunately there's an easy fix to this.

Finding your local directory

The receipt to get your local directory is really simple. There's one thing to remember - your function app is hosted inside a following directory: D:\home\site\wwwroot. When you look at it, you can expect following structure:

/
home
	- site
		- wwwroot
			- Function1
			- Function2
			- ...

All we need to do here is to combine main directory path with the name of the function. That way we can modify the snippet from above slightly:

/
using System.Net;
using System.IO;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    File.WriteAllText("D:\\home\\site\\wwwroot\\HttpTriggerCSharp1\\test.txt", "Foo");

    return req.CreateResponse(HttpStatusCode.OK, "Hello ");
}

and when we run it, you should see following output:

/
2017-05-17T06:43:29.408 Function started (Id=1baa9a60-d549-492d-8fef-089cd8e8de06)
2017-05-17T06:43:29.408 Function completed (Success, Id=1baa9a60-d549-492d-8fef-089cd8e8de06, Duration=4ms)

 

 

Proper deployment of your Function App using ARM template

In one of my projects I'm using ARM template to deploy both App Service plan and Function App to Azure. The important thing here is the fact, that I'm using a Free tier, so e.g. Always On property is not available for me. In this post I'll show you a small gotcha, which made my functions work improperly.

The structure

Currently the part of my project we're talking about looks like this:

  • App Service plan(Free)
  • Function App containing two functions - one is triggered every 5 minutes, fetches data from a FTP server and pushes it to a queue, the second one receives items from a queue and pushes them to a storage

Really simple, what could wrong here? As you probably know, Function Apps have two modes when it comes to choosing a hosting plan:

  • Consumption Plan(you pay for what you've used)
  • App Service Plan(predefined capacity allocation)

One important thing here is one of features according to Consumption Plan - with it you don't need Always On enabled for your Function App because each time a function is triggered, it is automatically awaken and processed. If you choose App Service Plan, you have to have Always On enabled - if not, your functions may work only occasionally. Since tier Free doesn't allow you to turn on this feature, using wrong consumption plan really breaks your setup.

The problem

When working on the mentioned projected during the last weekend I realized, that when I have a one day break from work, storage is missing items from that day. When I accessed functions to check whether everything is all right, missing items were immediately pushed to it. The same was true for Application Insights attached to my Function App - there was a big gap in the data collected. The conclusion was simple - there's something wrong with a hosting plan. I had to double-check my ARM template.

The template & solution

The part of the template responsible for provisioning my Function App looked like this:

/
{
  "name": "[parameters('liczniknetFunctionAppServicePlanName')]",
  "type": "Microsoft.Web/serverfarms",
  "location": "[resourceGroup().location]",
  "apiVersion": "2014-06-01",
  "dependsOn": [],
  "tags": {
	"displayName": "liczniknet-functionapp-serviceplan"
  },
  "properties": {
	"name": "[parameters('liczniknetFunctionAppServicePlanName')]",
	"workerSize": "[parameters('liczniknetFunctionAppServicePlanWorkerSize')]",
	"numberOfWorkers": 1
  }
}

For some reason I was completely sure, that the default mode for a Function App will be Consumption Plan. There's one small thing - when a Service App plan is being provisioned, it's not aware of resources, which will be deployed further. The solution was quite simple, I was missing two additional properties:

/
{
  "name": "[parameters('liczniknetFunctionAppServicePlanName')]",
  "type": "Microsoft.Web/serverfarms",
  "location": "[resourceGroup().location]",
  "apiVersion": "2014-06-01",
  "dependsOn": [],
  "tags": {
	"displayName": "liczniknet-functionapp-serviceplan"
  },
  "properties": {
	"name": "[parameters('liczniknetFunctionAppServicePlanName')]",
	"computeMode": "Dynamic",
	"sku": "Dynamic",
	"workerSize": "[parameters('liczniknetFunctionAppServicePlanWorkerSize')]",
	"numberOfWorkers": 1
  }
}

With this new setup my Function App is deployed with a proper Hosting Plan and I don't have to awake it manually.