Enable HTTP triggers for cloud functions

更新时间:
复制 MD 格式

EMAS Serverless supports function invocations that are triggered by HTTP requests. This topic describes how to enable the HTTP trigger feature for your cloud functions.

Important

After you enable the HTTP trigger feature for a function, it becomes accessible through HTTP requests. You must consider both business security and resource security.

  • Business security: Implement access control and security protection in your cloud function code to prevent unauthorized access from triggering sensitive operations.

  • Resource security: If a cloud function with HTTP access enabled receives a high volume of malicious, resource-consuming requests, you can set the function's trigger path to empty or disable the HTTP trigger feature for the service space. This action disables HTTP trigger support.

Procedure

To enable the HTTP trigger for a cloud function, you must first enable the feature for the service space and then configure an HTTP trigger path for the function.

  1. Enable the HTTP trigger feature for the space

    1. Log on to the EMAS console, select Serverless, and then click Enter to open the Serverless console.

    2. In the navigation pane on the left, choose Cloud Functions.

    3. In the upper-left corner of the page, select the service space, such as emasDev.

    4. On the HTTP Trigger Configuration tab, under HTTP Trigger Settings, set Change Status to Enabled. The system then assigns a default domain name (DefaultEndpoint) to the Space.

  2. Enable the HTTP trigger feature for the cloud function

    1. Return to the Cloud Function List tab.

    2. Select a cloud function and click its name, such as test, to open the Cloud Function details page.

    3. Click the Trigger tab, and then click the Edit button next to HTTP Path Trigger.

    4. On the Edit Cloud Function page, enter a trigger path. The following figure shows an example.

  3. Trigger the function using an HTTP request

    Accessing the URL https://${DefaultEndpoint}${HttpTriggerPath} triggers the corresponding cloud function.

Limits

  1. The request path cannot end with / and must be unique for each cloud function within the same service space. The path can only contain slashes (/), hyphens (-), underscores (_), periods (.), letters, and numbers. The maximum length is 128 characters.

  2. Only the following four request methods are supported. Requests that use other methods are rejected.

    • GET

    • POST

    • PUT

    • DELETE

  3. Only the following three `Content-Type` values are supported for the request body. Requests without a body, and therefore without a `Content-Type` header, are also allowed. Requests with other `Content-Type` values are rejected.

    • application/json

    • application/x-www-form-urlencoded

    • text/*

  4. All responses triggered by HTTP include the fixed header Content-Disposition: attachment. This header causes browsers to download the response as an attachment. Currently, this header cannot be overwritten. You can work around this issue using a custom domain name when the feature becomes available.

  5. The request body cannot exceed 1 MB.

  6. The response body cannot exceed 1 MB.

Cloud function input parameters

In a cloud function, you can use ctx.args to retrieve the content of the request. The parameters are described as follows:

{
    path: 'The URI of the request, such as /hello',
    httpMethod: 'The HTTP request method, such as GET',
    headers: {The HTTP request header, in key-value pairs},
    queryStringParameters: {The query string parameters of the HTTP request, in key-value pairs},
    body: 'The request body',
    isBase64Encoded: 'true or false. Indicates whether the body is Base64-encoded. The current value is always false.'
}

Cloud function return values

A cloud function can return data of types such as string, object, number, or boolean. It can also return a custom response. EMAS Serverless then converts the return value into a standard HTTP response.

  • Return a string

    The response has a Content-Type of text/plain.

    For example, if the cloud function returns:

    'hello world!'

    The final response is:

    HTTP/2 200
    date: Mon, 23 Mar 2020 10:12:41 GMT
    content-type: text/plain
    content-length: 12
    content-disposition: attachment
    request-id: ac14000d1584958361033101870
    hello world!
  • Return an object, number, or boolean

    The response has a Content-Type of application/json.

    For example, if the cloud function returns:

    {
        "keyString": "value1",
        "keyNumber": 1234
    }

    The final response is:

    HTTP/1.1 200 OK
    Date: Mon, 23 Mar 2020 10:18:48 GMT
    Content-Type: application/json; charset=utf-8
    Content-Length: 39
    request-id: ac14000d1584958728277106170
    content-disposition: attachment
    {"keyString":"value1","keyNumber":1234}
  • Return a custom response

    You can specify the Content-Type, HttpStatusCode, and Headers in the response from the cloud function.

    To do this, the cloud function must return an object and set the mpserverlessComposedResponse field to true.

    The return format for the cloud function is as follows:

    {
        "mpserverlessComposedResponse": true,
        "isBase64Encoded": true|false,
        "statusCode": httpStatusCode,
        "headers": { "headerName": "headerValue", ... },
        "body": "..."
    }

    For example, if the cloud function returns:

    {
      mpserverlessComposedResponse: true,
      isBase64Encoded : false,
      statusCode: 200,
      headers: {
        'content-type': 'text/html'
      },
      body: '<h1>Hello World</h1>'
    }

    The final response is:

    HTTP/2 200
    date: Mon, 23 Mar 2020 10:40:09 GMT
    content-type: text/html; charset=utf-8
    content-length: 20
    request-id: ac14000d1584960009970137070
    content-disposition: attachment
    <h1>Hello World</h1>