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)
- 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" } }
- 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" } }
- 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?
- 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.
- Configure dependencies for the function.
- 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 resultView FuncB's execution logs generated in the same period.
Function Execution FAQs FAQs
- How Long Does It Take to Execute a FunctionGraph Function?
- Which Steps Are Included in Function Execution?
- How Does FunctionGraph Process Concurrent Requests?
- What If Function Instances Have Not Been Executed for a Long Time?
- How Can I Speed Up Initial Access to a Function?
- How Do I Know the Actual Memory Used for Function Execution?
- Why Is My First Request Slow?
- What Do I Do If an Error Occurs When Calling an API?
- How Do I Read the Request Header of a Function?
- Can a Function Invoke Another Function?
- Can the Synchronous Execution Interface Be Invoked on a Private Network?
- Why Does a Function Use More Memory Than Estimated and Even Trigger the Out of Memory Alarm?
- How Do I Check the Memory Usage When Seeing "runtime memory limit exceeded"?
- How Do I Troubleshoot "CrashLoopBackOff"?
- After I Updated an Image with the Same Name, Reserved Instances Still Use the Old Image. What Can I Do?
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbotmore