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 using the Cloud Function task.
Assign the following IAM roles to your user account:
- Cloud Functions Admin
Assign the Cloud Function Invoker IAM role to the
service-GCP_PROJECT_NUMBER @gcp-sa-apigee.iam.gserviceaccount.comservice account. For more information about assigning IAM roles, see Managing access in Google Cloud.
- (Optional) Create an authentication profile of type Google OIDC ID Token using the service account with the Cloud Function Invoker IAM role assigned.
To use this task, you might need to create an authentication profile. Apigee Integration uses an authentication profile to connect to Google Cloud for deploying and running the Cloud Function task.
If you have not created an authentication profile before configuring an integration task, 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:
- Go to the Apigee UI and sign in.
- Select your organization using the drop-down menu in the upper left corner of the UI.
- Click Develop > Integrations.
- Search for or select the integration to edit from the Integrations list and click to open the integration designer.
- Click Add a task/trigger > Tasks to view the list of available tasks.
- Select and drag the Cloud Function element to the integration designer.
- Click the Cloud Function element on the designer to open the configuration pane, then click Configure Cloud Function.
In the Cloud Function Configuration dialog, choose any one of
- 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.
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:
- In the task configuration pane, click Open Cloud Function.
You are redirected to the Function details page in your Google Cloud Console.
- Click Edit.
- The Configuration page lets you edit the default configuration settings of the Cloud Function. See Configuring Cloud Functions for more information.
- 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
eventobject 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.
- 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 in case of an error. For more information, see Error handling strategies.