Retrieving information about the currently running function

Paul Batum edited this page Apr 11, 2018 · 4 revisions

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)
FunctionAppDirectory String Points to the root directory of the function app (e.g. when running on Azure, d:\home\site\wwwroot)

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;
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.