Context

更新时间:
复制 MD 格式

This topic describes the context object available when you write code in the PHP runtime environment of Function Compute.

When Function Compute runs your function, the runtime passes a $context parameter to your handler. This array contains metadata about the current invocation, your function configuration, and temporary credentials you can use to call other Alibaba Cloud services.

Context object structure

The following example shows the structure of the $context array in the PHP runtime:

[
  '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' =>[
     'logProject' => 'my-log-project',
     'logStore' => 'my-log-store',
     'qualifier' => 'qualifier',
     'versionId' => '1'
  ],
  'region' => 'cn-shanghai',
  'accountId' => '123456',
  'tracing': {
    'openTracingSpanContext': 'xxxxxxxxxxxx',
    'jaegerEndpoint': 'xxxxxxxx',
    'openTracingSpanBaggages': []
  }
]

Context fields

Top-level fields

FieldTypeDescription
requestIdStringThe unique ID of the current invocation. Record this ID to trace issues when errors occur.
credentialsArrayThe temporary 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 hardcoding your AccessKey pair in the function code. For details, see Grant Function Compute permissions to access other Alibaba Cloud services.
functionArrayConfiguration of the invoked function.
serviceArrayInformation about the service to which the function belongs.
regionStringThe ID of the region where the function runs. For example, cn-shanghai for the China (Shanghai) region. For all region IDs, see Service endpoints.
accountIdStringThe ID of the Alibaba Cloud account that owns the function.
tracingObjectDistributed tracing context based on the OpenTracing standard, populated when tracing is enabled.

credentials fields

FieldTypeDescription
accessKeyIdStringThe AccessKey ID of the temporary credential.
accessKeySecretStringThe AccessKey secret of the temporary credential.
securityTokenStringThe security token associated with the temporary credential.

function fields

FieldTypeDescription
nameStringThe name of the function.
handlerStringThe handler of the function, in the format <file>.<method>.
memoryIntegerThe memory size configured for the function, in MB.
timeoutIntegerThe maximum execution time configured for the function, in seconds.
initializerStringThe initializer of the function, in the format <file>.<method>.
initializationTimeoutIntegerThe maximum initialization time configured for the function, in seconds.

service fields

FieldTypeDescription
logProjectStringThe Log Project in Simple Log Service associated with this service.
logStoreStringThe Logstore in Simple Log Service associated with this service.
qualifierStringThe version or alias specified when the function was invoked.
versionIdStringThe version of the service that was actually invoked.

tracing fields

FieldTypeDescription
openTracingSpanContextStringThe OpenTracing span context for the current invocation, used for distributed trace propagation.
jaegerEndpointStringThe Jaeger collector endpoint for submitting trace data.
openTracingSpanBaggagesArrayOpenTracing baggage items attached to the current span.

Access context fields in your function

The following example shows how to access common context fields in a PHP handler:

<?php

function handler($event, $context) {
    // Use requestId to correlate logs with a specific invocation
    $requestId = $context['requestId'];
    error_log("Request ID: " . $requestId);

    // Use temporary credentials to call other Alibaba Cloud services
    $accessKeyId     = $context['credentials']['accessKeyId'];
    $accessKeySecret = $context['credentials']['accessKeySecret'];
    $securityToken   = $context['credentials']['securityToken'];

    // Read function configuration
    $functionName = $context['function']['name'];
    $memoryLimit  = $context['function']['memory'];   // in MB
    $timeoutLimit = $context['function']['timeout'];  // in seconds

    // Identify which version or alias was invoked
    $qualifier = $context['service']['qualifier'];
    $versionId = $context['service']['versionId'];

    return "OK";
}
Note The credentials in the context are temporary and expire after 36 hours. Do not cache them beyond the function invocation.