Create Azure Functions using Visual Studio and deploy it to Azure

Create Azure Functions using Visual Studio and deploy it to Azure

Azure Functions let us execute small pieces of code or function in a serverless environment as a cloud function. You just need to focus on code and functionalities rather focusing on cloud infrastructures or any other aspects of the cloud solution. You can create Azure Function directly from the Azure Portal. Azure Functions Tools for Visual Studio also lets you develop, test, and deploy functions to Azure. Having it developed in Visual Studio gives lots of other flexibility to the developers as well, where we can leverage all the IDE features including source control integration, testing, and other aspects.

Azure functions getting started with visual studio

Let’s learn how we can create a simple Azure Function and deploy it to Azure using Visual Studio.

Start a new instance of Visual Studio and select Azure Functions available under Visual C# Project Template from the New Project Dialog Window.

New Azure Function

 

While creating the project, make sure you select “Create new Git repository” option, so that newly created projects added to local git repository as well. If you are wondering how it works – Read one of my previous posts – Create New Git Repository While Creating New Project in Visual Studio

 

Next dialog window for Azure Functions will ask several options such as Azure Function Versions, Types etc. For now, choose Http Trigger, and Set the Access Rights “Anonymous”. Make sure the version of Azure Function is set to v2(.NET core)

Click on Ok.

New Project Azure Functions Options
Visual Studio will take a while to setup and create your Azure Function. The current solution has a function named “Functon1” which takes arguments name and display the same. Just like a simple RestAPI Call, where you can just invoke the method.

Code for Azure Function

As of now, let’s not change anything to the code, and Run the functioning app from Visual Studio by just pressing F5.

Function App Running

When the Function is running, you can access the Http EndPoint and pass on a parameter to test it out.

Azure Function Running Locally

Which means our function is working fine. Feel free to place a breakpoint in the code and check how the code flow and internal execution is.

When everything is working fine, push your code to your GitHub account.

You may ignore this step; however, we will refer this code base in the next post.

In the next step, we will publish the Azure Function

In Solution Explorer, right-click the project and select Publish. Choose Publish option again and then click on Start.

Select “Azure Function App” and then select “Create New”.

Publish Azure Function

Then provide required details for App Services and finally click on Publish.

Visual Studio will take a while to create all the Azure resources and publish your code. Once the Publish is completed, you can open the deployed url from Visual Studio.

My Function App

Now, you should be able to access your function from the deployed version of Azure Function.

Azure Function Runing from Cloud

Open the Azure Portal and navigate to the Azure Functions Resource and explore your function from there.

Azure Function in Azure Portal

If you already created the Azure function from the Azure Portal, you must be familiar with this. To monitor logs, success count and errors, navigate to Monitor option available under Function1

Monitoring Azure Function

You can also integrate your Azure Function with Azure Application Insights for better telemetry and monitoring

There are several types of Azure Functions with different types of triggers, you can now explore them. This post is all about a quick walkthrough on how we can create Azure functions from Visual Studio. We will cover some more detailed topics in the subsequent posts.

Geek’s Choice
Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

One Comment to “Create Azure Functions using Visual Studio and deploy it to Azure”

Comments are closed.