How To Create And Publish Azure Function From Visual Studio

In the previous article, we discussed Azure functions and its features, and we  also created an Azure function using Azure Portal. This is the second article in the Azure Functions series. In this article, we are going to discuss how we can simply create an Azure function using Visual Studio 19, and then we will deploy the same function to the Azure function app from Visual Studio.
 
Azure Functions in C# for Beginners series:
It is recommended to create an Azure function from Visual Studio or VS code so that we can write code in a proper way. We can create a C# based Azure function from Visual Studio. In this article, let's create a simple HTTP based Azure Function.
 

Create Azure Function Using Visual Studio

 
Prerequisites
So open Visual Studio and Go to File -> New -> Project. Search "Azure Functions" in the search box and select the Azure function template and click on Next.
 
How To Create And Publish Azure Function From Visual Studio
 
Give a name to the function project and click on Create. 
 
How To Create And Publish Azure Function From Visual Studio
 
Select the HTTP trigger template and set the Authorization level as Anonymous and click on Create.
 
How To Create And Publish Azure Function From Visual Studio
  • To restrict the use of your function we can set the Authorization level. There are three types of levels available: 1] When we set levels as Function, then we have to provide a function key to call our function. 2] When we set as Admin, then you need to provide the master key. Both the function key and admin keys are found in the 'keys' management panel on the portal when your function is selected. 3] When we don't want any authorization then we can simply set level as Anonymous. 
That's it. We have created our first Azure function. Open the Function1.cs file to see generated function.
 
How To Create And Publish Azure Function From Visual Studio
 
Now to run the function just run the project. It will then start azure function cli to run the function.
 
How To Create And Publish Azure Function From Visual Studio
 
The function is running on 'http://localhost:7071/api/Function1' after looking at the default function code written which accepts name from the query string or from the request body and appends the name to the Hello string and returns the output.
 
How To Create And Publish Azure Function From Visual Studio
 
Deploy Azure Function using Visual Studio
 
To deploy our function to Azure we need Azure Function App. Right Click on our solution and click on the Publish button.
 
How To Create And Publish Azure Function From Visual Studio
 
As we are publishing into Azure then Select Azure and click on Next.
 
How To Create And Publish Azure Function From Visual Studio
 
Select target as "Azure Function App(Windows)" and click on Next. We can either select an already created function app or we can create a new Function app.
 
To create a new function app click on "Create a new Azure Function" button.
 
How To Create And Publish Azure Function From Visual Studio
 
How To Create And Publish Azure Function From Visual Studio
  • Name - Give a name for your function app
  • Subscription - Select your Azure subscription
  • Resource group - If you have already created RG then select that or click on 'new' button to create new RG
  • Plan Type - Select as Consumption
  • Location - Which is near to you
  • Azure Storage - If you have already created Storage Account(SA) then select that or click on the 'new' button to create a new SA.
And then finally click on the Create button. In this way, we can create a new Function App, 
 
For this article, I am using an existing function which we have created in the previous article , and click on 'Finish' button.
 
How To Create And Publish Azure Function From Visual Studio
 
How To Create And Publish Azure Function From Visual Studio
 
Once our app is successfully published on Azure,  go to the Azure portal and search for our function app. Select Functions from the left sidebar to see our deployed function.
 
How To Create And Publish Azure Function From Visual Studio
 
To run the deployed function, click on the Function (in our case 'Function1') and then click on 'Get Function Url' button & finally copy the function URL.

How To Create And Publish Azure Function From Visual Studio
 
Now to test the above function you can either use Postman or you can simply open the browser and paste the above URL. Pass name from query string to see the correct output.
 
How To Create And Publish Azure Function From Visual Studio
 
That's it. We have created the Azure function from Visual Studio.
 

Conclusion

 
In this article, we have created a new Azure function from Visual Studio. Also, I demonstrated how to test and deploy the function from VS. I really hope that you enjoyed this article, share it with friends, and please do not hesitate to send me your thoughts or comments. Stay tuned for more Azure Functions articles. 
 
You can follow me on twitter @sumitkharche01
 
Happy Coding!!