How to instantiate edge functions in your application

Instantiate serverless functions directly within your edge application.

This guide uses the pre-configured Azion - Hello World function, which is linked to your account. You can repeat this process using any function already available on Azion Marketplace or develop your own function.


  1. Access Azion Console.
  2. On the upper-left corner of the page, open the Products menu, represented by three horizontal lines, and select Edge Application.
  3. Click the edge application you want to configure.
  4. Activate the Edge Functions module to enable functions.
  5. Click the Save button.
  6. Go to the Functions tab.
  7. Click Add Function.
  8. Name your function instance. For example: Hello World function.
  9. Select the Azion - Hello World function.
  10. Click the Save button.

The functions page now lists the newly created instance. However, this new function isn’t yet active in your application. You need to define what will trigger the function in your application. To invoke the instantiated function in the URI xxxxxxxxxx.map.azionedge.net/hello-world:

  1. Still on the Edge Application page, navigate to the Rules Engine tab.
  2. Click the Add Rule button and select Request Phase.
  3. Under the Criteria section, select the variable ${uri}.
  1. As a comparison operator, select is equal.
  2. As an argument, add /hello-world.
  3. In the Behaviors section, select Run Function from the behavior list.
  4. Select the Hello World function.
  5. Click the Save button.

To see your function running, access your application using its domain and the URI set in the rule, which is in the format xxxxxxxxx.map.azionedge.net/hello-world.


  1. Run the following PATCH request in your terminal, replacing [TOKEN VALUE] with your personal token and the <edge_application_id> variable with your edge application ID to activate the Edge Functions module:
Terminal window
curl --location --request PATCH 'https://api.azionapi.net/edge_applications/<edge_application_id>' \
--header 'Accept: application/json; version=3' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token [TOKEN VALUE]' \
--data '{
"edge_functions": true
}'
  1. You’ll receive a response with the updated value.
  2. Run the following GET request to retrieve the edge_function_id of the Hello World function:
Terminal window
curl --location 'https://api.azionapi.net/edge_functions' \
--header 'Accept: application/json; version=3' \
--header 'Authorization: Token [TOKEN VALUE]'
  1. You’ll receive a response similar to this:
{
...
"results": [
{
"id": <edge_function_id>,
"name": "Azion - Hello World",
"language": "javascript",
"code": "\r\nasync function handleRequest(request) {\r\n return new Response(\"Hello World!\",{status:200})\r\n}\r\n\r\naddEventListener(\"fetch\", event => {\r\n event.respondWith(handleRequest(event.request))\r\n})\r\n\r\n",
"json_args": {},
"function_to_run": "handleRequest",
"initiator_type": "edge_application",
"active": true,
"last_editor": "azionmanager@azion.com",
"modified": "2021-08-10T17:49:26.224184Z",
"is_proprietary_code": false,
"reference_count": 4,
"version": "-",
"vendor": "azionmanager@azion.com"
}
]
}
  1. Copy the <edge_function_id> value.
  2. Run the following POST request, replacing the edge function ID value with the value you received in the previous response:
Terminal window
curl --location 'https://api.azionapi.net/edge_applications/<edge_application_id>/functions_instances' \
--header 'Accept: application/json; version=3' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token [TOKEN VALUE]' \
--data '{
"name": "Hello World",
"edge_function_id": <edge_function_id>,
"args": {}
}'
  1. You’ll receive a response similar to this:
{
"results": {
"edge_function_id": <edge_function_id>,
"name": "Hello World",
"args": {},
"id": <edge_function_instance_id>
}
}
KeyDescription
edge_function_idThe ID of the edge function. This is not the same as the ID of the instantiated function, which is unique for each application’s function instance.
nameName of the instantiated function.
argsDefinition of arguments required for the function. Some functions don’t take arguments.
idID of the instantiated function.
  1. Run the following POST request in your terminal, replacing [TOKEN VALUE] with your personal token, the <edge_application_id> variable with your edge application ID, and the <edge_function_instance_id> variable with the function instance ID received in the previous response:
Terminal window
curl --location 'https://api.azionapi.net/edge_applications/<edge_application_id>/rules_engine/request/rules' \
--header 'Accept: application/json; version=3' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token [TOKEN VALUE]' \
--data '{
"name": "Set cache setting - /cache",
"behaviors": [
{
"name": "run_function",
"target": "<edge_function_instance_id>"
}
],
"criteria": [
[
{
"variable": "${uri}",
"operator": "is_equal",
"conditional": "if",
"input_value": "/hello-world"
}
]
]
}'
  1. You’ll receive a response similar to this, confirming your function was created successfully:
{
"results": {
"id": <rule_id>,
"name": "Run function - /hello-world",
"phase": "request",
"behaviors": [
{
"name": "run_function",
"target": "<edge_function_instance_id>"
}
],
"criteria": [
[
{
"variable": "${uri}",
"operator": "is_equal",
"conditional": "if",
"input_value": "/hello-world"
}
]
],
"is_active": true,
"order": 2,
"description": ""
}
}
  1. Wait a few minutes for the changes to propagate.

To see your function running, access your application using its domain and the URI set in the rule, which is in the format xxxxxxxxx.map.azionedge.net/hello-world.


Contributors