Help Center> FunctionGraph> FAQs> Function Execution FAQs> Can a Function Invoke Another Function?
Updated on 2023-06-20 GMT+08:00

Can a Function Invoke Another Function?

Yes. A function can invoke another function using an API or SDK in the same region. For cross-region invocation, the caller must have public access.

Using an API

For details, see Example 4: Using a Function to Invoke a Subfunction.

Using an SDK (Python)

  1. Create a function named FuncB, which will be invoked.

    Create the function from scratch with the Python 3.6 runtime and the following code:

    # -*- coding:utf-8 -*-
    import json
    def handler (event, context):
           # Prints input parameters to verify the invocation result.
        logger = context.getLogger()
        logger.info(event)
        return {
            "statusCode": 200,
            "isBase64Encoded": False,
            "body": json.dumps(event),
            "headers": {
                "Content-Type": "application/json"
            }
        }
  2. Create an invoking function named FuncA.

    1. Create the function from scratch with the Python 3.6 runtime and the following code. Replace <Your Function Region> and <Your Function URN> with the region and URN of FuncB.

    # -*- coding:utf-8 -*-
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkfunctiongraph.v2.region.functiongraph_region import FunctionGraphRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkfunctiongraph.v2 import *
    def handler (event, context):
        logger = context.getLogger()
        ak = context.getAccessKey()
        sk = context.getSecretKey()
    
        credentials = BasicCredentials(ak, sk)
        client = FunctionGraphClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(FunctionGraphRegion.value_of("<Your Function Region>")) \
            .build()
    
        try:
            request = InvokeFunctionRequest()
            request.function_urn = "<Your Function URN>"
            # Input parameters.
            request.body = "{'input_key':'input_value'}"
            # Response body format. v0: text; v1: JSON.
            request.x_cff_request_version = "v1"
            response = client.invoke_function(request)
            logger.info(response)
        except exceptions.ClientRequestException as e:
            logger.error(e.status_code)
            logger.error(e.request_id)
            logger.error(e.error_code)
            logger.error(e.error_msg)
        return {
            "statusCode": 200,
            "isBase64Encoded": False,
            "body": "",
            "headers": {
              "Content-Type": "application/json"
            }
        }
    1. Configure dependencies for the function.

      Configure dependencies huawei-cloud-sdk-core-python-3.x and huaweicloud-sdk-python-v3, which can be downloaded from SDK Overview. For details about how to create a dependency, see How Do I Create Function Dependencies?

    2. Grant permissions to the function.

      Grant the FunctionGraph CommonOperation permission to the function by creating an agency so that the function can invoke other functions. For details, see Creating an Agency.

  3. Check the execution result.

    Configure a blank test event for FuncA, and click Test. A result similar to the following is displayed.

    Figure 1 Viewing the execution result

    View FuncB's execution logs generated in the same period.

Function Execution FAQs FAQs

more