Hello guys, its been a while since I wrote any technical posts. I was very busy with my 1.5 year old son, in fact I was barely managing to do my daily office works, as he often come to my workplace (which is a room in my house nowadays), and pounce up on my laptop. So somehow I managed to get some time here and there in between, and wrote this post. Hope you enjoy!
Azure functions uses an event driven programming model. Each function runs in response to something happening, like a trigger or a binding.
The different trigger types supported in azure functions are:-
- Timer — we can configure the azure function to run on a specific time, say midnight 12 AP
- Message — listens for a message in a particular queue where the azure function can access the contents of that particular message
- HTTP request — Azure functions can be triggered in response to an http request
- And there are many more triggers like when a new row is inserted into a cosmos db or when a new blow is created in azure storage.
Azure functions has anther concept called “bindings” which helps it to make it easier to integrate with other azure services. For example, if you want to look up a document in cosmos db by it’s id, you don’t need to go into the trouble of writing all the connections and querying code yourself, instead, you can set up an input binding, that the azure function runtime can automatically look up the document by its id, and pass it as in input parameter to your function.
So the azure functions helps us to eliminate the boiler plate code, and we can simply focus on the core business logic alone, which accelerate the development time exponentially. Doesn’t it sound cool?
Different hosting models of Azure functions are :
- Consumption based plan(Server less) :- The main features of consumption based plans are,
a) per second billing model — you will be charges only for those time when your function is running
b) 1,000,000 executions are free !
2. App service plan :- Pay a monthly fee to reserve one or more service for your function app. This is particularly useful when you have a predictable cost for your application.
3. Premium plan :- This combines the best features of the both plans described above. The main features of this plans are:
a) pre-warmed instances — the services are always ready to run, which eliminate the cold start problem which the consumption plan can suffer from where the first user has to wait after a period of idle time, for the new server to start.
b) VNet integration
c) Longer run duration
4. Docker container
Next, we can look at how can we create Azure function app in azure portal.
First of all, log into the azure portal. Then click on the function app.
Then click on “Create” option
Then proceed with the usual registration process that we do for any other azure services. Nothing specific here. Just make sure that, you are selecting the “Consumption” based plan on the hosting blade.
and enable the “Application insights” on the monitoring section.
Once you made all the entries, click on the “Review + Create” option, and your first azure function app is ready. Let’s go directly to the function app.
Here we are on the landing page of Azure function app. Click on the “Functions” options.
Click on the “Create” option.
Then select the “HTTP Trigger”.
On the landing page of the newly created azure function, I clicked on the “Code+ Test” option.
As you can see, we have a static http async method which takes a http request as input, and returns a task<IActionResult>. i.e., if the incoming http request contains a query string parameter called “name”, the azure function will return a 200 OK result along with a text message. Let me run the function.
For running the newly created function, just click on the “Test/Run” option
Here the azure portal will automatically create the necessary testing data for us, as shown in the below screenshot. Here we can see we have provided the “name” query string parameter with a value “Azure”
hit the “Run” button, and you will see the output as follows:
Here we can see the input string we given as the query string parameter has been fetched by azure function, and returned back to us as the predefined text in the azure function, with a 200 OK message. So it is as simple as that.
Just give it a try folks.
Thanks for reading!