Azure Function or Serverless Computing Tutorial.

What is serverless computing?

Serverless computing helps developers to focus on developing code rather than managing infrastructure. The infrastructure-related activities like provisioning, scaling and managing the servers will be taken care of automatically by the cloud service provider. The Azure cloud provides a serverless computing service called Functions App.

In this tutorial, we will learn about how to create and run the Functions App from the Azure portal. The Functions can be created and executed from your local machine (Linux, Windows, and Mac) with the help of Azure Functions Core Tools.

Create a Function App from the portal

To create a Function App, login to the Azure portal and from all services search for Functions.

 

To start with Function App click on create and on the next page add project details.

Select your Subscription name and Resource Group name from the drop-down list. Refer below the screenshot for the instance details.

 

Here, my App name is “techiesapp” and publish as codebase or you can choose Docker container. In this tutorial, we will use code. Function App support .NET, Node.js, Python, Java, and Powershell Core language. I have selected .NET for this tutorial.

Click the Hosting button to continue. Next page, add the below details

  • Storage account: Use existing or create a new one (functionappstorage)
  • Operating system: I have selected Windows
  • Plan type:  There are 3 options available, I choose App Service Plan and you can select a plan based on your requirements.
  • SKU and size: I have opted “Shared D1”.  You can determine the SKU and size (Memory, CPU and scaling capacity ) based on your code.

Click the Monitoring button to add Azure Application Insight (This service is for monitoring).

Click the “Review + Create” button to create the Function App. The deployment takes a few minutes to complete.  Once done, go to the resource and you can see a dashboard like below.

This is an overview dashboard where you can see the status is running, and subscription and Resource Group details, etc.

To create a new Function click Functions present on the left side menu and click “New Function” and you can see a dashboard like below.

Here you can see different types of templates available. These template helps how to start (or trigger) a function. For the demonstration, I choose the HTTP trigger.

Click on the template and a pop-up window will come on the right side, here you can add a name for your new Function and authorization level.

Authorization level is in 3 type

  1. Function: This is the default authorization level and it requires a code parameter to the URL of our function.
  2. Anonymous: Anonymous means no authorization is required. Any valid HTTP request passes
  3. Admin: Admin authorization requires a host key for authorization.

To understand more about this I recommend you to read Microsoft documentation

I have chosen Function as my authorization level and created my Function App with the below details.

Once the Function App is created we can see a default .NET code is present in the editor.

I used the same code in this demonstration. To execute the code, click on the arrow mark ( < ) present on the right side and select “Test” from the panel.  The panel contains few tools which will help us to execute the function.

 

From this panel, we can select which HTTP method we required (GET or POST) and there is a section to add a query parameter and header. We can also specify a request body, in this case, it is JSON and is configured by default where we have property name is “name” and the value “azure”.

To execute the Function click on the Run button and you can see the output in the right panel and logs in below black panel. If the code executed successfully it shows a “Status: 200 ok” Message in the right panel.

Here, we can change the request body value and we can add a new parameter and execute the code again. You can try it yourself.

We have now successfully created an HTTP triggered function and executed it from the portal.

Next, to get the URL of the function click on “Get function URL” on top next to the “Run” button.

We can directly access this URL on the browser or from the Powershell.  I triggered it from the PowerShell and could see the below error.

 

This is because we haven’t passed the “name” parameter. I have modified the URL as given below (passed name parameter) and got HTTP 200 ok, success message.

"https://techiesapp.azurewebsites.net/api/MyHttpFunctioncode=Pxi6SaI2fG/jXacxl3RBsio8l4qdRipSg/tFbW5wRQxYsfaEWa36uQ==&name=Techies"

 

 

To understand more about how Azure Function App is work we will look into the “view files” which are present in the right-side panel. Here we can see two files named function.json and run.csx.

 

The function.json file is a declarative definition of the bindings for this function, in this case, we have two binding, the input, and the output. The first binding type is “HTTPtrigger” and its direction is “in” and the methods are Get and Post. The second binding named “$return” and its type is HTTP and direction is “out” this is the output binding. Here we will specify the return value. The second file is run.csx which contains the code.

 

 

The Function App is serverless, even though it is running on a virtual machine. But the virtual machine is provisioned and managed by cloud service providers. To know where our function is running, click on the “console” present in the below black panel and can see the below directory structure.

 

We have learned how to run an HTTP trigger-based Function and how to check logs and change the parameters.

Timer-based Functions.

We have learned about HTTP trigger-based Function, Next, we can understand Timer-based Functions. Time trigger Functions run based on the scheduled time. The time is scheduled as a cron using the TimeTrigger attribute. To understand more about this, we can create a Time-based Function.

  1. Log in to the Azure account
  2. Create Functions App
  3. Once the Function App created, create a new Function by selecting the Timer-based Function template.

It will popup-up a window on the right side.  Give a new name and schedule time for the function. I have scheduled the Function to run every 10 seconds.

Click the Create button to create it. On the dashboard, you can see a piece of code already present, we will use the same for the demonstration. Look at the log panel present in the bottom side and can see the Function is started running based on the cron timing. We don’t need to click the “Run” button to execute it.

To change the execution time, edit the function.json file present in the right-side panel. Save the file and the code start run based on the new cron timing.

 

Leave a Reply

Your email address will not be published.