Getting Started with Azure Functions

If you have a chunk of code you’d like to have run so you can call it whenever you want, Azure Functions might just be the right solution. Azure Functions let you use a serverless environment to execute such functions. All you need is an Azure account and Visual Studio and you can have a function up and running rather quickly.

If you don’t already have a Microsoft Azure subscription, you can sign up for a free trial Azure account. The current offer gives you $200 in credit that can be used within 30 days. When you set up a free trial, it will allow you to cap your personal spending by default at $0, thus avoiding the risk of spending any real money until you are ready.

If you don’t have Visual Studio, you can get the Community edition at no cost as well. This is available online. You’ll need Visual Studio 2017. You should make sure you’ve updated to the latest version.

To illustrate how to use Azure Functions, this article will walk you through creating a simple project in Visual Studio, followed by setting up the function on Azure. The function that will be created will be a simple service that will return a random Bible verse when called. This function could just as easily return a random number, a value, customer data, or any service you want to make available online.

Creating the Azure Functions Project

To begin, create a new project in Visual Studio. Select New Project → Visual C# → Azure Functions, as show in Figure 1.

Creating an Azure Functions Project in Visual Studio 2017
Figure 1: Creating an Azure Functions Project in Visual Studio 2017

When you create an Azure Function project, you will be asked to select a template for your function, as shown in Figure 2.

Setting the template for your Azure Function
Figure 2: Setting the template for your Azure Function

Select Http trigger for the application we are building here. This means that the function being created will be called with an HTTP request. For the storage account, you don’t really need one. If you have a storage account already set up in Azure, you can use it here; otherwise, you can leave this as the emulator for now. The value you will need to set is the access rights. For the example in this article, the access rights will be set to Anonymous. This will allow a client to access the function without the need to provide a key.

With the settings updated, click OK to continue. This should create the project; however, if this is the first time you have used Azure Functions, you might be prompted to install the Azure Function CLI tools. This will be done via a pop-up dialog, as shown in Figure 3.

Prompt to install the Azure Functions CLI tools
Figure 3: Prompt to install the Azure Functions CLI tools

Once you’ve clicked OK and have the CLI tools updated, you will have the default Azure Functions project. The default project has code for a ‘hello world’-style application. You should be able to build the application and then run it.

When you run the default application, it will execute in the local host emulator (see Figure 4).

Running the default Azure Function code
Figure 4: Running the default Azure Function code

With the function code running, open a browser window and enter the URL shown next to “Function1:” in Figure 4. In this case, that is http://localhost:7071/api/Function1. The result should be what you see in Figure 5.

Calling the default Azure Function app
Figure 5: Calling the default Azure Function app

The default Azure Function App expects a value to be passed into it. In this case, it expects a name. Modify the URL you used to call the function by adding a parameter to the end as such:


This time, the result will look more like Figure 6.

Running the default Azure Function app with a parameter
Figure 6: Running the default Azure Function app with a parameter

If you look at the console window that was created when you ran the Visual Studio app in Figure 4, you’ll see that a lot of logging information was displayed as you called the function from your browser. At this time, you can close that window.

Creating the Bible Quote Function

You can review the code that was provided in the default application to see how it grabs the name and then returns the HTTP Response. Listing 1 provides updated code that replaces what the default application does with a simple selection of a random quote.

using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;

namespace BibleQuote
   public static class BibleQuote
      public static async Task<HttpResponseMessage>
         Run([HttpTrigger(AuthorizationLevel.Anonymous, "get",
         "post", Route = null)]HttpRequestMessage req,
         TraceWriter log)
         string myQuote;
         Random rnd = new Random();

         switch (rnd.Next(1,5))
            case 1:
               myQuote = "The lips of the righteous feed many: but
                  fools die for want of wisdom.";
            case 2:
               myQuote = "Discretion shall preserve thee,
                  understanding shall keep thee:";
            case 3:
               myQuote = "Discretion shall preserve thee,
                  understanding shall keep thee:";
            case 4:
               myQuote = "Envy thou not the oppressor, and choose
                  none of his ways.";
               myQuote = "something else";
         // Creates a number between 1 and 13
         int month = rnd.Next(1, 13);

         dynamic data = await req.Content.ReadAsAsync<object>();
         return req.CreateResponse(HttpStatusCode.OK, myQuote);

Listing 1: BibleQuote Azure Function

The code in Listing 1 will use the same logging the default application used. It then simply generates a random number that is used to select one of four quotes to return to the caller. If you enter this code into Visual Studio and compile, you’ll get a screen similar to what you saw in Figure 4. When you then enter the URL shown in Figure 7 to call the application, you should see one of the random quotes.

The BibleQuote Azure Function
Figure 7: The BibleQuote Azure Function

Publishing the Function to Azure

With the Azure Function created, the next step is to publish it to the Web; to Azure. To publish to Azure, right-click the project name in the Solution Explorer and select Publish, as shown in Figure 8.

Selecting to Publish the Azure Function in Visual Studio
Figure 8: Selecting to Publish the Azure Function in Visual Studio

When you select Publish, you’ll be presented with a dialog asking where you want to publish, as shown in Figure 9. In this case, you are publishing to Azure, so select Azure Function App. If you’ve not already set up an Azure Function on Azure, you can select an existing function; otherwise, select Create New and click the Publish button.

Publishing the Azure Function
Figure 9: Publishing the Azure Function

To publish the Azure Function on Azure, you’ll need to provide a few details, as shown in Figure 10.

The App Service settings
Figure 10: The App Service settings

For your function to work from Azure, you’ll need to give it a unique App Name. In my example, I’ve used BibleQuote. As mentioned earlier, you’ll need an Azure subscription to use the Azure Functions. The Resource Group will allow you to organize where you want to store your function. If you’ve set up groups in Azure before, you can select one of them; otherwise, you’ll need to click New and set up a new group. Finally, you’ll need to select the type of service plan you want to use. When you set up a plan, you’ll need to state what location you want to run the function from as well as the size plan you’ll use. In general, this will be Consumption for Azure Functions.

Press Create and the service should be created, as shown in Figure 11.

The published BibleQuote Azure Function
Figure 11: The published BibleQuote Azure Function

With the function now published to Azure, you should be able to call it from the Web. To do this, you can use the URL shown in Figure 11 on the Publish page. This URL will be similar to the localhost URL you used earlier, except it will now point to For my function, when I run the following URL, I get the result shown in Figure 12.

The BibleQuote
Figure 12: The BibleQuote

In Conclusion

Creating an Azure Function is relatively easy when using Visual Studio 2017. In this article, you actually saw two different functions created to illustrate how easy they are. The first, hello world application, showed how to use the default code. The second app illustrated how easy it is to make your own unique functions. With what you’ve learned, you should have everything you need to start creating now!

More by Author

Must Read