My First Azure Function

Noah Ispas
4 min readMay 3, 2020

This is the first article in the Azure Functions series. We will start easy, showing how you create your first Azure Function with Visual Studio and C#. In the following blog posts, we will dive into more interesting and complex topics.

Prerequisites

There are very less Prerequisites for getting started with this simple Hello World Function. You could use any IDE or editor you want, but we will use Visual Studio to make things easy. So prerequisites are the following:

  • .Net Core Framework installed
  • Visual Studio installed
  • Azure Account

Getting Started

We will directly dive in with the following steps:

  • Open Visual Studio
  • Create a new Project
  • Select Azure Functions Template
  • Just follow the wizard, give the project and the function a name, and set the project location
  • Select Trigger. The default should be HTTP, which simply means that the function will be triggered by an HTTP request. we will cover more triggers in a later article in the series
  • Click on Create, which should result in a project

Inspect & Adjust

Now it’s time to have a look at what Visual Studio has generated for us. There is Visual Studio Solution which holds 1 Visual Studio Project.

The class HelloWorld.cs contains our first function. Around the function’s signature, we can see different interesting aspects.

  • Right above the function’s signature, we can declare the name of the Azure Function using the FunctionName attribute (line).
  • The first argument of the Run method contains an HttpRequest which is annotated as an attribute that specifies the trigger of the function. In this case, the function can be triggered by both an HTTP post and a get request.

Run & Test Locally

  • To Run, just click on the play button. Alternatively, if you like to work from the CLI as I do, use func start assuming you have installed the azure function core tools
  • The function will start in a separated CLI window independent of the way you started it. In the CLI window, the URL will be shown under which the function is available
  • Now simply call the function by using the browser, Postman, or my new favorite tool insomnia. Don’t forget to add the query param name to the URL otherwise, you will get a 400 :).

Deploy & Test on Azure

Deploying on Azure via Visual Studio is pretty easy, however, I recognized that the publishing wizard differs between a Visual Studio installation on macOS and Windows10. I will try to keep the following steps as generic as possible.

Deploy

  • In the context menu of the visual studio project select publish. You will have to log in to your azure account
  • You will either need to create a new App Service and Service Plan (which will be done with the help of the wizard) or choose an existing one from your azure subscription
  • When creating a new App Service you will have to specify some settings like the name for the App Service, selecting the subscription and resource group. You should already have a resource group if you were working with Azure before. But no worries you can also create new on the fly
  • The same goes for creating a new Service Plan. Here we will have to give a name, select a region, and the pricing which is determined by the number of resources you want to dedicate to your function
  • On macOS, the last step in the wizard is to specify a storage account in which the function should be stored. I remember that on windows10 this was specified in one of the first steps of the wizard. Anyway, here you can also choose an existing storage account or just create a custom one on the fly. Surprise.

Test

Deploying the function required the most amount of work in this post. Now we can actually test the Azure Function on Azure. Yay.

  • Note that the website URL which is shown after the wizard on windows10 or which should open automatically on macOS is not the actual URL of the function, it is only the base URL for the Function Space
  • In order to get the actual URL of the function go to Azure Portal and find your Azure Function resource. Click on it. You will see an overview where your Azure Function Space is defined. Here you could have more than one actual function. Select the only function which should be named helloWorld (or the name you specified in the FunctionName attribute, remember?)

Conclusion

Congratulation, you have successfully created, deployed, and tested your first Azure Function with C# and .Net. Debugging locally should work fine using Visual Studio, for remote debugging as well as some more advanced stuff around Azure Functions stay tuned.

The next post will be about working with a CosmosDB inside Azure Functions.

Cheers

--

--

Noah Ispas

Inspire and helping people to do what they love and do best. Connecting the dots and creating harmonious environments.