How To Create Azure Function

Azure Functions allows us to run small piece of code without worrying about application infrastructure. The cloud infrastructure provides all the up-to-date servers.

Features

Azure functions support following feature:

  • Choice of language: We can use any language in order to build the functions, like C#, .NET Core, Java, Node.js, Python etc.
  • Pay-per-use: this pricing model is use for the function as it very small piece of code execute separtely.
  • Bring your own dependency: It allows us to use NuGet and NPM packages
  • Simplified Integration: This is the biggest advantage of Azure function is support with Azure Services and software-as-a-service (SaaS) offerings.
  • Flexible development: Azure functions code can be setup in Github, Azure DevOps.
  • Stateful serverless architecture: Orchestrate serverless application with Durable Functionss.

What is Serverless Applications?

These are Platform as Service(PaaS) that take the “fully managed” aspects of the service to the max. Serverless service offerings allow for individual code functions to be deployed and managed without the need to deploy and manage the underlying VM including even the app that hosts the function too. Serverless architecture isn’t really to host application with servers, Of course they still needed CPU, RAM, Networking and other components in order to host the any serverless application. Serverless really is about taking PaaS to extreme level.

What can we do with Functions?

Functions is a great solution for processing bulk data, integrating systems, working with the internet-of-things (IoT), and building simple APIs and microservices.

My first Azure Function

We are going to create first function which will be a REST Api. I’ll take a parameter in the request and return the processed result as http response. This function has trigger of HttpTrigger.

Setup environment

We are going to setup development environment for local development. Functions will be running locally and deployed from local to Azure.

  • Create a folder in local workspace
  • Install Visual Studio Code locally to write the code and deploy.
  • Install Azure plugin in Visual Studio

Create new Function Project

Login to your Azure account in Azure plugin on Visual Studio Code. Follow below steps:

  1. Click on new function project in Azure Functions Extension:
  2. Select a language for function project
  3. Select template for the function
  4. Enter name of the function
  5. Select Authentication type. I have selected Anonymous:
  6. This process will create the project and create a file TestMessage.cs file. Automatically created file looks like below:

Running the application

Run the application by pressing F5 key. It will automatically restore all package and install Azure Function runtime locally. It’ll also create url to call locally. Check the Terminal window for the url created for Http API.

Copy the url and run on browser. add query parameter “name=value” in order to run it. As per the code it is expecting a parameter.

Publishing to Azure Portal

We are going to create new Resource Group on Azure portal to host this function and related resources. Because it is not only going to create the Function App, it also going to create payment plan and storage account for the Function in this resource group. Advantage of creating separate resource group allows us to delete all resources related to this with single click.

Create Function App

I am going to create Function App directly and create new resource group from there it self.

  • Search for “Function App” from create new resource page.
    Search for Function App
  • Create the “Function App” with provide below information:
    • Resource Group: Click on create new link for new resource group:
    • provide unique name to the function app. It will unique on Azure platform.
    • Select runtime, the technology we are going to use. In our case it is .NET core 3.1.
    • Zone: it can be closest zone to you for better performance.
      Create new Function
  • On next screen selecting hosing information. We are going to host on Wondows. It is going to create new storage account for this app. And we will select consumption plan. It will be charged as per call. We can also use webapp service plan or dedicated host. But that will be costly for our test case.
    Select Hosting
  • Click next to go to Monitoring section. Keep al field default.
  • Finally Create the app and wait for it to finish. Once finished, go to resource, it’ll look like below. It create various resources.
    Resource Group View
  • Click on Function App (Type:App Service) and it’ll show list of function within this app. Currently there is no function. Functions will be created automatically when we deploy our code from Visual Studio Code.

Deploy code from the VS Code.

As we are successfully complied and run the project, we are also have created the function app on Azure Portal. Now we have to deploy to Azure. Based on the function in local it’ll automatically create functions on the portal. Follow below steps to deploy code in function app:

  1. Click deploy from Functions section of Azure plugin. It will show the list of function apps from the azure on top bar. If it doesn’t appear then restart the VS Code.
    Select Function App
  2. Once deployment completed, it will show completion notification on bottom right corner of the VS Code.
  3. Go to Azure portal and select the Function App, it will show our function in the list. Select the function from left pan and get the URL of the function to test from browser.
  4. Url will look like “https://nityafunction.azurewebsites.net/api/TestMessage”. We will add parameter “name=sometext” to the url and will run on browser. As per the code it is expecting parameter in query string.

Conclusion

We are able to create our first function app and deploy on the Azure. If there is small piece of code, we could also created from portal with “In Portal” option. But in case of deployment from VS code, it deploys the DLL and function.js. We can create multiple function in same application with different triggers.

One thought on “How To Create Azure Function

Comments are closed.