You need to enable JavaScript to run this app.
Volcano Engine Function as a Service

Volcano Engine Function as a Service

Copy page
Download PDF
Python runtime
Development methods
Copy page
Download PDF
Development methods
For Python functions, veFaaS uses the handler entry function defined in the index.py file that you provide to start the veFaaS service process and process request events. Python functions support both online editing in the console and uploading code packages. When submitting a code package via a local ZIP file or TOS, ensure that the root directory of the ZIP package contains a Python source file named index.py, and that this file contains an entry function named handler. This can avoid execution failures caused by the absence of the entry file or entry function.​
The sample Python function code is as follows.​
​
import json​
​
def handler(event, context):​
# Function business logic​
return {​
"statusCode": 200,​
"headers": {​
"Content-Type": "application/json"​
},​
"body": json.dumps({​
"info": "hello veFaaS!"​
})​
}​
​
Entry function handler​
The handler is the entry point for the request processing logic of the function. It contains your core business logic, which is used to process event requests, and finally returns an optional value. The signature of the handler function is as follows.​
​
def handler(event, context):​
# Function business logic​
return some_value # Optional return value​
​
When veFaaS triggers your handler function, the Python runtime passes two parameters, event and context, to your handler function.​
event parameter​
The event parameter is used to pass trigger event data to your handler function. The type of the event parameter is dict.​
The specific content of the event parameter varies based on the trigger event type. For details, see Trigger event message structure.​
context parameter​
The context parameter is used to pass runtime information to your handler function. The type of the context parameter is object.​
  • Field description​
  • The context parameter contains the following fields:​
  • ​
    Field​
    Description​
    request_id​
    The request uuid automatically generated by the system. Obtain it from the context passed into the handler function.​
    ​
  • Usage example​
  • ​
    def handler(event, context):​
    print("Request ID:", context.request_id)​
    ​
Return value​
The handler function can return an optional value of the dict type. Among the return values, only the three key-value pairs statusCode, headers, and body will be returned to the requester.​
If no value is returned, veFaaS will return a 200 status code by default.​
A sample return value of the handler function is as follows.​
​
{​
"statusCode": 200, # The response code. It has the same semantics as the HTTP status code. The default value is 200.​
"headers": { # Optional. The request header returned by the HTTP trigger.​
"Content-Type": "application/json"​
},​
"body": json.dumps({ # Optional. The body returned by the HTTP trigger.​
"message": 'hello world'​
})​
}​
​
Parameter description​
​
Parameter​
Required​
Description​
statusCode​
Optional​
The response code of the API. It has the same semantics as common HTTP status codes.​
If the parameter is not specified, the default response code is 200.​
headers​
Optional​
Response header. It contains the following secondary parameter Content-Type, which indicates the type of the message body. We recommend using the default value application/json.​
body​
Optional​
Response body. It contains the following secondary parameter message, which indicates the content of the response.​
​
Use environment variables​
For veFaaS functions, you can configure the environment variables required by the runtime of your function in the console and retrieve them in your code to implement function processing logic. veFaaS injects configured environment variables into the container where the function runs, and the program can read them using a built-in library for the programming language.​
For example, if the key of an environment variable is ENV_KEY, you can use the following code sample to read the environment variable.​
​
import os​
​
env_value = os.environ.get('ENV_KEY')​
​
​
Last updated: 2026.08.19 14:06:05