Skip to content

Retrieving information about the currently running function

Francisco Gamino edited this page Dec 17, 2019 · 7 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 on Functions V2 or Higher

Function context information is exposed by the $TriggerMetadata variable in the run.ps1 file. This variable contains the function name, directory, and invocation id. If you are using a template that does not have the $TriggerMetadata variable, you can get function directory via $PSScriptRoot. See example below:

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

write-output "Current directory:"
write-output "PSScriptRoot: $PSScriptRoot"
write-output "Directory: $($TriggerMetadata.FunctionDirectory)"

write-output "FunctionName: $($TriggerMetadata.FunctionName)"
write-output "InvocationId: $($TriggerMetadata.InvocationId)"

# more code ...

Python on Functions V2 or Higher

The function context information can be exposed by introducing context: func.Context in the function parameter. This context contains invocation_id, function_name and function_directory. See example below:

import azure.functions as func


def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    return func.HttpResponse(
        f"invocation_id = {context.invocation_id}\n"
        f"function_name = {context.function_name}\n"
        f"function_directory = {context.function_directory}"
    )

V1 Experimental Languages (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