Create Simple Azure Function

Overview

Azure functions allow us to run the code in a server-less manner. That means we do not have to create the VM to host the running code nor do we have to create a web application and publish code to the web application to run continuously.

In this article, we will create a simple azure function that responds to incoming requests.

To follow this article, we should take care of the below prerequisites

  1. Microsoft Azure subscription
    If you do not have one, you may go for the Azure Free Tier program: https://azure.microsoft.com/en-us/free/

    This will give you an access to Azure for 12 months for free!
  1. Visual Studio 2017 (version 15.3)

    Include the Azure development workload.

    Azure

Post the installation or upgrade of Visual Studio 2017 to version 15.3, manually update the Visual Studio 2017 tools for Azure Functions.

  1. Open Visual Studio 2017
  2. Select Tools menu
  3. Under Extensions and Updates > Updates > Visual Studio Marketplace > Azure Functions and Web Jobs Tools
  4. Click Update

    Azure

The VSIX installer will run to install the updates.

Azure

Preferably, close the Visual Studio during the update.

Azure Functions Project Creation in Visual Studio 2017 

After we update “Azure Functions and Web Jobs Tools”, we should see Azure Functions project template in Visual Studio. This project can be published to function app in Azure from Visual Studio.

A function app hosts execution of our custom developed azure functions.

  1. Open Visual Studio
  2. Click New Project
  3. Select Visual C# > Cloud > Azure Functions

    Note

    Function app name must be a valid namespace. Do not use special characters, hyphens, underscores, or non-alphanumeric characters.

    Azure
  1. Right-click the project name and select Add > New Item. 
  2. Select Azure Function and click Add.

    Azure
  1. Select HttpTrigger.
  2. Select Anonymous for Access Rights.
  3. Click OK.

    Azure

    The function created can be accessed by HTTP request from any client.

    A class file will be added to your project that has the basic implementation of your Azure function.

    Azure

    Let’s take a close look at the generated file.

    • The FunctionName attribute represents the name of the function.
    • The HttpTrigger attribute represents the message that triggers the function.

Test the Azure Function locally

Now, we have the basic Azure function ready. Let us test this on local first.

  1. Click Run or F5 from Visual Studio to execute the Azure function.
  2. It will prompt you to download and install the Azure Functions CLI tools.

    Azure

  3. Click "Yes" to continue.
  4. This will download and install Azure Functions CLI tools.

    Azure 
  1. Once installed, now our Azure function is ready to listen.
  2. Copy the URL of function from the Azure Functions runtime output, highlighted in Red rectangle below (i.e. http://localhost:7071/api/Function1).

    Azure

  3. Paste the URL for the HTTP request to any browser.
  4. Append the query string ?name=<yourname>
  5. Execute the request.

    Azure

If you can see the same result, our Azure function is working fine on local and we are ready to publish it to Azure.

Publish Function to Azure

  1. Right-click project
  2. Select Publish.
  3. Choose "Create New" if you do not have any pre-existing Azure Function App.
  4. Choose “Select Existing” option if you have one already.

    Azure
  1. Click Publish.

    Azure
  1. The Azure function will be deployed to Azure Function App.

    Azure
Test function in Azure

You can browse to URL that calls your HTTP triggered function as:

  1. http://<functionappname>.azurewebsites.net/api/<functionname>?name=<yourname>  

 

Azure

Up Next
    Ebook Download
    View all
    Learn
    View all