Updated on 2023-11-22 GMT+08:00

Functions

If you are using Huawei Cloud FunctionGraph as a provider, all functions inside the service are Huawei Cloud FunctionGraph functions.

Configuration

All of the Huawei Cloud FunctionGraph in your Serverless service can be found in serverless.yml under the functions attribute.
# serverless.yml
service: fg-service

provider:
  name: huawei

plugins:
  - serverless-huawei-functions

functions:
  first:
    handler: index.handler

Handler

The handler attribute should be the function name you have exported in your handler file.

For example, when you export a function with the name handler in index.js, your handler should be handler: index.handler.
// index.js
exports.handler = (event, context, callback) => {};

Memory Size and Timeout

The memorySize and timeout for the functions can be specified at the provider or function level. The provider definition enables all functions to share this configuration, whereas the function definition indicates that this configuration is only valid for the function.

The default memorySize is 256 MB and the default timeout is 30s if not specified.
# serverless.yml

provider:
  memorySize: 512
  timeout: 90

functions:
  first:
    handler: first
  second:
    handler: second
    memorySize: 256
    timeout: 60

Handler Signature

The signature of an event handler is:
function (event, context) { }
  • event

    If the function is triggered by an APIG event specified, the event passed to the handler will be:

    // JSON.parse(event)
    {
      events: {
        "body": "",
        "requestContext": {
            "apiId": "xxx",
            "requestId": "xxx",
            "stage": "RELEASE"
        },
        "queryStringParameters": {
            "responseType": "html"
        },
        "httpMethod": "GET",
        "pathParameters": {},
        "headers": {
            "accept-language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
            "accept-encoding": "gzip, deflate, br",
            "x-forwarded-port": "443",
            "x-forwarded-for": "xxx",
            "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "upgrade-insecure-requests": "1",
            "host": "xxx",
            "x-forwarded-proto": "https",
            "pragma": "no-cache",
            "cache-control": "no-cache",
            "x-real-ip": "xxx",
            "user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0"
        },
        "path": "/apig-event-template",
        "isBase64Encoded": true
      }
    }
  • context

    The context parameter contains the runtime information about the function. For example, request ID, temporary AK, and function metadata. See Developing an Event Function.