How to know when a new instance of Azure Web Apps is available

These days I’ve been researching some mechanism to find out when a new instance has been associated to the load balancer in Azure Web Apps, ready to serve your content. I’ve used Application Initialization Module, a module of Internet Information Services that allows you to launch a series of requests, in order to warm-up your web site and get a faster response, without having to wait for a first request from a user in those parts that have a slow start. In my case, it helps me to know when a new node is ready and associated to the load balancer. For this example I’ve used a couple of approaches that give you an idea of how it works.

Within the section system.webServer I’ve added the following:

<applicationInitialization doAppInitAfterRestart="true">
  <add initializationPage="/Notify/LogicApp" hostName="arraffinity.azurewebsites.net" />
  <add initializationPage="/Notify/RequestBin" hostName="arraffinity.azurewebsites.net" />
</applicationInitialization>

As you can see there are two calls:  /Notify/LogicApp and /Notify/RequestBin containing the following logic:

using System.IO;
using System.Net;
using System.Text;
using System.Web.Mvc;
 
namespace ARRTest.Controllers
{
    public class NotifyController : Controller
    {
 
        // GET: Notify
        public void RequestBin()
        {
            var request = (HttpWebRequest)WebRequest.Create("http://requestb.in/1fl81uk1");
 
            DoRequest(request);
        }
 
        // GET: Notify
        public void LogicApp()
        {
            var request = (HttpWebRequest)WebRequest.Create("https://prod-07.northeurope.logic.azure.com:443/workflows/f335b522855146cda8a78505b3fac57e/triggers/manual/run?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=EeQwQ4_ZS-L6UubWCwbA95XAf54e5PUYH3smX27Qaqg");
 
            DoRequest(request);
        }
 
        private void DoRequest(HttpWebRequest request)
        {
            var data = Encoding.ASCII.GetBytes(string.Format("webapp={0}&instanceName={1}", Request.Url.Host, Server.MachineName));
 
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
 
            using (var stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }
 
            var response = (HttpWebResponse)request.GetResponse();
 
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
        }
    }
}

Those two calls make a POST request to two different URLs. The first one to the RequestBin service that allows me to pick up a request and display the content.

RequestBin - Application Initialization Module
RequestBin – Application Initialization Module
For the second example I’ve used the Azure Logic Apps service, which allows me to chain a series of actions. I’ve included a notification through email, to alert me when the new instance is ready.
Logic App Run - Application Initialization Module
Logic App Run – Application Initialization Module

For this example, it sends a simple email with the name of the instance and the domain of the web site.

Email - Application Initialization Module
Email – Application Initialization Module

However, it could be integrated with any system that accepts HTTP requests and monitor when the scaling has taken effect or when the nodes have been restarted.

Happy clouding!