Pingpong
Pingpong

Reputation: 8009

Check what environment Azure Functions is running on

I need to check what environment, local or Azure, Azure Functions is running on.

Below is the code based on this and this

var isLocal = string.IsNullOrEmpty(GetEnvironmentVariable("WEBSITE_INSTANCE_ID")

Is this a documented feature, or stable feature?

If not, is there an alternative?

Azure function 2.x

VS 2017

Upvotes: 2

Views: 3104

Answers (1)

Ketan
Ketan

Reputation: 1550

Azure App Service sets some environment variables with information about your Web App/Function App running on Azure.

  • WEBSITE_SITE_NAME - The name of the site.
  • WEBSITE_SKU - The sku of the site (Possible values: Free, Shared, Basic, Standard).
  • WEBSITE_COMPUTE_MODE - Specifies whether website is on a dedicated or shared VM/s (Possible values: Shared, Dedicated).
  • WEBSITE_HOSTNAME - The Azure Website's primary host name for the site (For example: site.azurewebsites.net). Note that custom hostnames are not accounted for here.
  • WEBSITE_INSTANCE_ID - The id representing the VM that the site is running on (If site runs on multiple instances, each instance will have a different id).
  • WEBSITE_NODE_DEFAULT_VERSION - The default node version this website is using.
  • WEBSOCKET_CONCURRENT_REQUEST_LIMIT - The limit for websocket's concurrent requests.

You can use WEBSITE_INSTANCE_ID to get the id of instance hosting your Function App

public static class TestFunction
    {
        [FunctionName("TestFunction")]
        public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
        {
            log.LogInformation(Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID"));
        }
    }

Reference: https://github.com/projectkudu/kudu/wiki/Azure-runtime-environment#environment

Upvotes: 2

Related Questions