How to use Azure Functions to process events with serverless code architecture?

With the help of Microsoft Azure services, we can now just focus on the main business focussed needs and not worry about the infrastructure and provisioning servers. In this short post, I’ll tell you how to use Azure functions to process events with serverless code architecture.

Azure Functions is a serverless compute service that enables you to run code on-demand without having to explicitly provision or manage infrastructure. Use Azure Functions to run a script or piece of code in response to a variety of events.
– Microsoft Azure

A function can be executed based on events created via HTTP, Timer, Webhook, Blob or Queue. Also, a function can be written using most of the popular languages like C#, F#, TypeScript, JavaScript, PHP, PowerShell, Python, Bash and Batch.

Use Azure Portal to add a Function application which is another type of App Service. The only difference is like an app service, we don’t need to provision or manage a function application. Creating a function is all down to a scenario like what time of trigger you would like to use with the choice of your language as listed above.

Triggers

Some examples of supported triggers are:

  • HttpTrigger: a function that will be run whenever it receives an HTTP request.
  • TimerTrigger: a function that will be run on a specified schedule.
  • QueueTrigger: a function that will be run whenever a message is added to a specified Azure Queue Storage.
  • BlobTrigger: a function that will be run whenever a blob is added to a specified container.
  • EventHubTrigger: a function that will be run whenever an event hub receives a new event.
  • ServiceBusQueueTrigger: a function that will be run whenever a message is added to a specified Service Bus queue.
  • Generic or GitHub Webhook: a function that will be run whenever it receives a webhook request.

Example

Let’s create a timer trigger function in C#. The function code can be written, run in the portal itself and the name of the file is run.csx. This function is same as any other C# method and looks like following:

Triggers are basically what will run your function. Once a type of trigger is selected for some scenario, we can pass that trigger as parameter(s) to the function. Each type of trigger will need different information. For example, to create a timer trigger, we just need to set up the Timestamp parameter name as myTimer and a schedule information in cron expression format.

So, as you can see in the code above, a parameter named myTimer of type TimerInfo and a log is available to Run method.  The body of this method will be executed every time as per the given cron expression format schedule. Use crontab.guru which is an amazing tool to quickly get cron expression format as per your needs. Note, Azure has one more character at the beginning to cron expression format for seconds.

Inputs & Outputs

Like any other standard function, Azure Function can have input(s) and output(s) where both are optional for a function. An example of input will be fetch some data from a blob storage and provide it to the Run function. An example of output will be to add an item to a queue.

function.json Configuration

Any configuration that we define for a function like parameter bindings, inputs, outputs are stored in a special file named as function.json. So, currently this file looks like following with one parameter binding information for myTimer:

Monitor

Like any service in Azure, Functions can also be monitored like success, error counts and details to diagnose properly.

Function App Settings

Azure Function is just another type of App Service, so as per the requirements, we may need different app settings like storing a connection-string, key or blob storage container name, etc. The options are quite similar to a standard app service settings like applications settings, dev console, deploying a function from GitHub, Visual Studio Team Services, etc. for Continuous Integration.

Next, it also has Kudu support to access advanced functionality of App Service like uploading zips, killing processes and more. Even CORS, API can be managed to allow HTTP triggered functions to be called from within a web browser or any HTTP client consumer.

This sums up my short post to explain how to use Azure Functions to process events with serverless code architecture. For a real world example using Azure Functions, please subscribe to my blog as I’ll be writing another post soon.

Siddharth Pandey

Siddharth Pandey is a Software Engineer with thorough hands-on commercial experience & exposure to building enterprise applications using Agile methodologies. Siddharth specializes in building, managing on-premise, cloud based real-time standard, single page web applications (SPAs). He has successfully delivered applications in health-care, finance, insurance, e-commerce sectors for major brands in the UK. Other than programming, he also has experience of managing teams, trainer, actively contributing to the IT community by sharing his knowledge using Stack Overflow, personal website & video tutorials.

You may also like...

Advertisment ad adsense adlogger