Building FunctionGraph Event Functions with Python from scratch

Following chapter describes how to build FunctionGraph event functions using Python from scratch:

Introduction

For general details about creating event functions from scratch and executing an event function, see Creating a Function from Scratch and Executing the Function in the user manual.

Function Development Overview

Use the following syntax when creating a handler function in Python:

def handler(event, context):
  # Your code here
  return data
  • handler: Name of the function that FunctionGraph invokes to execute your code.

    The name must be consistent with that you define when creating a function in FunctionGraph.

  • event: Event parameter defined for the function.

    The parameter is in JSON format.

  • context: Runtime information provided for executing the function.

    See Using the FunctionGraph context for details.

Responses are output through return.

If function throws an exception, the function execution is considered failed and the error message object is returned.

Handler

The FunctionGraph handler is the method in your function code that processes events. When the function is invoked, FunctionGraph runs the handler method. The function runs until the handler returns a response, exits or times out.

The handler method of a Python function is in the format of [file name].[function name].

By default it is index.handler.

You can configure the handler on the FunctionGraph console (Configuration -> Basic Settings -> Handler).

For example, if you set the handler to index.handler in your function configuration, FunctionGraph will load the handler function defined in the index.py file.

Defining and accessing the input event object

The input event object is defined in the event parameter of the handler function. The event parameter is in JSON format. You can define the content of the event parameter as needed.

When working with the event parameter, you can directly access the content of the event parameter by using the dot notation or bracket notation.

For example, if the event parameter is defined as follows:

{
  "order_id": "592f8c9e-1b6a-4c3e-9d5b-1234567890ab",
  "amount": "100.00",
  "item": "book"
}

You can access the value of order_id by using the following code:

order_id = event["order_id"]  # Using bracket notation
# or
order_id = event.get("order_id")  # Using get method

You can define the expected shape of the input event using type hints.

For example:

"""
Handler function for processing order events.
@param {Object} event - Input event containing order details
@param {string} event.order_id - The unique identifier for the order
@param {number} event.amount - The order amount
@param {string} event.item - The item purchased
@param {Object} context - The runtime information provided by FunctionGraph.
@return {string} A message indicating the result of processing the order.
"""

def handler(event, context):
  order_id = event["order_id"]  # Using bracket notation
  # or
  order_id = event.get("order_id")  # Using get method
  amount = event["amount"]
  item = event["item"]

  # Process the order event...

  return "Order processed successfully";

After you define the event parameter, FunctionGraph code completion will help you access the content of the event parameter.

For trigger events, the event parameter is predefined by FunctionGraph.

You can refer to the following documentation for details about the predefined event parameters for different trigger events. See: Function-supported trigger events for details.

Return value

The return value of the function output:

  • Successful execution: The defined function output information is returned.

  • Failed execution: Due to a thrown exception (errorType(message)), an error message JSON object containing errorMessage, errorType and stackTrace is returned.

    The format is as follows:

    {
      "errorMessage": "function invocation exception, error: [message]",
      "errorType": "[errorType]",
      "stackTrace": [
         "stack trace"
      ]
    }
    

    where errorMessage is the error message returned by the runtime and errorType is the error type.

Initializer

For details about the initializer, see Initializer function.

The initializer is in the format of [File name].[Initializer name].

For example, if the initializer is named index.initializer, FunctionGraph loads the initializer function defined in the index.py file.

To use Python to build initialization logic, define a Python function as the initializer.

The following is a simple initializer:

def initializer(context):
  logger = context.getLogger()
  logger.info("Initializing:", context.getFunctionName())

  return
  • Function name: The function name initializer must be the initializer function name specified for a function.

    For example, if the initializer is named index.initializer, FunctionGraph loads the initializer function defined in the index.py file.

  • context: The context parameter contains the runtime information about a function. For example, request ID, temporary AK, and function metadata. See Using the FunctionGraph context for details.