Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Functions: Nodejs, What are restrictions / limitations when using file system?

I have not been able to get an azure function working that uses the node file system module.

I created a brand new function app with most basic HTTP trigger function and included the 'fs' module:

var fs = require('fs');

module.exports = function (context, req, res) {
    context.log('function triggered');
    context.log(req);

    context.done();
}

This works fine. I see the full request in live streaming logs, and in the function invocation list.

However, as soon as I add the code which actually uses the file system, it seems to crash the azure function. It neither completes or throws the error. It also doesn't seem to show up in the azure function invocations list which is scary since this is loss of failure information and I might think my service was running fine when there were actually crashes.

var fs = require('fs');

module.exports = function (context, req, res) {
    context.log('function triggered');
    context.log(req);

    fs.writeFile('message.txt', 'Hello Node.js', (err) => {
        if (err) throw err;
        console.log('It\'s saved!');
        context.done();
    });
}

The fs.writeFile code taken directly from the node.js website: https://nodejs.org/dist/latest-v4.x/docs/api/fs.html#fs_fs_writefile_file_data_options_callback

I added the context.done() in the callback, but that snippet should work without issue on normal development environment.

This brings up the questions:

  • Is it possible to use the file system when using Azure Functions?
  • If so, what are the restrictions?
  • If no restrictions, are developers required to keep track and perform cleanup or is this taken care of by some sandboxing?

From my understanding even though this is considered server-less computing there is still a VM / Azure Website App Service underneath which has a file system. I can use the Kudu console and navigate around and see all the files in /wwwroot and the /home/functions/secrets files.

Imagine a scenario where an azure function is written to write a file with unique name and not perform cleanup it would eventually take up all the disk space on the host VM and degrade performance. This could happen accidentally by a developer and possibly go unnoticed until it's too late.

This makes me wonder if it is by design not to use the file system, or if my function is just written wrong?

like image 878
Matt Mazzola Avatar asked Aug 30 '16 04:08

Matt Mazzola


1 Answers

Yes you can use the file system, with some restrictions as described here. That page describes some directories you can access like D:\HOME and D:\LOCAL\TEMP. I've modified your code below to write to the temp dir and it works:

var fs = require('fs');

 module.exports = function (context, input) {
    fs.writeFile('D:/local/Temp/message.txt', input, (err) => {
    if (err) {
        context.log(err);
        throw err;
    }
    context.log('It\'s saved!');
        context.done();
    });
}

Your initial code was failing because it was trying to write to D:\Windows\system32 which is not allowed.

like image 189
mathewc Avatar answered Sep 19 '22 03:09

mathewc