Beginner’s guide to Azure Functions — Part 1
This article is part 1 of series of articles.
In this part, we will first go through basic overview of Azure functions and then we will see how HTTP Triggers and Queue Bindings work with a simple demo.
In coming parts, we will extend the demo to see other types of Triggers and Bindings. We will also see how to deploy Azure functions in a later article.
Why Azure functions?
Azure functions are used to run small pieces of code to process some data on a trigger. Output binding can be used to store the data for further processing.
The coolest feature about Azure functions is that you do not need to worry about the infrastructure, scaling and provisioning. Developers have to just focus on writing the code(in any of the supported language) and deploying it to Azure.
Another thing which I like about Azure functions is that you only pay for the time your code runs and if scaling is required then Azure automatically takes care of that.
Prerequisites for Part 1
- Visual Studio 2017 or higher with Azure Cloud SDK
- Azure Storage emulator
- Basic knowledge about C# and Azure Storage Queue
- Postman
What are we going to implement?
Here in this part, we are going to implement a very basic flow where an Azure function with HTTP Trigger will store the marks of the students in a Storage Queue for further processing.
In the next part we will see how to process the queue and store the results in BLOB storage.
Before starting on implementation part, let’s understand what we mean by HTTP trigger. The HTTP trigger lets you invoke a function with an HTTP request i.e. a function will get triggered on an HTTP request.
The storage queue on the other hand is just a container which holds the message so that it can be picked up by some other function or service to process further.
I guess, it’s right time to jump in to the implementation
Step 1 - Add a new function
Open Visual studio and create a new project. Select Cloud from menu and select Azure Function. Give name as ResultProcessor and click OK as below -
Next select HTTP Trigger with local storage emulator and access rights as Function as below —
Note — If using version v1 to create an Azure function, then use 1.0.11 version of Microsoft.NET.Sdk.Functions package.
Access rights “Function” means that when this function is deployed on Azure, we need a code to access this function. We will see this in a later article. But for checking the function locally, we do not require any code i.e locally it would work similar to Anonymous access rights, this we will see in this article. Click OK and then rename the function as SubmitMarks.
Step 2 - Create HTTP input trigger
Before proceeding let’s add a class StudentMarks which will contain only two properties as below -
Now we have an HTTP input triggered function, we will work on the post request and leave the route as null for this demo. We will also store the request body to StudentMarks object as below -
Step 3 - Testing HTTP Trigger
Now we have implemented our function which will get trigger on HTTP request. Before proceeding let’s test this locally. Run this function in Visual studio and open Postman and hit the function URL —
Once we click send, our function will get triggered and show the message as below -
Step 4 - Storing to local queue storage
We will now modify the code so that the the submitted marks are stored in a Queue for further processing. This means we need to add an output Binding to a Queue as below -
In this demo, we are going to use Storage Emulator, so before running the function ensure that local.settings.json file has the connection setting to local Storage Emulator —
Step 5 - Test local queue storage
Now, let’s test this using postman -
On clicking Send button, we get following in Postman —
Everything working smoothly, so as the last thing let’s go and check the storage emulator queue in the Cloud Explorer window. We can see that a new queue is created -
Let’s check the queue content -
And yes, it works successfully.
Wrapping up —
We saw in this demo how easy it is to use Azure Cloud SDK to create a function and test it locally using Postman and Storage Emulator.
In the next part, we will see how to process the stored queue and store the results in a BLOB storage locally.