Azure Functions is a perfect solution for web jobs or scripts which takes a small amount of time to execute and are invoked occasionally or scheduled for a frequent run.
One way how to start develop Azure Function is to do it straight from the GUI on Azure Portal. But I would rather suggest starting locally as it will be easier to debug the function, at least in the beginning.
The first thing we should do is to npm install globally the Azure Functions Core Tools v2 from Microsoft. Make sure you install v2 which is a self-contained cross-platform package and will work on Windows, Mac, and Linux.
The Azure Functions Core Tools provide a local development experience for creating, developing, testing, running, and debugging
More info about the package you can find on github https://github.com/Azure/azure-functions-core-tools
After we have installed the tools we can start to build our first function. First thing is to create a new Function App in the current folder by running
func init from a command line. By default, this will also create a local git repository which we can skip by passing a parameter to CLI command
func init -n
Next step is to create the function itself. By executing
func new we will enter the function creation wizard where we will be asked to:
- select a template (in this post we will focus on Http invoked function)
- provide function’s name
This is how the function’s code looks after we have created it from a template.
We can now start the function by executing
func start This command will startup Function App locally and provide us with the url which will trigger the function in the screenshot above.
What the function does when invoked from Http is retrieving request data via
req parameter and looks for
name parameter in request’s query or body. If found it then returns status 200 and adds some text to the response, see
If the name is not found then the function returns status 400 and message to provide a name.
The context parameter provides us with a runtime's context object which can be used to pass and receive data from the function and to communicate with the runtime. You can read more about context object from official docs https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#context-object
One thing to notice is that we must explicitly call
context.done() at the end of our function to inform the runtime that the function has done its job. Without calling it the runtime won’t know whether the function has completed or not and the function’s execution will time out eventually. Also, without calling
context.done() we won’t receive a response.
So this is the bare minimum to start playing around with Azure Functions which is one of the many Serverless options out there in a market. In next posts, I will show you how to debug the functions, communicate between functions and publish the Function App to Azure from VS Code.