Code Style

The code must have a function to call (handler) that will be executed every time the cloud function is called. It will receive the body content (the POST request content) and the query string (from the URL) as arguments.

The following response types are supported:

  • simple (for example, as a number or string);
  • detailed (an object with the status_code, headers or body fields).

The detailed response allows the cloud function to change HTTP status codes and headers. The use of codes 500 and 503 is reserved for monitoring the health status of the entire service.

The detailed response, unlike the simple one, has at least one of the following fields: status_code, headers, or body. If none of them are present, then the function response is considered simple and is transmitted as is.

If the function response contains at least one of the above mentioned fields, then it is considered detailed and the content of the body parameter is given as the response body; status_code is transmitted to the HTTP response code; the content of headers — to the HTTP response headers.

The remaining fields are ignored, and a corresponding warning is written to the logs.

When executing the code, it is worth highlighting two events:

  • running the container;
  • activating the cloud function.

When running the container, your code is imported. We recommend that you initialize long running connections that can be reused for many calls.

When the cloud function is activated, the function you specified (handler) is called.

In the Python Runtime

Input parameters can be obtained via the kwargs dictionary:

def handler(**kwargs):
    return f"Hello, {kwargs}"

The example above shows a simple response format. The detailed response looks as follows:

def handler(**kwargs):
    return {
        "status_code": 418,
        "headers": { "X-Auth-Token": "ABCDEFGJOHNISHIDDENFARFROMME" },
        "body": kwargs
    }

An example of initializing a connection to a DBMS when running a container can be found in the example on GitHub.

In the Node.js Runtime

Input parameters are passed by the object as the first argument to the function.

The simple response looks as follows:

module.exports.main = (inParams) => {
    return `Hello, ${inParams}`
}

The detailed response looks as follows:

module.exports.main = (inParams) => {
    return {
        status_code: 418,
        headers: { "X-Auth-Token": "ABCDEFGJOHNISHIDDENFARFROMME" },
        body: inParams
    }
}