Run a function on an application
Add the Rules Engine rule that runs a function instance on an application, from Azion Console or the Azion API, then verify the response.
A function instance runs only when a Rules Engine rule selects it with the Run Function behavior. You add that rule from Azion Console or the Azion API. For a guided first run that creates the function, the instance, and the rule in one pass, refer to Functions quickstart.
Prerequisites
- A function on the account. To write one, refer to Write and test a function.
- A function instance on the application. To create one, refer to Instantiate a function on an application.
- The Application Accelerator module turned on for the application. The Run Function behavior requires it. Refer to Application Accelerator.
- The Edit Applications permission on the account. The rule changes an application. Refer to Teams Permissions.
- A personal token, for the API path.
Add the rule that runs the function
The rule sets the execution phase, the criteria that trigger it, and the instance it runs. It then runs that instance on every request that matches the criteria. Azion Console and the Azion API create the same rule.
Add the rule in Azion Console
To add the rule from Azion Console:
Access Azion Console > Applications > your application.
Enter a name for the rule. For example: Run hello-world.
Select the ${uri} variable.
Enter the URI as the argument. For example: /hello-world.
The rule appears in the Rules Engine tab of the application. New rules can take a few minutes to propagate.
Add the rule with the Azion API
Send a POST request to the request rules endpoint of the application. Replace [TOKEN VALUE] with your personal token, <application_id> with your application ID, and <edge_function_instance_id> with the ID of the function instance:
The response returns the rule the application now carries:
The "state": "pending" value means the rule takes a few minutes to propagate. For every field of the rules endpoint, refer to Create an application rule.
Verify the response
Your application answers on a domain in the format xxxxxxxxx.map.azionedge.net. To set the domain for your application, refer to Add a custom domain to a workload.
Wait for the rule to propagate, then request the URI set in the criteria:
The response body is the string the function returns. The hello-world function of the Functions quickstart answers:
A response that does not come from the function means the rule did not match the request, or the change has not propagated yet.