Skip to content

Retrieving information about the currently running function

Fabio Cavalcante edited this page May 25, 2017 · 7 revisions

NOTE: This feature is only available in Azure Functions versions above 1.0.10945

In some scenarios, information about the currently running function, like the function directory or its name, are needed. Here is how you can access this information from your function code:

.NET Languages (C#, F#, etc.)

You can modify your function method to take an additional parameter of type ExecutionContext (If using pre-compiled assemblies, the full type name is Microsoft.Azure.WebJobs.ExecutionContext in the Microsoft.Azure.WebJobs.Extensions assembly).

With this additional parameter in place, the runtime will automatically provide an context instance that exposes the following properties:

Property name Type Description
InvocationId Guid Provides the invocation ID, uniquely identifying the current invocation
FunctionName String Provides the name of the current function (e.g. HttpTrigger1)
FunctionDirectory String Provides the current function directory (e.g. when running on Azure, d:\home\site\wwwroot\HttpTrigger1)

Here's an example of a C# function that uses the ExecutionContext to return the Invocation ID when executed:

public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log, ExecutionContext context)
{
    return req.CreateResponse(System.Net.HttpStatusCode.OK, context.InvocationId);
}

JavaScript/Node.JS

The context passed into your function exposes an executionContext property, which is an object with the following properties:

Property name Type Description
invocationId String Provides the invocation ID, uniquely identifying the current invocation
functionName String Provides the name of the current function (e.g. HttpTrigger1)
functionDirectory String Provides the current function directory (e.g. when running on Azure, d:\home\site\wwwroot\HttpTrigger1)

Here's an example of a JavaScript HTTP function that uses the ExecutionContext to return the Invocation ID when executed:

module.exports = function (context, req) {
    context.res = {
        body: context.executionContext.invocationId
    };
    context.done();
};

PowerShell, PHP, Python, Bash, Batch and other scripting languages

Function context information is exposed by the following environment variables:

Property name Description
EXECUTION_CONTEXT_INVOCATIONID Provides the invocation ID, uniquely identifying the current invocation
EXECUTION_CONTEXT_FUNCTIONNAME Provides the name of the current function (e.g. HttpTrigger1)
EXECUTION_CONTEXT_FUNCTIONDIRECTORY Provides the current function directory (e.g. when running on Azure, d:\home\site\wwwroot\HttpTrigger1)

Here's an example of a PowerShell HTTP function that returns a response containing the function name, directory and invocation ID in JSON format:

$content = "FUNCTIONNAME=$EXECUTION_CONTEXT_FUNCTIONNAME,FUNCTIONDIRECTORY=$EXECUTION_CONTEXT_FUNCTIONDIRECTORY" 
$result = @{Status = 200; Headers =@{ "content-type" = "text/plain" }; Body = $content} | ConvertTo-Json
Out-File -Encoding Ascii $res -inputObject $result;

Learn

Azure Functions Basics

Advanced Concepts

Dotnet Functions

Java Functions

Node.js Functions

Python Functions

Host API's

Bindings

V2 Runtime

Contribute

Functions host

Language workers

Get Help

Other

Clone this wiki locally