Jaa


How to Azure Function App with Hybrid Connection

On my path to create this article, I wrote numerous other along the way.  To get a overview of the project I worked on, read the following articles as well.

Basically, my project was to create an ARM template that created the features required to test the Hybrid Connection manager.

In this article, I will discuss the connection of an Azure Function to an Azure VM in a VNET (also in Azure) using the App Service Hybrid Connection manager, here are the steps:

  • Create the Azure Function (App Service / Dedicated, I.e. not Consumption)
  • Configure App Service Hybrid Connection on the Azure Function
  • Configure the Hybrid Connection Manager (HCM) on the Azure Virtual Machine

Create the Azure Function (App Service / Dedicated, I.e. not Consumption)

Create a new App Service Plan Azure Function from within the Azure portal.  Select the Function similar to that seen in Figure 1.

image

Figure 1, how to create an Azure Function App, Hybrid Connection

Then, as seen in Figure 2, give the Azure Function a name, and very important, as of writing this article, it is only possible to configure a Hybrid Connection when the function is running on an App Service Plan Hosting Plan.  App Service Plan will remain within the same tenant, while it is possible that Consumption based Azure Functions run in multiple tenants all at the same timeimage

Figure 2, how to create an Azure Function App, Hybrid Connection

Then save the Azure Function.

Create a simple HTTP Trigger, Figure 3.

image

Figure 3, how to create an Azure Function, Hybrid Connection

Then you can test the Azure Function using, for example CURL, as seen in Figure 4.

image

Figure 4, how to test an Azure Function, Hybrid Connection

Configure App Service Hybrid Connection on the Azure Function

Click on the Function App –> the Platform features tab –> the Configure your hybrid connection endpoints, As seen in Figure 5. image

Figure 5, how to configure an Azure Function, Hybrid Connection, configure endpoints

As seen in Figure 6, give the connection a name, the enpoint should be the NETBIOS name of the endpoint which this Azure Function App will connect to and on which port it will make the connection. image

Figure 6, how to configure an Azure Function, Hybrid Connection, configure endpoints

If you already have connection enpoints, then you can reuse an existing Service Bus.  Note that there are different SKUs of Service Bus so if you reuse the same one, make sure it is at a SKU which supports the throughput.  See here.

Once the Hybrid Connection endpoint is configured, you will see something similar to Figure 7, with a status of Not Connected.

 

image

Figure 7, how to configure an Azure Function, Hybrid Connection, configure endpoints

Configure the Hybrid Connection Manager (HCM) on the Azure Virtual Machine

You can download the Hybrid Connection Mananger from the same page where you configured the Hybrid Connection endpoint.  Selecting “Configure your hybrid connection endpoints” as seen previously in Figure 5 is the place to down load the installation package.

Once you have it, install is on the machine to which you want the Azure Function App to connect to.  After the installation, you are pormpted to enter your Azure credentials, use the credentials which has access to the Azure Function App you created.  After authentication, select the correct subscription and then you will see the Hybrid Connection you just created.  See Figure 8. image

Figure 8, how to configure an Azure VM or HOST, Hybrid Connection, configure endpoints

Once configured, navigate back to the Azure Function App and look at the status of the Hybrid Connection, figure 9.  If all is ok, then the status will show as Connected and you will be able to connect to the VM using the configured port. image

Figure 9, how to configure an Azure VM or HOST, Hybrid Connection, configure endpoints

An example of some code to use for the Azure Function, see here also for a discussion on optimal use of connections and coding patterns “Managing Connections”.

 
using System.Net;
using System.Net.Http;

private static HttpClient httpClient = new HttpClient();

public static async Task<HttpResponseMessage> 
            Run(HttpRequestMessage req, TraceWriter log)
{
    try {
        var response = await httpClient.GetAsync("https://HCM200:7071");
        return req.CreateResponse(HttpStatusCode.OK, "Response: " + response);
    }
    catch (Exception ex) {
        return req.CreateResponse(HttpStatusCode.BadRequest, "Error: " + ex.Message); 
    }
}