Windows Azure Cloud Service or Web Site: Where am I Deployed?

Recently, I came across someone trying to figure out where their code was deployed between Windows Azure Web Sites and Windows Azure Cloud Services. There are many things that you may want to wrap discovery code around between deploying to Cloud Services and Web Sites. One such example that comes to mind is use of LocalStorage in Cloud Services over writing directly to disk in Web Sites.

Non .NET Example with Environment Variables

In Non .NET Languages such as PHP or Node.js you may not have a standard configuration location or you may find the implementation of Environment  Variables more convenient. In a past article I outlined how to read App Settings from the App Settings section of the Windows Azure Management Portal, which explained that App Settings configured in such a way are exposed as Environment Variables.

Cloud Services also have a way to configure custom Environment Variables in the Service Definition (CSDEF) file which is packaged up with a Cloud Service Package (CSPKG). Considering both Cloud Services and Windows Azure Web Sites enable you to expose Environment Variables this is consistent way to share information in either deployment method.

Configuring Environment Variables in Web Sites Portal

Configuring Environment Variables in Cloud Services


Environment Variable in Action

PHP


Node.js


ASP.NET Example using Web.config

In ASP.NET there is the concept of a web.config file. This is an xml based file which is used for storing configuration settings which are specific to a deployment. It is easy to pivot on environment by providing transforms of the web.config file. Let’s assume we have 3 environments Local, WebSites and CloudService.

This will yield three configuration transforms which upon build will roll up to the base web.config:

  • web.local.config
  • web.websites.config
  • web.cloud.config

Creating an App Setting

An App Setting is a Key/Value pair configured in the <appsettings /> element within the web.config file.

Web.config

Application Configuration File

We’ll set this appsettings to a value appropriate for debugging.


Web.local.config

Configuration Transform

We will provide a replacement element for the appsettings for our local test environment.


Web.websites.config

Configuration Transform

We will provide a replacement element for the appsettings for Windows Azure Web Sites.


Web.cloud.config

Configuration Transform

We will provide a replacement element for the appsettings for a Windows Azure Cloud Service.


AppSetting in Action


Conclusion

Depending on your development language or your scenario [as the Environment Variable route would work for ASP.NET as well] you can quickly and easily setup a way to verify which Environment you are deployed to be it Local Test, Windows Azure Web Sites, Windows Azure Cloud Services or any other environment you may have.

Stay Cloudy my Friends…