Google Cloud Functions Extension

You're viewing Apigee Edge documentation.
Go to the Apigee X documentation.
info

Version 1.3.1

Invoke Cloud Functions that are deployed through your Google Cloud project.

Currently, this extension supports invoking HTTP trigger functions.

Prerequisites

This content provides reference for configuring and using this extension. Before using the extension from an API proxy using the ExtensionCallout policy, you must:

  1. Enable the Cloud Functions API.

  2. Define and deploy functions in Cloud Functions for your Google Cloud project.

  3. Grant user access via IAM for the level of access you'll want for the function. For example, you can limit access to the function to just the service account that you use to configure the extension.

  4. Use the GCP Console to generate a key for the service account.

  5. Use the contents of the resulting key JSON file when adding and configuring the extension using the configuration reference.

About Cloud Functions

With Google Cloud Functions, you can create and deploy functions on Google Cloud, then invoke those functions from other code. For an introduction to Cloud Functions, try out one of the quickstarts.

Samples

The following example illustrates how to invoke functions in Cloud Functions using the ExtensionCallout policy.

Invoke Node.js function

The following example features an ExtensionCallout policy calling a Google Cloud Functions extension. The extension calls the default "hello world" function included when you enable the Cloud Functions API.

The following Node.js JavaScript is deployed in Cloud Functions for a GCP account. If the request includes a message property, the code returns that. Otherwise, it returns "Hello World!" as its response.

/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
exports.helloWorld = (req, res) => {
  let message = req.query.message || req.body.message || 'Hello World!';
  res.status(200).send(message);
};

This example includes a Google Cloud Functions extension configured with credentials needed to authenticate and get authorized to invoke code on Cloud Functions.

The preceding function code is saved in Cloud Functions as simply helloWorld. The following ExtensionCallout policy configuration code uses this name, along with region and project ID values matching the particulars of the Cloud Functions environment where the function is deployed.

<Action>invoke</Action>
<Input><![CDATA[
  {
    "region" : "us-central1",
    "projectId" : "my-project",
    "functionName" : "hello-world",
    "method" : "POST",
    "payload" : { "message" : "Hello yourself!" }
  }
]]></Input>
<Output parsed="false">function.response</Output>

The following AssignMessage policy captures the response value for debugging purposes.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Get-Function-Response">
    <DisplayName>Get Function Response</DisplayName>
    <AssignTo type="response" createNew="false"/>
    <Set>
        <Payload contentType="application/json">{function.response}</Payload>
    </Set>
</AssignMessage>

The response to the request above would be like the following:

Hello yourself!

The default value of parsed is true. This example sets parsed="false" in the <Output> tag of the policy, which prevents the policy from parsing the JSON response. For most situations when using the Cloud Functions extension you set parsed="false". See <Output> element for more.

If the Cloud Function returns a JSON response and you set parsed="true", then the response from the extension is an object reference. In order to extract the response from the reference, use the following syntax: {objectName}.{jsonKey}. For example:

function.response.message

Actions

invoke

Invokes a Cloud function.

Currently, this extension supports invoking HTTP trigger functions.

Syntax

<Action>invoke</Action>
<Input><![CDATA[
  {
    "region" : "deployment-region",
    "projectId" : "project-id",
    "functionName" : "function-name",
    "method" : "http-method",
    "payload" : { json-payload }
  }
]]></Input>
<Output>function.response</Output>

Example

<Action>invoke</Action>
<Input><![CDATA[
  {
    "region" : "us-central1",
    "projectId" : "my-project",
    "functionName" : "hello-world",
    "method" : "POST",
    "payload" : { "message" : "Hello yourself!" }
  }
]]></Input>
<Output>function.response</Output>

Request parameters

Parameter Description Type Default Required
region The Google Cloud region where the function is deployed. String. None. Yes.
projectId GCP project ID. String. None. Yes.
functionName The name of the HTTP function to invoke. This is the name you used when you created the function (not necessarily a name from the function's code). String. None. Yes.
method The HTTP method to use when invoking the function. String. GET No.
payload The payload to send with the function invocation. JSON. None. No.

Response

The specified function's response value, if any.

Response properties

None.

Configuration Reference

Use the following when you're configuring and deploying this extension for use in API proxies. For steps to configure an extension using the Apigee console, see Adding and configuring an extension.

Common extension properties

The following properties are present for every extension.

Property Description Default Required
name Name you're giving this configuration of the extension. None Yes
packageName Name of the extension package as given by Apigee Edge. None Yes
version Version number for the extension package from which you're configuring an extension. None Yes
configuration Configuration value specific to the extension you're adding. See Properties for this extension package None Yes

Properties for this extension package

Specify values for the following configuration properties specific to this extension.

Property Description Default Required
credentials When entered in the Apigee Edge console, this is the contents of your service account key file. When sent via the management API, it is a base64-encoded value generated from the service account key file. None. Yes.