Introduction to Azure Functions
Azure Functions is described as a event-driven, serverless compute experience. It is Microsoft’s entry into the world of serverless sitting next to other solutions such as AWS Lambda. In this post I will provide a brief introduction to Functions and show you how to use them.
Languages
The first impressive feature of Functions is the language support. I mean it even supports bash. For a full updated list of supported languages please see the documentation.
Do however keep in mind that a few of these languages are only experimental and do not have access to the full list of triggers or bindings.
Triggers
A trigger is required to run the Function. Azure Functions comes with a useful list of different triggers for your Function. Below I will cover some of the more common triggers:
HTTP Trigger
This is probably the most common trigger. The HTTP trigger gives your function a URL. Once the URL is accessed it will execute your function making the request available as a parameter.
Most of the experimental languages have at least the HTTP trigger available.
Timer Trigger
The timer trigger allows you to specify set intervals at which to execute the function. This seems like a great replacement for the classic cron job as it also removes the processing off of your production server.
You are even able to specify the interval with a CRON expression like the one below:
|
|
The above expression will run your function every minute.
Event Hub Trigger
This trigger allows you to respond to messages from Azure Event Hubs. This makes it quite easy to implement scalable background processing into existing applications.
Bindings
Bindings make it easy to connect to various sources of data from within your Function. This includes databases, file systems, or even transactional email services.
The documentation contains a handy table showing all the available bindings.
Cost
Currently Azure Functions has 2 pricing plans available. Each has its own pros and cons.
Consumption Plan
With this pricing plan you can think of your Function as this tiny bit of code magically running in the ether when needed. Azure will provide all the required resources. Which means that you no longer need to worry about resource management. You are simply billed for the time your Function runs.
App Service Plan
If you already have an app service (web, mobile or API app) then you might want to consider the App Service plan. Azure will use your existing app service plan to run your Function. Using this plan you there is no additional cost in using Azure Functions.
Your First Function
Before yesterday only Windows users had an easy way to emulate and work with Functions. Luckily some work has been done on porting the Azure Functions CLI to .Net Core. Linux and Mac users can now also use the CLI tool and develop their functions locally.
To install the tool, simply run the following command:
|
|
Now you can run func
to be greeted by the coolest ASCII logo I have
ever seen:
To initialise your first Function run the following command:
|
|
The commands above will set up the root of your Function. The local.settings.json file is where you can manage local app settings, connection strings and settings for the CLI. When you push your application to Azure you have the option of publishing the settings contained in this file to the Azure hosted Function.
To create your first function run the following command:
|
|
This command will create a new directory containing example code for your first JavaScript Azure Function.
To run this function locally simple run the following command in the root of your project:
|
|
You should see output similar to this:
To see your function in access visit http://localhost:7071/api/MyHTTPTrigger?name=Zander
.
Keep in mind that the port might be different when you start your function.
Finally, if you want to publish your Function to Azure you can run the following command:
|
|
For more information on this command please see the documentation.
Conclusion
As you can see it is super easy to get started with Azure Functions. I am however only scraping the surface in this post. I recommend you check out the official documentation for more information.