Cloud Function task

Stay organized with collections Save and categorize content based on your preferences.

You're viewing Apigee X documentation.
View Apigee Edge documentation.

The Cloud Function task lets you configure and run Google Cloud Functions from your integration. Google Cloud Functions is a lightweight compute solution for developers to create single-purpose, stand-alone functions that respond to Cloud events without the need to manage a server or a runtime environment. For more information, see Google Cloud Functions documentation.

Before you begin

Ensure that you perform the following tasks in your Google Cloud project before configuring the Cloud Function task.

  1. Assign the following IAM roles to your principal:
    • Cloud Functions Admin
    • Apigee Integration Editor
  2. Assign the Cloud Function Invoker IAM role to your default service account ( service-PROJECT_NUMBER @gcp-sa-apigee.iam.gserviceaccount.com).
  3. (Optional) Create an authentication profile of type Google OIDC ID Token using the default service account with the Cloud Function Invoker IAM role assigned.

For more information about managing access using IAM roles, see Access control.

For information on granting roles to principals, see Granting, changing, and revoking access.

Authentication

You might need to create an authentication profile depending on the configuration of your existing or new Cloud Function. If an authentication profile is indicated, then Apigee Integration uses the created profile to connect to Google Cloud for running the Cloud Function task.

If you have not created an authentication profile before configuring an integration, you can access the profile creation dialog by clicking + New authentication profile in the task configuration pane. For more information about creating authentication profiles, see Create a new authentication profile.

Configure the Cloud Function task

To configure the Cloud Function task in your integration, perform the following steps:

  1. In the Apigee UI, select your Apigee Organization.
  2. Click Develop > Integrations.
  3. Select an existing integration or create a new integration by clicking CREATE INTEGRATION.

    If you are creating a new integration:

    1. Enter a name and description in the Create Integration dialog.
    2. Select a Region for the integration from the list of supported regions.
    3. Click Create.

    This opens the integration in the integration designer.

  4. In the integration designer navigation bar, click +Add a task/trigger > Tasks to view the list of available tasks.
  5. Click and place the Cloud Function element to the integration designer.
  6. Click the Cloud Function element on the designer to open the configuration pane, then click Configure Cloud Function.
  7. In the Cloud Function Configuration dialog, choose any one of the following:
    • Link existing function. Select this option to link an existing Cloud Function that is configured in integration. Enter the Cloud Function Trigger URL.
    • Create new function. Select this option to create a new Cloud Function associated with integration. Enter the Cloud Function Name and select the function region from the drop-down list.
  8. Click Save.

    A basic Google Cloud function is created in your Google Cloud project and is associated with your integration. The task configuration pane displays the Trigger URL and the Task Parameters of the Cloud Function.

Edit a Cloud Function task

Configuring a Cloud Function task in Apigee Integration creates a basic HTTP triggered Cloud Function in your Google Cloud project.

To edit a Cloud Function task, do the following:

  1. In the task configuration pane, click Open Cloud Function.

    You are redirected to the Function details page in your Google Cloud Console.

  2. Click Edit.
  3. The Configuration page lets you edit the default configuration settings of the Cloud Function. See Configuring Cloud Functions for more information.
  4. Click Next to edit the source code files of the Cloud Function.

    By default, the Cloud Function contains the following source files:

    • main.py : This file contains the initialization code to run the Cloud Function from your integration.
    • task.py : This file contains the executable code of the Cloud Function. Write your script inside the run(event) function. This function is called when the Cloud Function task executes. The event object from the main.py file contains all the task parameters.

      See Access integration variables for information about how to use the variables defined at the integration level in your script.

  5. Click Deploy.

Access integration variables

To access an integration variable in your Cloud Function, you must pass the variable in the form of task parameters to the Cloud Function task. The task parameters are key-value pairs where Key is the name of the reference variable used in your Cloud Function source file and the Value is the corresponding integration variable name that the reference variable points to. You can add one or more task parameters in the Task Parameters section of the task configuration pane.

The following methods are used to access integration variables from your Cloud Function:

  • set: Writes the value to a variable.
  • get: Reads the value of a variable.

For example, if you have an integration variable named EmployeeName that you want to use in your Cloud Function source file, define the following task parameters:

  • Key: EmployeeKey
  • Value: EmployeeName

The following sample script shows the usage of set and get functions to access the defined integration variables.

def run(event):
  # Read the integration variable EmployeeName using the reference variable EmployeeKey
  value = event.get('EmployeeKey');
  # Change the integration variable EmployeeName value using the reference variable EmployeeKey
  newValue = event.set('EmployeeKey' , 'XYZ');
  # The new value of the integration variable is retained throughout the Cloud Function task.
  return

Retry on failure

You can configure various retry strategies to handle errors in a task. The retry strategies allow you to specify how to rerun the task or integration in case of an error. For more information, see Error handling strategies.