Context
Function Compute passes a context object to each handler, providing the request ID, function metadata, region, and logger for the invocation.
Runtime differences
Python 3.12 restructures the context object from earlier runtimes (3.10, 3.9, 3.6).
| Feature | Python 3.12 | Python 3.10, 3.9, 3.6 |
|---|---|---|
credentials field |
Removed. Use environment variables. | Available as Credentials struct. |
service struct |
Removed. log_project, log_store, qualifier, and version_id moved to FunctionMeta or top-level context. |
Available as ServiceMeta struct. |
logger field |
Available on the context object. | Not available. |
FunctionMeta fields |
6 fields: name, handler, memory, timeout, version_id, qualifier. |
4 fields: name, handler, memory, timeout. |
Python 3.12
Context properties
| Property | Type | Description |
|---|---|---|
request_id |
String | Unique request identifier. Record for troubleshooting. |
function |
FunctionMeta |
Invoked function metadata. FunctionMeta fields. |
region |
String | Region ID, such as cn-shanghai for China (Shanghai). Endpoints. |
logger |
Logger | Logger for function execution. Initially None; call context.getLogger() to obtain a logging.Logger. |
account_id |
String | ID of the Alibaba Cloud account that owns the function. |
log_project |
String | Simple Log Service project name associated with the function. |
log_store |
String | Simple Log Service Logstore name associated with the function. |
FunctionMeta fields (Python 3.12)
| Field | Description |
|---|---|
name |
Function name. |
handler |
Handler entry point. |
memory |
Memory allocated to the function, in MB. |
timeout |
Maximum execution time, in seconds. |
version_id |
Version of the function being invoked. |
qualifier |
Version or alias specified when the function was invoked. |
Credentials migration
The credentials field is removed in Python 3.12. Use environment variables instead.
To access other Alibaba Cloud services, use these environment variables:
| Environment variable | Description |
|---|---|
ALIBABA_CLOUD_ACCESS_KEY_ID |
Temporary AccessKey ID. |
ALIBABA_CLOUD_ACCESS_KEY_SECRET |
Temporary AccessKey Secret. |
ALIBABA_CLOUD_SECURITY_TOKEN |
Security token. |
Configure environment variables.
Before and after comparison:
# Python 3.10 and earlier -- credentials from context
def handler(event, context):
creds = context.credentials
access_key_id = creds.access_key_id
access_key_secret = creds.access_key_secret
security_token = creds.security_token
# Python 3.12 -- credentials from environment variables
import os
def handler(event, context):
access_key_id = os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
access_key_secret = os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
security_token = os.environ['ALIBABA_CLOUD_SECURITY_TOKEN']
Context class definition
# -*- coding: utf-8 -*-
import json
class FunctionMeta:
def __init__(self, name, handler, memory, timeout, version_id, qualifier):
self.name = name
self.handler = handler
self.memory = memory
self.timeout = timeout
self.version_id = version_id
self.qualifier = qualifier
class FCContext:
def __init__(self, account_id, request_id, function_meta, logger, log_project, log_store,
region):
self.account_id = account_id
self.request_id = request_id
self.function = function_meta
self.logger = logger
self.log_project = log_project
self.log_store = log_store
self.region = region
Python 3.10, 3.9, and 3.6
Context properties
| Property | Type | Description |
|---|---|---|
request_id |
String | Unique request identifier. Record for troubleshooting. |
credentials |
Credentials |
Temporary credentials from the AssumeRole API, valid for 36 hours. Access other Alibaba Cloud services without embedding an AccessKey pair. Credentials fields. Grant Function Compute permissions to access other Alibaba Cloud services. |
function |
FunctionMeta |
Invoked function metadata. FunctionMeta fields. |
service |
ServiceMeta |
Service metadata. ServiceMeta fields. |
region |
String | Region ID, such as cn-shanghai for China (Shanghai). Endpoints. |
account_id |
String | ID of the Alibaba Cloud account that owns the function. |
Credentials fields
| Field | Description |
|---|---|
access_key_id |
Temporary AccessKey ID. |
access_key_secret |
Temporary AccessKey Secret. |
security_token |
Security token. |
After you assign a RAM role to a function, Function Compute obtains temporary credentials through the AssumeRole API. These credentials are valid for 36 hours and allow the function to access other Alibaba Cloud services without an embedded AccessKey pair.
FunctionMeta fields (Python 3.10, 3.9, and 3.6)
| Field | Description |
|---|---|
name |
Function name. |
handler |
Handler entry point. |
memory |
Memory allocated to the function, in MB. |
timeout |
Maximum execution time, in seconds. |
ServiceMeta fields
| Field | Description |
|---|---|
log_project |
Simple Log Service project name. |
log_store |
Simple Log Service Logstore name. |
qualifier |
Version or alias of the service specified when the function was invoked. |
version_id |
Version of the service that was actually invoked. |
qualifieris the version or alias from the invocation request;version_idis the version actually executed. They differ when an alias resolves to a specific version.
Context class definition
# -*- coding: utf-8 -*-
import json
class Credentials:
def __init__(self, access_key_id, access_key_secret, security_token):
self.access_key_id = access_key_id
self.access_key_secret = access_key_secret
self.security_token = security_token
class Tracing:
def __init__(self, span_context, base64_baggages, jaeger_endpoint):
self.span_context = span_context
self.jaeger_endpoint = jaeger_endpoint
self.span_baggages = self.parseOpenTracingBaggages(base64_baggages)
def parseOpenTracingBaggages(self, base64_baggages):
span_baggages = {}
# None || '' returns false
if base64_baggages:
try:
import base64
str_baggages = base64.b64decode(base64_baggages)
span_baggages = json.loads(str_baggages)
except Exception as e:
import logging
fc_sys_logger = logging.getLogger('fc_sys_logger')
fc_sys_logger.error('Failed to parse base64 opentracing baggages:[{}], err: {}'.format(base64_baggages, e))
return span_baggages
class FunctionMeta:
def __init__(self, name, handler, memory, timeout):
self.name = name
self.handler = handler
self.memory = memory
self.timeout = timeout
class FCContext:
def __init__(self, account_id, request_id, credentials, function_meta, service_meta, region, tracing):
self.credentials = credentials
self.function = function_meta
self.request_id = request_id
self.service = service_meta
self.region = region
self.account_id = account_id
# self.tracing = tracing
Example
Example 2: Read and write OSS resources using a temporary AccessKey pair.