- What's New
- Function Overview
- Service Overview
- Billing
-
Getting Started
- Creating a Function from Scratch and Executing the Function
- Creating a Function Using a Template and Executing the Function
- Creating an HTTP Function Using a Container Image and Executing the Function
- Creating an Event Function Using a Container Image and Executing the Function
- Getting Started with Common Practices
-
User Guide
- Replacing the Temporary AK/SK
- Before You Start
- Building Functions
-
Configuring Functions
- Configuring Initialization
- Configuring Basic Settings
- Configuring Agency Permissions
- Configuring the Network
- Configuring Disk Mounting
- Configuring Environment Variables
- Configuring Asynchronous Execution Notification
- Configuring Single-Instance Multi-Concurrency
- Managing Versions
- Managing Aliases
- Configuring Dynamic Memory
- Configuring Heartbeat Function
- Configuring Tags
- Configuring Snapshot-based Cold Start
- Configuring a Log Group and Log Stream
- Shared VPC
- Online Debugging
-
Creating Triggers
- Managing Triggers
- Using a Timer Trigger
- Using an APIG (Dedicated) Trigger
- Using a Kafka Trigger
- Using a DIS Trigger
- Using an SMN Trigger
- Using an LTS Trigger
- Using a CTS Trigger
- Using a DDS Trigger
- Using a GeminiDB Mongo Trigger
- Using an APIG Trigger
- Using an APIC Trigger
- Using a DMS (for RabbitMQ) Trigger
- Using an Open-Source Kafka Trigger
- Cron Expressions for a Function Timer Trigger
- Using an EG Trigger
- Invoking the Function
- Monitoring
- Function Management
- Dependency Management
- Reserved Instance Management (Old)
- Reserved Instance Management
- Flow Management
- Increasing Resource Quota
- GPU Function Management
- Application Center
- Sharing
- Programmable CDN Function
- CLI Command Reference
- Audit
-
Best Practices
- FunctionGraph Best Practices
- Processing DIS Data
- Integrating with LTS to Analyze Logs in Real Time
- Integrating with CTS to Analyze Login/Logout Security
- Periodically Starting or Stopping Huawei Cloud ECSs
- Building an HTTP Function with Spring Boot
- Creating a FunctionGraph Backend API That Uses a Custom Authorizer
- Uploading Files with FunctionGraph and APIG
- Processing IoT Data
- Workflow + Function: Automatically Processing Data in OBS
- Filtering Logs in Real Time by Using FunctionGraph and LTS
- Building an HTTP Function with Go
- Using FunctionGraph HTTP Functions to Process gRPC Requests
- Cold Start Optimization Practices
-
Developer Guide
- Overview
- Initializer
- Node.js
- Python
- Java
- Go
- C#
- PHP
- Development Tools
-
API Reference
- Before You Start
- API Overview
- Calling APIs
- Examples
- Extension and OpenTelemetry APIs
-
APIs
- Function Invocation
- Function Quotas
- Dependencies
- Test Events
- Function Tracing
-
Function Lifecycle Management
- Querying Functions
- Creating a Function
- Deleting a Function or Function Version
- Querying the Code of a Function
- Modifying the Code of a Function
- Querying the Metadata of a Function
- Modifying the Metadata of a Function
- Updating Max. Instances of a Function
- Querying Function Tags
- Enabling or Disabling the Snapshot Function
- Querying ServiceBridge Functions Bound to a Specified Function
- Querying Snapshot Status
- Querying Resource Tags
- Querying Resources
- Deleting Resource Tags
- Creating Resource Tags
- Creating a VPC Endpoint
- Deleting a VPC Endpoint
- Updating the Pinning Status of a Function
- Querying the Available ServiceBridge Version
- Versions and Aliases
- Function Metrics
- Function Logs
- Function Templates
- Reserved Instances
- Function Import and Export
- Function Triggers
-
Function Flows
- Executing a Flow Synchronously
- Executing a Flow Asynchronously
- Deleting Flows
- Querying a Flow
- Creating a Flow
- Querying Instances of a Flow
- Querying a Flow Instance
- Querying Metadata of a Flow Instance
- Modifying Metadata of a Flow Instance
- Querying Flow Metrics
- Querying Metrics of a Flow
- Re-executing a Flow
- Stopping a Flow
- Querying Records of a Flow in Pagination Mode
- Calling Back a Flow
-
Asynchronous Execution Notification
- Querying Asynchronous Execution Notification Settings of a Function Version
- Deleting Asynchronous Execution Notification Settings
- Configuring Asynchronous Execution Notification
- Querying Asynchronous Execution Notification Settings of a Function's All Versions
- Querying Asynchronous Invocation Requests
- Querying Active Asynchronous Invocation Requests
- Stopping an Asynchronous Invocation Request
- Permissions Policies and Supported Actions
- Appendix
- Change History
- SDK Reference
-
FAQs
-
General FAQs
- What Is FunctionGraph?
- Do I Need to Apply for Any Compute, Storage, or Network Services When Using FunctionGraph?
- Do I Need to Deploy My Code After Programming?
- How Do I Obtain a Token?
- What Runtimes Does FunctionGraph Support?
- How Much Disk Space Is Allocated to Each FunctionGraph Function?
- Does FunctionGraph Support Function Versioning?
- How Does a Function Read or Write Files?
- How Do I Set a Proxy When Using CLI?
- Does FunctionGraph Support Function Extension?
- Which Permissions Are Required for an IAM User to Use FunctionGraph?
- How Can I Create an ODBC Drive-based Python Dependency Package for Database Query?
- What Is the Quota of FunctionGraph?
- What Chinese Fonts Does FunctionGraph Support?
- How Does FunctionGraph Resolve a Private DNS Domain Name?
- How Does a Container Image–based Function Resolve a Private DNS Domain Name?
- How Do I Use a Domain Name to Access an API Registered with API Gateway (Dedicated)?
- What Are the Common Application Scenarios of FunctionGraph?
- Why Can't the API Gateway Domain Name Bound to a Service Be Resolved During Function Invocation?
- Does FunctionGraph Support Synchronous Transmission at the Maximum Intranet Bandwidth?
- What If the VPC Quota Is Used Up?
- How Can I Print Info, Error, or Warn Logs?
- Can I Set the Domain Name of an API to My Own Domain Name?
- Can I Change the Runtime?
- Can I Change a Function's Name?
- Why Is Message "failed to mount exist system path" Displayed?
- How Do I Obtain Uploaded Files?
- Why Can't I Receive Responses for Synchronous Invocation?
- What Should I Do If the os.system("command &") Execution Logs Are Not Collected?
- Which Directories Can Be Accessed When a Custom Runtime Is Used?
- Which Minor Versions of Python 3.6 and 3.9 Are Supported?
- Which Actions Can Be Used Instead of a VPC Administrator Agency for VPC Access?
- What Are the Possible Causes for Function Timeout?
- How Do I Obtain the Code of a Function?
- Do You Have Sample Code for Initializers?
- How Do I Enable Structured Log Query?
- Can I Enable a Listening Port in a Function to Receive External TCP Requests via EIP?
- Does FunctionGraph Support Domain Name Resolution?
- How Do I Obtain the Source IP Address of an HTTP Request Initiated by a Function?
- Function Creation FAQs
-
Trigger Management FAQs
- What Events Can Trigger a FunctionGraph Function?
- What If Error Code 500 Is Reported When Functions that Use APIG Triggers Return Strings?
- What Do LATEST and TRIM_HORIZON Mean in DIS Trigger Configuration?
- How Do I Use an APIG Trigger to Invoke a Function?
- How Does a Function Obtain the Request Path or Parameters When Using an APIG Trigger?
- Can I Configure a Kafka Trigger in a Different Subnet from My Function?
-
Dependency Management FAQs
- What Is a Dependency?
- When Do I Need a Dependency?
- What Are the Precautions for Using a Dependency?
- What Dependencies Does FunctionGraph Support?
- Does FunctionGraph Support Class Libraries?
- How Do I Use Third-Party Dependencies on FunctionGraph?
- How Do I Create Function Dependencies?
- How Do I Create a Dependency on the FunctionGraph Console?
- How Do I Add a Dependency to a Function?
-
Function Execution FAQs
- How Long Does It Take to Execute a FunctionGraph Function?
- Which Steps Are Included in Function Execution?
- How Does FunctionGraph Process Concurrent Requests?
- What If Function Instances Have Not Been Executed for a Long Time?
- How Can I Speed Up Initial Access to a Function?
- How Do I Know the Actual Memory Used for Function Execution?
- Why Is My First Request Slow?
- What Do I Do If an Error Occurs When Calling an API?
- How Do I Read the Request Header of a Function?
- Can the Synchronous Execution Interface Be Invoked on a Private Network?
- Why Does a Function Use More Memory Than Estimated and Even Trigger the Out of Memory Alarm?
- How Do I Check the Memory Usage When Seeing "runtime memory limit exceeded"?
- How Do I Troubleshoot "CrashLoopBackOff"?
- After I Updated an Image with the Same Name, Reserved Instances Still Use the Old Image. What Can I Do?
- Function Configuration FAQs
- External Resource Access FAQs
-
Other FAQs
- How Do I View the Alarm Rules Configured for a Function?
- Does FunctionGraph Support ZIP Decompiling During Video Transcoding?
- Will Resources Created During FunctionGraph 2.0 OBT Be Automatically Released When They Expire? Will Them Be Billed?
- What Is an App in FunctionGraph?
- Do I Need to Pay for Cold Start Time?
- Why Am I Seeing a Message Indicating that My Account Was Suspended When Creating a Function?
- Will the Requests of All My Functions in Different Regions Be Billed?
- Migration from FunctionGraph V1 to V2
-
General FAQs
-
More Documents
-
User Guide (ME-Abu Dhabi Region)
- Service Overview
- Getting Started
- Before You Start
- Building Functions
-
Configuring Functions
- Configuring Initialization
- Configuring Basic Settings
- Configuring Agency Permissions
- Configuring the Network
- Configuring Disk Mounting
- Configuring Environment Variables
- Configuring Asynchronous Execution Notification
- Configuring Single-Instance Multi-Concurrency
- Managing Versions
- Managing Aliases
- Configuring Dynamic Memory
- Configuring Heartbeat Function
- Online Debugging
- Creating Triggers
- Invoking the Function
- Monitoring
- Function Management
- Dependency Management
- Reserved Instance Management
- Increasing Resource Quota
- Audit
-
FAQs
-
General FAQs
- What Is FunctionGraph?
- Do I Need to Apply for Any Compute, Storage, or Network Services When Using FunctionGraph?
- Do I Need to Deploy My Code After Programming?
- What Runtimes Does FunctionGraph Support?
- How Much Disk Space Is Allocated to Each FunctionGraph Function?
- Does FunctionGraph Support Function Versioning?
- How Does a Function Read or Write Files?
- Does FunctionGraph Support Function Extension?
- Which Permissions Are Required for an IAM User to Use FunctionGraph?
- How Can I Create an ODBC Drive-based Python Dependency Package for Database Query?
- What Is the Quota of FunctionGraph?
- How Does FunctionGraph Resolve a Private DNS Domain Name?
- How Does a Container Image–based Function Resolve a Private DNS Domain Name?
- How Do I Use a Domain Name to Access an API Registered with API Gateway (Dedicated)?
- What Are the Common Application Scenarios of FunctionGraph?
- Why Can't the API Gateway Domain Name Bound to a Service Be Resolved During Function Invocation?
- Does FunctionGraph Support Synchronous Transmission at the Maximum Intranet Bandwidth?
- What If the VPC Quota Is Used Up?
- How Can I Print Info, Error, or Warn Logs?
- Can I Set the Domain Name of an API to My Own Domain Name?
- Can I Change the Runtime?
- Can I Change a Function's Name?
- Why Is Message "failed to mount exist system path" Displayed?
- How Do I Obtain Uploaded Files?
- Why Can't I Receive Responses for Synchronous Invocation?
- What Should I Do If the os.system("command &") Execution Logs Are Not Collected?
- Which Directories Can Be Accessed When a Custom Runtime Is Used?
- Which Minor Versions of Python 3.6 and 3.9 Are Supported?
- Which Actions Can Be Used Instead of a VPC Administrator Agency for VPC Access?
- What Are the Possible Causes for Function Timeout?
- How Do I Obtain the Code of a Function?
- Do You Have Sample Code for Initializers?
- How Do I Enable Structured Log Query?
- Can I Enable a Listening Port in a Function to Receive External TCP Requests via EIP?
- Function Creation FAQs
-
Trigger Management FAQs
- What Events Can Trigger a FunctionGraph Function?
- What If Error Code 500 Is Reported When Functions that Use APIG Triggers Return Strings?
- What Do LATEST and TRIM_HORIZON Mean in DIS Trigger Configuration?
- Why Can't I Enable or Disable OBS Triggers by Calling APIs?
- How Do I Use an APIG Trigger to Invoke a Function?
- How Does a Function Obtain the Request Path or Parameters When Using an APIG Trigger?
- Can I Create an OBS Trigger with an Existing Bucket?
-
Dependency Management FAQs
- What Is a Dependency?
- When Do I Need a Dependency?
- What Are the Precautions for Using a Dependency?
- What Dependencies Does FunctionGraph Support?
- Does FunctionGraph Support Class Libraries?
- How Do I Use Third-Party Dependencies on FunctionGraph?
- How Do I Create Function Dependencies?
- How Do I Create a Dependency on the FunctionGraph Console?
- How Do I Add a Dependency to a Function?
-
Function Execution FAQs
- How Long Does It Take to Execute a FunctionGraph Function?
- Which Steps Are Included in Function Execution?
- How Does FunctionGraph Process Concurrent Requests?
- What If Function Instances Have Not Been Executed for a Long Time?
- How Can I Speed Up Initial Access to a Function?
- How Do I Know the Actual Memory Used for Function Execution?
- Why Is My First Request Slow?
- What Do I Do If an Error Occurs When Calling an API?
- How Do I Read the Request Header of a Function?
- Why Does a Function Use More Memory Than Estimated and Even Trigger the Out of Memory Alarm?
- How Do I Check the Memory Usage When Seeing "runtime memory limit exceeded"?
- How Do I Troubleshoot "CrashLoopBackOff"?
- After I Updated an Image with the Same Name, Reserved Instances Still Use the Old Image. What Can I Do?
- Function Configuration FAQs
- External Resource Access FAQs
- Other FAQs
-
General FAQs
-
API Reference (ME-Abu Dhabi Region)
- Before You Start
- API Overview
- Calling APIs
- Function Model Definition
-
Function Management Zone APIs
- Querying a Function List
- Querying the Metadata of a Function
- Querying the Code of a Function
- Creating a Function
- Deleting a Function or Function Version
- Modifying the Code of a Function
- Modifying the Metadata of a Function
- Publishing a Function Version
- Querying the Versions of a Function
- Creating an Alias for a Function Version
- Modifying the Alias Information About a Function Version
- Deleting an Alias of a Function Version
- Querying the Alias Information About a Function Version
- Querying the Version Alias List of a Function
- Querying All Triggers of a Function
- Querying the Information About a Trigger
- Deleting All Triggers of a Function
- Creating a Trigger
- Deleting a Trigger
- Function Data Zone APIs
- Permissions Policies and Supported Actions
- Appendix
- Change History
- Developer Guide (ME-Abu Dhabi Region)
-
User Guide (Kuala Lumpur Region)
- Service Overview
- Getting Started
- Before You Start
- Building Functions
-
Configuring Functions
- Configuring Initialization
- Configuring Basic Settings
- Configuring Agency Permissions
- Configuring the Network
- Configuring Disk Mounting
- Configuring Environment Variables
- Configuring Asynchronous Execution Notification
- Configuring Single-Instance Multi-Concurrency
- Managing Versions
- Managing Aliases
- Configuring Dynamic Memory
- Online Debugging
- Creating Triggers
- Invoking the Function
- Monitoring
- Function Management
- Dependency Management
- Reserved Instance Management
- Audit
-
FAQs
-
General FAQs
- What Is FunctionGraph?
- Do I Need to Apply for Any Compute, Storage, or Network Services When Using FunctionGraph?
- Do I Need to Deploy My Code After Programming?
- What Runtimes Does FunctionGraph Support?
- How Much Disk Space Is Allocated to Each FunctionGraph Function?
- Does FunctionGraph Support Function Versioning?
- How Does a Function Read or Write Files?
- Does FunctionGraph Support Function Extension?
- Which Permissions Are Required for an IAM User to Use FunctionGraph?
- How Can I Create an ODBC Drive-based Python Dependency Package for Database Query?
- What Is the Quota of FunctionGraph?
- How Does a Container Image–based Function Resolve a Private DNS Domain Name?
- How Do I Use a Domain Name to Access an API Registered with API Gateway (Dedicated)?
- What Are the Common Application Scenarios of FunctionGraph?
- Why Can't the API Gateway Domain Name Bound to a Service Be Resolved During Function Invocation?
- Does FunctionGraph Support Synchronous Transmission at the Maximum Intranet Bandwidth?
- What If the VPC Quota Is Used Up?
- How Can I Print Info, Error, or Warn Logs?
- Can I Set the Domain Name of an API to My Own Domain Name?
- Can I Change the Runtime?
- Can I Change a Function's Name?
- Why Is Message "failed to mount exist system path" Displayed?
- How Do I Obtain Uploaded Files?
- Why Can't I Receive Responses for Synchronous Invocation?
- What Should I Do If the os.system("command &") Execution Logs Are Not Collected?
- Which Directories Can Be Accessed When a Custom Runtime Is Used?
- Which Minor Versions of Python 3.6 and 3.9 Are Supported?
- Which Actions Can Be Used Instead of a VPC Administrator Agency for VPC Access?
- What Are the Possible Causes for Function Timeout?
- How Do I Obtain the Code of a Function?
- Do You Have Sample Code for Initializers?
- How Do I Enable Structured Log Query?
- Can I Enable a Listening Port in a Function to Receive External TCP Requests via EIP?
- Function Creation FAQs
-
Trigger Management FAQs
- What If Error Code 500 Is Reported When Functions that Use APIG Triggers Return Strings?
- What Do LATEST and TRIM_HORIZON Mean in DIS Trigger Configuration?
- Why Can't I Enable or Disable OBS Triggers by Calling APIs?
- How Do I Use an APIG Trigger to Invoke a Function?
- How Does a Function Obtain the Request Path or Parameters When Using an APIG Trigger?
- Can I Create an OBS Trigger with an Existing Bucket?
-
Dependency Management FAQs
- What Is a Dependency?
- When Do I Need a Dependency?
- What Are the Precautions for Using a Dependency?
- What Dependencies Does FunctionGraph Support?
- Does FunctionGraph Support Class Libraries?
- How Do I Use Third-Party Dependencies on FunctionGraph?
- How Do I Create Function Dependencies?
- How Do I Create a Dependency on the FunctionGraph Console?
- How Do I Add a Dependency to a Function?
-
Function Execution FAQs
- How Long Does It Take to Execute a FunctionGraph Function?
- Which Steps Are Included in Function Execution?
- How Does FunctionGraph Process Concurrent Requests?
- What If Function Instances Have Not Been Executed for a Long Time?
- How Can I Speed Up Initial Access to a Function?
- How Do I Know the Actual Memory Used for Function Execution?
- Why Is My First Request Slow?
- What Do I Do If an Error Occurs When Calling an API?
- How Do I Read the Request Header of a Function?
- Why Does a Function Use More Memory Than Estimated and Even Trigger the Out of Memory Alarm?
- How Do I Check the Memory Usage When Seeing "runtime memory limit exceeded"?
- How Do I Troubleshoot "CrashLoopBackOff"?
- After I Updated an Image with the Same Name, Reserved Instances Still Use the Old Image. What Can I Do?
- Function Configuration FAQs
- External Resource Access FAQs
- Other FAQs
-
General FAQs
- Change History
-
API Reference (Kuala Lumpur Region)
- Before You Start
- API Overview
- Calling APIs
- Examples
-
APIs
- Function Invocation
- Function Quotas
- Dependencies
- Test Events
- Function Tracing
-
Function Lifecycle Management
- Querying Functions
- Creating a Function
- Deleting a Function or Function Version
- Querying the Code of a Function
- Modifying the Code of a Function
- Querying the Metadata of a Function
- Modifying the Metadata of a Function
- Updating Max. Instances of a Function
- Enabling or Disabling the Snapshot Function
- Querying Snapshot Status
- Querying Resource Tags
- Querying Resources
- Deleting Resource Tags
- Creating Resource Tags
- Creating a VPC Endpoint
- Deleting a VPC Endpoint
- Versions and Aliases
- Function Metrics
- Function Logs
- Reserved Instances
- Function Import and Export
- Function Triggers
-
Asynchronous Execution Notification
- Querying Asynchronous Execution Notification Settings of a Function Version
- Deleting Asynchronous Execution Notification Settings
- Configuring Asynchronous Execution Notification
- Querying Asynchronous Execution Notification Settings of a Function's All Versions
- Querying Asynchronous Invocation Requests
- Stopping an Asynchronous Invocation Request
- Appendix
- Developer Guide (Kuala Lumpur Region)
-
User Guide (ME-Abu Dhabi Region)
- Videos
- General Reference
Copied.
Custom Runtime
Scenarios
A runtime runs the code of a function, reads the handler name from an environment variable, and reads invocation events from the runtime APIs of FunctionGraph. The runtime passes event data to the function handler and returns the response from the handler to FunctionGraph.
FunctionGraph supports custom runtimes. You can use an executable file named bootstrap to include a runtime in your function deployment package. The runtime runs the function's handler method when the function is invoked.
Your runtime runs in the FunctionGraph execution environment. It can be a shell script or a binary executable file that is compiled in Linux.
After programming, simply package your code into a ZIP file (Java, Node.js, Python, and Go) or JAR file (Java), and upload the file to FunctionGraph for execution. When creating a ZIP file, place the handler file under the root directory to ensure that your code can run normally after being decompressed.
If you edit code in Go, zip the compiled file, and ensure that the name of the dynamic library file is consistent with the plug-in name of the handler. For example, if the name of the dynamic library file is testplugin.so, set the handler name to testplugin.Handler.
Compiling Description
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go
Runtime File bootstrap
If there is a file named bootstrap in your function deployment package, FunctionGraph executes that file. If the bootstrap file is not found or not executable, your function will return an error when invoked.
The runtime code is responsible for completing initialization tasks. It processes invocation events in a loop until it is terminated.
The initialization tasks run once for each instance of the function to prepare the environment for handling invocations.
Runtime APIs
FunctionGraph provides HTTP runtime APIs to receive function invocation events and returns response data in the execution environment.
- Obtaining Invocation Event
Method – Get
Path – http://$RUNTIME_API_ADDR/v1/runtime/invocation/request
This API is used to retrieve an invocation event. The response body contains the event data. The following table describes additional data about the invocation contained in the response header.
Table 1 Response header information Parameter
Description
X-Cff-Request-Id
Request ID.
X-CFF-Access-Key
AK of the account. An agency must be configured for the function if this variable is used.
X-CFF-Auth-Token
Token of the account. An agency must be configured for the function if this variable is used.
X-CFF-Invoke-Type
Invocation type of the function.
X-CFF-Secret-Key
SK of the account. An agency must be configured for the function if this variable is used.
X-CFF-Security-Token
Security token of the account. An agency must be configured for the function if this variable is used.
- Invocation Response
Method – POST
Path – http://$RUNTIME_API_ADDR/v1/runtime/invocation/response/$REQUEST_ID
This API is used to send a successful invocation response to FunctionGraph. After the runtime invokes the function handler, it publishes the response from the function to the invocation response path.
- Invocation Error
Method – POST
Path – http://$RUNTIME_API_ADDR/v1/runtime/invocation/error/$REQUEST_ID
$REQUEST_ID is the value of variable X-Cff-Request-Id in the header of an event retrieval response. For more information, see Table 1.
$RUNTIME_API_ADDR is a system environment variable. For more information, see Table 2.
This API is used to send an error invocation response to FunctionGraph. After the runtime invokes the function handler, it publishes the response from the function to the invocation response path.
Runtime Environment Variables
You can use both custom and runtime environment variables in function code. The following table lists the runtime environment variables that are used in the FunctionGraph execution environment.
Key |
Description |
---|---|
RUNTIME_PROJECT_ID |
Project ID |
RUNTIME_FUNC_NAME |
Function name |
RUNTIME_FUNC_VERSION |
Function version |
RUNTIME_PACKAGE |
App to which the function belongs |
RUNTIME_HANDLER |
Function handler |
RUNTIME_TIMEOUT |
Function timeout duration |
RUNTIME_USERDATA |
Value passed through an environment variable |
RUNTIME_CPU |
Number of allocated CPU cores |
RUNTIME_MEMORY |
Allocated memory |
RUNTIME_CODE_ROOT |
Directory that stores the function code |
RUNTIME_API_ADDR |
Host IP address and port of a custom runtime API |
The value of a custom environment variable can be retrieved in the same way as the value of a FunctionGraph environment variable.
Example
This example contains one file called bootstrap. The file is implemented in Bash.
The runtime loads the function script from the deployment package by using two variables.
The bootstrap file is as follows:
#!/bin/sh set -o pipefail #Processing requests loop while true do HEADERS="$(mktemp)" # Get an event EVENT_DATA=$(curl -sS -LD "$HEADERS" -X GET "http://$RUNTIME_API_ADDR/v1/runtime/invocation/request") # Get request id from response header REQUEST_ID=$(grep -Fi x-cff-request-id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2) if [ -z "$REQUEST_ID" ]; then continue fi # Process request data RESPONSE="Echoing request: hello world!" # Put response curl -X POST "http://$RUNTIME_API_ADDR/v1/runtime/invocation/response/$REQUEST_ID" -d "$RESPONSE" done
After loading the script, the runtime processes invocation events in a loop until it is terminated. It uses the API to retrieve invocation events from FunctionGraph, passes the events to the handler, and then sends responses back to FunctionGraph.
To obtain the request ID, the runtime saves the API response header in a temporary file, and then reads the request ID from the x-cff-request-id header field. The runtime processes the retrieved event data and sends a response back to FunctionGraph.
The following is an example of source code in Go. It can be executed only after compilation.
package main import ( "bytes" "encoding/json" "fmt" "io" "io/ioutil" "log" "net" "net/http" "os" "strings" "time" ) var ( getRequestUrl = os.ExpandEnv("http://${RUNTIME_API_ADDR}/v1/runtime/invocation/request") putResponseUrl = os.ExpandEnv("http://${RUNTIME_API_ADDR}/v1/runtime/invocation/response/{REQUEST_ID}") putErrorResponseUrl = os.ExpandEnv("http://${RUNTIME_API_ADDR}/v1/runtime/invocation/error/{REQUEST_ID}") requestIdInvalidError = fmt.Errorf("request id invalid") noRequestAvailableError = fmt.Errorf("no request available") putResponseFailedError = fmt.Errorf("put response failed") functionPackage = os.Getenv("RUNTIME_PACKAGE") functionName = os.Getenv("RUNTIME_FUNC_NAME") functionVersion = os.Getenv("RUNTIME_FUNC_VERSION") client = http.Client{ Transport: &http.Transport{ DialContext: (&net.Dialer{ Timeout: 3 * time.Second, }).DialContext, }, } ) func main() { // main loop for processing requests. for { requestId, header, payload, err := getRequest() if err != nil { time.Sleep(50 * time.Millisecond) continue } result, err := processRequestEvent(requestId, header, payload) err = putResponse(requestId, result, err) if err != nil { log.Printf("put response failed, err: %s.", err.Error()) } } } // event processing function func processRequestEvent(requestId string, header http.Header, evtBytes []byte) ([]byte, error) { log.Printf("processing request '%s'.", requestId) result := fmt.Sprintf("function: %s:%s:%s, request id: %s, headers: %+v, payload: %s", functionPackage, functionName, functionVersion, requestId, header, string(evtBytes)) var event FunctionEvent err := json.Unmarshal(evtBytes, &event) if err != nil { return (&ErrorMessage{ErrorType: "invalid event", ErrorMessage: "invalid json formated event"}).toJsonBytes(), err } return (&APIGFormatResult{StatusCode: 200, Body: result}).toJsonBytes(), nil } func getRequest() (string, http.Header, []byte, error) { resp, err := client.Get(getRequestUrl) if err != nil { log.Printf("get request error, err: %s.", err.Error()) return "", nil, nil, err } defer resp.Body.Close() // get request id from response header requestId := resp.Header.Get("X-CFF-Request-Id") if requestId == "" { log.Printf("request id not found.") return "", nil, nil, requestIdInvalidError } payload, err := ioutil.ReadAll(resp.Body) if err != nil { log.Printf("read request body error, err: %s.", err.Error()) return "", nil, nil, err } if resp.StatusCode != 200 { log.Printf("get request failed, status: %d, message: %s.", resp.StatusCode, string(payload)) return "", nil, nil, noRequestAvailableError } log.Printf("get request ok.") return requestId, resp.Header, payload, nil } func putResponse(requestId string, payload []byte, err error) error { var body io.Reader if payload != nil && len(payload) > 0 { body = bytes.NewBuffer(payload) } url := "" if err == nil { url = strings.Replace(putResponseUrl, "{REQUEST_ID}", requestId, -1) } else { url = strings.Replace(putErrorResponseUrl, "{REQUEST_ID}", requestId, -1) } resp, err := client.Post(strings.Replace(url, "{REQUEST_ID}", requestId, -1), "", body) if err != nil { log.Printf("put response error, err: %s.", err.Error()) return err } defer resp.Body.Close() responsePayload, err := ioutil.ReadAll(resp.Body) if err != nil { log.Printf("read request body error, err: %s.", err.Error()) return err } if resp.StatusCode != 200 { log.Printf("put response failed, status: %d, message: %s.", resp.StatusCode, string(responsePayload)) return putResponseFailedError } return nil } type FunctionEvent struct { Type string `json:"type"` Name string `json:"name"` } type APIGFormatResult struct { StatusCode int `json:"statusCode"` IsBase64Encoded bool `json:"isBase64Encoded"` Headers map[string]string `json:"headers,omitempty"` Body string `json:"body,omitempty"` } func (result *APIGFormatResult) toJsonBytes() []byte { data, err := json.MarshalIndent(result, "", " ") if err != nil { return nil } return data } type ErrorMessage struct { ErrorType string `json:"errorType"` ErrorMessage string `json:"errorMessage"` } func (errMsg *ErrorMessage) toJsonBytes() []byte { data, err := json.MarshalIndent(errMsg, "", " ") if err != nil { return nil } return data }
Table 3 describes the environment variables used in the preceding code.
Streaming Response
Custom runtime supports response of large packets whose size ranges from 6 MB to 200 MB in data streams.
- Log in to the FunctionGraph console, and click the name of the custom runtime function to go to the details page.
- Choose Configuration > Advanced Settings and enable Streaming Response.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot