How To Create And Publish Azure Function From Visual Studio
Tags: azure, serverless, csharp, beginners
Published: 2021-01-17 15:14:59

In the [previous](https://dev.to/sumitkharche/how-to-easily-create-azure-functions-using-azure-portal-5ghd) 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.

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
* Visual Studio 19 with Azure Development workload
* [Azure](https://portal.azure.com/) account (If you don't have an Azure subscription, [create a free trial account](https://azure.microsoft.com/free)) 

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.

![create-new-func](https://dev-to-uploads.s3.amazonaws.com/i/tkrf5oxkoqi5trfsozmf.PNG)

Give a name to the function project and click on Create. 

![name](https://dev-to-uploads.s3.amazonaws.com/i/ortfosm9xpqx7ll3z2jc.PNG)

Select the HTTP trigger template and set the Authorization level as Anonymous and click on Create.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/aqgmm2okug6agy4jpff9.PNG)

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ase6rh5iced67o1xfgtp.PNG)

Now to run the function just run the project. It will then start azure function cli to run the function.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bcgq4lb53bemyjjjsksg.PNG)

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cfj90qgzwpznf9h8nszr.PNG)

# 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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2xo95tguewwozfq719q8.png)

As we are publishing into Azure then Select Azure and click on Next.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/likmw29nv75m3es8kwjo.PNG)

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s2i2pe8b68krfzsw8z6i.PNG)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9axtte6i23dy12jq1f6r.PNG)

* 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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dqnmsb9l7ceoavezqflm.PNG)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5mq90rpjuo0f6jgv8cm9.PNG)

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/901e12euwc6zdyhy0n3j.PNG)

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/y2f608s3u02z5osw72qn.PNG)

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x58d5k1gaqsmmu74zapv.PNG)

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](https://twitter.com/sumitkharche01)

Happy Coding!!