All Products
Search
Document Center

Function Compute:Context

Last Updated:Mar 31, 2026

Function Compute passes a context object to your function handler at invocation time. The context object carries runtime metadata about the invocation, the function configuration, the service, and the execution environment — giving your code everything it needs to log requests, access dependent services securely, and trace distributed calls.

Context object structure

Both event handlers and HTTP handlers receive the context object as the $context parameter. In the PHP runtime, $context is an array with the following structure:

[
  'requestId' => 'b1c5100f-819d-c421-3a5e-7782a27d8a33',
  'credentials' => [
    'accessKeyId'     => 'STS.access_key_id',
    'accessKeySecret' => 'access_key_secret',
    'securityToken'   => 'security_token',
  ],
  'function' => [
    'name'                  => 'my-func',
    'handler'               => 'index.handler',
    'memory'                => 128,
    'timeout'               => 10,
    'initializer'           => 'index.initializer',
    'initializationTimeout' => 10,
  ],
  'service' => [
    'name'       => 'my-service',
    'logProject' => 'my-log-project',
    'logStore'   => 'my-log-store',
    'qualifier'  => 'qualifier',
    'versionId'  => '1',
  ],
  'region'    => 'cn-shanghai',
  'accountId' => '123456',
  'tracing' => [
    'openTracingSpanContext'   => 'xxxxxxxxxxxx',
    'jaegerEndpoint'          => 'xxxxxxxx',
    'openTracingSpanBaggages' => [],
  ],
]

Context properties

PropertyTypeDescription
requestIdStringUnique ID of the current invocation. Log this value to correlate function logs with a specific call when troubleshooting errors.
credentialsArrayTemporary AccessKey pair that Function Compute obtains by assuming your service-linked role. Valid for 36 hours. Use these credentials to call other Alibaba Cloud services (such as Object Storage Service (OSS)) without embedding your AccessKey pair in function code. Fields: accessKeyId, accessKeySecret, securityToken.
functionArrayConfiguration of the invoked function. Fields: name, handler, memory, timeout, initializer, initializationTimeout.
serviceArrayService to which the function belongs, including its Log Service project and Logstore. The qualifier field reflects the version or alias specified at invocation time; versionId reflects the version that was actually executed. Fields: name, logProject, logStore, qualifier, versionId.
regionStringRegion where the function runs. For example, cn-shanghai for China (Shanghai). For all region IDs, see Endpoints.
accountIdStringAlibaba Cloud account ID that owns the function.
tracingArrayTracing Analysis parameters for distributed tracing. Fields: openTracingSpanContext, jaegerEndpoint, openTracingSpanBaggages. For details, see Overview.

Access context fields in your handler

The following example shows how to read key fields from $context in an event handler:

<?php

function handler($event, $context): string
{
    // Log the request ID for tracing and troubleshooting
    $requestId = $context['requestId'];
    error_log("Request ID: " . $requestId);

    // Read function configuration
    $funcName    = $context['function']['name'];
    $memoryLimit = $context['function']['memory'];
    error_log("Function: {$funcName}, Memory: {$memoryLimit} MB");

    // Read the invoked qualifier and actual version
    $qualifier = $context['service']['qualifier'];
    $versionId = $context['service']['versionId'];
    error_log("Qualifier: {$qualifier}, Version: {$versionId}");

    return "OK";
}

Use credentials to call OSS

The credentials field provides a temporary AccessKey pair that grants the same permissions as your function's service-linked role. Use these credentials to initialize SDK clients instead of hardcoding your AccessKey pair. The temporary credentials are valid for 36 hours. For instructions on granting OSS permissions, see Grant permissions across Alibaba Cloud accounts by using RAM roles.

What's next

  • Overview — Set up Tracing Analysis for your functions

  • Endpoints — Find the endpoint for your region