Running Azure Functions Locally with the CLI and VS Code

Today I would show a little bit, how I work with VS Code and creating with it Azure Functions (that is now generally available).

For a long time the one and only test was to copy the code into the azure portal and hit the run button (maybe with test input data). But when you develop an sheduled process or a waiting on an upload onto a blob store, this will be tricky. For thie Microsoft has published the azure-functions-cli. To Install them you must have installed the npm package manager.

The First Function

Now it’s time to create a function. First of all navigate to the directory where you want to create the functions, in this you must execute the init command to create the function repo.

After that you will see two files in the folder:

  • host.json
  • appsettings.json

With these files, you will have foll control of the behaviour of your functions.

Appsettings

In this file you can set connectionstring to blob stores (e.g. emulator), or database connections. But you are free to set other settings like in an webapplication. A sample looks like:

Hostsettings

With these file you have the full controll of the behavior of the complete function app. So in this you cann set the polling intervall for Queues or the route prefix for an http function. The following snipplet shows the complete possibility with comments.

The first function

Now it’s time to create a function. I describe here  a simple httpTrigger. To create one you only must execute an new command in the function repo.

This will start yeoman a nice too to create a quickstart skeleton. So for the function app.

FuncNew

First it ask you in wich language you will create your function. The next step shows you a selection of trigger in this I use the HttpTrigger. Next you must give it a name like “MyHttpTrigger” and that’s it. you now have your first app created.

Running and Editing in VS Code

Now we wan’t to edit the function, for this you can open it up with the code command.

Next you go to View – > Integrated Terminal. This starts a PowerShell in VS Code in ich you then execute the function with the run command.

After the run command you must tell him what function you will run. In my case “MyHttpTrigger” but you must tell him not the name only, you must tell him the relative path. With the -c argument you can send arguments into the function too.

On the first run you will be promted with an message to startup an server that will “host” the function and can execute it. After tell him to start it you will get a response from you function that say Hello to you hooray!

Hello-response

Debugging my functions

So here is a MAAAAJOR Problem. At the moment, you cannot debug a c# function in vs code. To debug c# functions you must follow the instruction on https://blogs.msdn.microsoft.com/webdev/2016/12/01/visual-studio-tools-for-azure-functions/.

But when you decided to create functions with nodejs you can execute the run command with the option –debug

This execute the fucntion and wait for you to hit the F5 Button in VSCode. After doing the, you must press any key to continiue the processing in the Integated Terminal. And BAM it hits your breakpoints.

Breakpoint

Conclusion

Azur Functions is the best way to create microservices that may work as dispatcher, or as a part of an enterprise application. But the tool support in VS Code is very sad especcialy for other languages like nodejs. Also the intellisense does not work properly for c#, this will slow the developement process down. But the advantage, you learn the complete api. BTW you can Azure Functions in your Logic Apps to for transforming data or s.th.