Azure Functions – Basic Concepts and Hello World

Azure Functions – Basic Concepts and Hello World

In this article, we will have look at what are Azure Functions, where / how they can be used. We will also try to create a sample Azure Function in Azure Portal.

For following some of the steps in this article, you will need an Azure account. If you do not have an Azure account,  you can create free account on Azure.

What are Azure Functions ?

Azure functions let you run small pieces of code in the cloud. You do not need to worry about the infrastructure part when you write Azure Functions. Azure functions is part of Azure’s serverless offerings.

Some of the interesting features of Azure Functions are:

  • You can choose the language of your choice to write Azure Functions. Currently you can use C#, Java, JavaScript, Python or any of the other supported languages.
  • You can also refer to other dependencies while you are creating Azure Functions. Azure Functions supports NPM and NuGet.
  • Azure Functions can easily integrate with other Azure Services and SaaS offerings
  • You can deploy Azure Functions on Windows or Linux Operating System
  • You can choose to enable application insights monitoring in Azure Functions.
  • Azure Functions runtime is open source and the code is available on GitHub.

Mostly functions are stateless. Meaning they do not convey state information from one run of a Function to another. There is also a maximum time for which the Function can run.

Why Azure Functions ?

Azure Functions are very useful if you want to process a lot of data or your application wants to integrate/orchestrate with a lot of other systems or your application has long running tasks like image processing, File maintenance etc.

The Pricing Plans

Azure functions come with two pricing plans:

  • Consumption Plan – You do not have to worry about the computational resources. Azure takes care of everything. You just need to pay for the time for which your code runs. Scaling part is automatically taken care by Azure.
  • Premium Plan – The plan is similar to the Consumption Plan. It has additional features like perpetual warm instances to avoid initial delays, VNet connectivity, Unlimited execution duration, more predictable pricing, etc. You may want to use this when you need very high computational resources or you have very complex long running jobs, etc.
  • App Service Plan – You can run Azure functions on App Service Plans. In Azure App Service Plan, you can decide which computational resources you need. If you already have App Services running, then you can run Azure Functions without incurring any additional costs.

In addition to this, you may also want to know that Azure Funcitonsl require a storage account associated with it. It is billed separately.

Also, you can also choose to associate Application Insights with Azure Functions which is also billed separately.

Building Blocks of Azure Functions

Azure Functions has two important things: triggers and bindings

Triggers

As the name suggests, a trigger is something that invokes the Azure Function. Every Azure Function should have one and only trigger. A trigger may also provide access to the payload of the message.

There are different types of triggers available for Azure Functions, some of them are:

  • HTTP Trigger
  • Timer Based
  • DB Trigger
  • Event Grid Trigger
  • Service Bus Trigger
  • Event Hub Trigger

Bindings

A Binding is a declarative way to connect another resource with Azure Function. Binding has direction – input or output. Data from bindings is provided as parameters to the Functions.

Deploying Function App

You can deploy the Azure Function App as a part of CI/CD pipeline. You can also choose to use Web Deploy or Cloud Sync or Zip Deploy or any other supported option.

Sample Azure Function

Let’s try to create a sample Azure Function from Azure Portal.

Go to “Create a resource” then search for Function App and click on Create. It would show you a panel where you can provide additional details of the Function App.

  • Azure Subscription – Active and Valid Azure Subscription
  • Resource Group – Logical container for the Function App
  • Function App Name – Globally unique name
  • Publish Option – Either Code or Docker Container
  • Runtime Stack – .Net core, Java, Python, etc.
  • Region – Geographical region where the job should be deployed

There are other tabs also, but keep those as default and click on “Review + Create” button. This will review the creation and will bring you to Create screen. Press “Create” button which will start deployment of Function app.

Once the Function App deployment is done, go to the resource and Expand Functions node. Now, click on (+) button to add new Function. Select Http Triggered Function from next panel. It would show you a panel on right side, where you can select the authentication method and name for the Function. Let them be default and click on Create button.

The default function will look like below. Now click on “Get Function URL” and copy the URL of function.

Paste the copied URL in browser and Hit Enter key. It would show you below message. You can also add query string parameter “&name=yourname” and it will show you greetings message.

You just created a working Function App on Azure. You can notice how Newtonsoft.Json is referred in the function app. You can also try modifying the sample template. Try logging something and see if it goes to Application Insights. I hope you liked the article and do comment and let me know your thoughts.

Leave a Reply