This function invokes another function.
As you make your way through this tutorial, look out for this icon . Whenever you see it, it's time for you to perform an action.
Before you deploy this sample function, make sure you have run step A, B and C of the Oracle Functions Quick Start Guide for Cloud Shell
- A - Set up your tenancy
- B - Create application
- C - Set up your Cloud Shell dev environment
Assuming your have successfully completed the prerequisites, you should see your application in the list of applications.
fn ls apps
In order to use other OCI Services, your function must be part of a dynamic group. For information on how to create a dynamic group, refer to the documentation.
When specifying the Matching Rules, we suggest matching all functions in a compartment with:
ALL {resource.type = 'fnfunc', resource.compartment.id = 'ocid1.compartment.oc1..aaaaaxxxxx'}
Please check the Accessing Other Oracle Cloud Infrastructure Resources from Running Functions for other Matching Rules options.
Now that your dynamic group is created, create a new policy that allows the
dynamic group to read any resources you are interested in receiving
information about, in this case we will grant access to functions-family
in
the functions related compartment.
Your policy should look something like this:
Allow dynamic-group <dynamic-group-name> to use functions-family in compartment <compartment-name>
For more information on how to create policies, check the documentation.
Review the following files in the current folder:
- the code of the function, func.py
- its dependencies, requirements.txt
- the function metadata, func.yaml
In Cloud Shell, run the fn deploy command to build the function and its dependencies as a Docker image, push the image to OCIR, and deploy the function to Oracle Functions in your application.
fn -v deploy --app <app-name>
To test the function, we need another function to invoke. If you do not have any, create a HelloWorld function for example and get its OCID and endpoint.
The function requires the following keys in the payload to be invoked:
- function_ocid, the OCID of the other function we are calling (HelloWorld for example)
- function_endpoint, the endpoint of the other function
- function_body, the body for the invocation of the other function
echo '{ "function_ocid":"<function-OCID>", "function_endpoint":"<function-endpoint>", "function_body":"<function-body>" }' | fn invoke <app-name> oci-invoke-function-python
e.g.:
echo '{ "function_ocid":"ocid1.fnfunc.oc1.phx.aaaaaxxxxxxx", "function_endpoint":"https://xxxxxx.us-phoenix-1.functions.oci.oraclecloud.com", "function_body":"" }' | fn invoke myapp oci-invoke-function-python
You should see the following output:
{"message": "Hello World"}