Understanding Azure Functions And Creating Your First Function In Azure

Introduction

 
In this article, we will understand what Azure functions and we will also be creating Azure function using C# and invoking it through an http trigger.
 
By the end of this article, we will have detailed understanding on creating and using Azure functions.
 
Prerequisites
  • Microsoft free account and Azure Credits.
  • Basic knowledge of using Azure Services
In this article, we will cover the following topics:
  • Understand what Azure functions
  • Create your first Azure function using C#.
  • Invoking your Azure function through http Trigger.

Implementation


Step 1 - What are Azure functions?
  • This Azure service allows you to run a small piece of code as a function.
  •  Here you just develop and upload your code to Azure function.
  •  You would only get billed for the amount of time code is been run.
  •  You can use a variety of programming laungauges for creating Azure functions.
  •  C#, Java, Javascript, Python and Powershell.
  •  You can use libraries by using nudget and npm packages.
Azure functions can be invoked using timer trigger, http trigger and many other methods. 
 
Step 2 - Create your first Azure function
 
Use this link to create a free Microsoft account.
 
The account creation will require you to provide your credit/debit card details for validation. The first month of the Azure Subscription is free and you will not be automatically charged unless you upgrade your account manually.
 
Once the account is created successfully, we can log into the Azure Portal using this link.
 
The home screen will display all recently used Azure resources.
 
Under Azure functions click and create new function.
 
 
Select your existing resource group and if this is your first Azure resource a new resource group would be created.
 
The next step is to select the development stack. Here I will be using C# hence I will choose .NET Core as runtime stack. 
 
 
Azure function needs aa Azure storage account. If you don't have one, a new one will be created.
 
There are 2 different plans avaiable for Azure function billing.
 
Consumption based plan
 
In this we would only be billed for the amount of time our code is run. In our example I will select a consumption based plan.
 
Premium 
 
If we have an existing app service plan the same can also be used to bill for Azure functions.
 
Under monitoring we will enable application insights and click on review and create.
 
It will take a few minutes to create and deploy our Azure function.
 
Azure function will  create the following resources in Azure.
  • Azure storage account
  • Azure application insights
  • Azure function app
  • Azure app service plan. 
Azure function app acts as an encapsulation for different Azure functions.
 
We can create our new Azure inside our deployed Azure function apps.
 
Click on create new functions.
 
There are number of predefined templates to choose from and this provides a good starting point to create your first Azure function.
 
I will choose httptrigger for my function. 
 
 
Once the function is created navigate through your fucntion and you will see the option for code+test.
 
I have written simple code to log my message and return an string object when function is invoked.
 
Logging is the best way to test Azure function. 
 
 
Step 3 - Invoking your Azure function through http trigger
 
Click on test and run and select your http action as GET 
 
 
We can see we got string response back and also the the message is getting logged correctly.
 
Now we will see how to invoke this function outside of the Azure portal.
 
Under function overview at the top click on get function url.
 
Copy paste function url in browser and you will get the response in the browser.
 
Thus we have successfully invoked our Azure function.
 

Summary

 
In this article, we have learned how to create, configure, and deploy our first Azure function.
 
Thanks a lot for reading. I hope you liked this article. Please share your valuable suggestions and feedback. Write in the comment box in case you have any questions. Have a good day!