Writing Cloud Functions

Google Cloud Functions can be written in Node.js and Python, and are executed in language-specific runtimes.

Cloud Functions runtimes

The Cloud Functions execution environment varies by your chosen runtime. The Node.js 6, Node.js 8, and Python runtime overview pages provide details regarding versions, file naming requirements, and handling dependencies.

Naming Cloud Functions

Cloud Functions have a "name" property that is set at deploy time, and once set, it cannot be changed. The name of a function is used as its identifier and it must be unique within a region. By default, the name of a function is also used as the entry point into your source code. For example, a function with a name foo will, by default, execute the foo() function in the source code you deploy. If you want to have a different function executed, you can use the --entry-point flag at deploy time. For details, see the Deploying Cloud Functions documentation.

Types of Cloud Functions

Cloud Functions come in two distinct variants: foreground (HTTP) and background.

HTTP functions

You invoke HTTP functions from standard HTTP requests. These HTTP requests wait for the response and support handling of common HTTP request methods like GET, PUT, POST, DELETE and OPTIONS. When you use Cloud Functions, a TLS certificate is automatically provisioned for you, so all HTTP Functions can be invoked via a secure connection.

For details, see Writing HTTP Functions.

Example:

Node.js

/**
 * HTTP Cloud Function.
 *
 * @param {Object} req Cloud Function request context.
 *                     More info: https://expressjs.com/en/api.html#req
 * @param {Object} res Cloud Function response context.
 *                     More info: https://expressjs.com/en/api.html#res
 */
exports.helloHttp = (req, res) => {
  res.send(`Hello ${req.body.name || 'World'}!`);
};

Python

def hello_http(request):
    """HTTP Cloud Function.
    Args:
        request (flask.Request): The request object.
    Returns:
        The response text, or any set of values that can be turned into a
        Response object using `make_response`
        <http://flask.pocoo.org/docs/0.12/api/#flask.Flask.make_response>.
    """
    request_json = request.get_json()
    if request_json and 'message' in request_json:
        name = request_json['message']
    else:
        name = 'World'
    return 'Hello, {}!'.format(name)

Background functions

You can use background functions to handle events from your Cloud infrastructure, such as messages on a Google Cloud Pub/Sub topic, or changes in a Google Cloud Storage bucket.

For details, see Writing Background Functions.

Example:

Node.js 6

/**
 * Background Cloud Function.
 *
 * @param {object} event The Cloud Functions event.
 * @param {function} callback The callback function.
 */
exports.helloBackground = (event, callback) => {
  callback(null, `Hello ${event.data.name || 'World'}!`);
};

Node.js 8

/**
 * Background Cloud Function.
 *
 * @param {object} data The event payload.
 * @param {object} context The event metadata.
 */
exports.helloBackground = (data, context) => {
  return `Hello ${data.name || 'World'}!`;
};

Python

def hello_background(data, context):
    """Background Cloud Function.
    Args:
         data (dict): The dictionary with data specific to the given event.
         context (google.cloud.functions.Context): The Cloud Functions event
         metadata.
    """
    if data and 'name' in data:
        name = data['name']
    else:
        name = 'World'
    return 'Hello, {}!'.format(name)

Next steps

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Functions Documentation