This guide instructs you through running your first function on Azion. The steps take about 10 minutes.
- Create a function that returns
Hello World!.
- Instantiate the function on an application.
- Add a Rules Engine rule that runs the instance on the
/hello-world URI.
- Request that URI and read the response.
Three objects make a function run, and each one links to the next:
- The function holds the JavaScript code.
- The function instance binds the function to an application. It also carries the Args, a JSON object passed into the execution context.
- The Rules Engine rule selects the instance with the Run Function behavior. The rule sets the execution phase and the criteria that trigger it.
Creating a function does not execute it. An instance without a Rules Engine rule never runs.
The Azion Terraform provider manages the same three objects. For every interface that carries them, refer to Functions.
- An Azion account. To create one, refer to How to create an account on Azion.
- The Edit Functions permission on the account. It grants permission to create, edit, and remove functions, and it also requires the permission View Functions.
- The Edit Applications permission on the account. The instance and the rule change an application, and this permission grants permission to edit, create, and remove applications. It also requires the permission View Applications. Refer to Teams Permissions.
- An application. To create one, refer to Applications quickstart.
- Access to Azion Console. To sign in, refer to Access Azion Console.
- An Azion account. To create one, refer to How to create an account on Azion.
- The Edit Functions permission on the account. It grants permission to create, edit, and remove functions, and it also requires the permission View Functions.
- The Edit Applications permission on the account. The instance and the rule change an application, and this permission grants permission to edit, create, and remove applications. It also requires the permission View Applications. Refer to Teams Permissions.
- An application. To create one, refer to Applications quickstart.
- The Azion CLI installed and authorized.
- An Azion account. To create one, refer to How to create an account on Azion.
- The Edit Functions permission on the account. It grants permission to create, edit, and remove functions, and it also requires the permission View Functions.
- The Edit Applications permission on the account. The instance and the rule change an application, and this permission grants permission to edit, create, and remove applications. It also requires the permission View Applications. Refer to Teams Permissions.
- An application. To create one, refer to Applications quickstart.
- A personal token and
curl.
The function holds the code that answers the request. This one returns the string Hello World!.
To create the function in Azion Console:
Name the functionEnter a name for the function. For example: hello-world.
Paste the code in the Code tabIn the Code tab, paste the following code:
The function is saved and available to instantiate on an application.
To read the response of the function before it serves traffic, use Preview deployment. It runs the code in Azion Console without a deployment.
The CLI reads the code from a local file. To create the function:
Save the code to a fileWrite the following code to index.js:
Read the outputThe command prints the id of the new function:
Record the id. The instance takes it. For every flag the command accepts, refer to Azion CLI create.
The request body carries the code as a JSON string, so newlines are escaped as \n.
Send the create requestReplace [TOKEN VALUE] with your personal token:
Read the responseA 202 carries the function:
Record the id. The instance takes it. The "state": "pending" value means the change takes a few minutes to propagate.
An instance binds the function to one application, and one function can back several instances. Each path turns on the Functions module for the application, then creates the instance.
Computing time and invocations for functions generate usage-related costs. For the rates, refer to Pricing.
To bind the function to an application in Azion Console:
Still in Azion Console, go to Products Menu > Build > Applications
Select the application that runs the function
In the Main Settings tab, turn on the Functions module
Go to the Functions Instances tab
Select + Function Instance
Name the instanceEnter a name for the instance. For example: hello-world instance.
Select the functionSelect the hello-world function.
The new instance appears in the Functions Instances tab.
To turn on the module and create the instance with the Azion CLI, replace the application id and the function id with your own:
Turn on the Functions moduleThe command confirms the application:
Read the outputThe command prints the id of the new instance:
Record the instance id. The rule takes it.
Two requests turn on the module and create the instance.
Turn on the Functions moduleReplace <application_id> with your application ID:
The response returns the modules the application now carries:
Create the instanceReplace <function_id> with the id the function create returned:
Read the responseA 202 carries the instance:
The function field holds the id of the function the instance runs. It differs from id, which is unique for each instance.
Record the instance id. The rule takes it.
An instance runs only when a Rules Engine rule selects it. This rule runs the instance on requests to /hello-world.
To add the rule in Azion Console:
In the same application, go to the Rules Engine tab
Name the ruleEnter a name for the rule. For example: Run hello-world.
Select the variable in the Criteria sectionIn the Criteria section, select the ${uri} variable.
Select is equal as the comparison operator
Enter the argumentEnter /hello-world as the argument.
In the Behaviors section, select Run Function
Select the instance you created
The rule runs the instance on every request whose URI is /hello-world.
The CLI reads the rule from a JSON file. To add the rule:
Write the rule to a fileSave the following as rule.json, with the id of your instance in attributes.value:
Read the outputThe command prints the id of the new rule:
The rule runs the instance on every request whose URI is /hello-world.
To add the rule with the Azion API:
Write the rule to a fileThe criteria carry ${uri}, which a shell expands, so the body is sent from a file. Save the following as rule.json, with the id of your instance in attributes.value:
Read the responseA 202 carries the rule, with the order it holds among the rules of the application:
The rule runs the instance on every request whose URI is /hello-world.
New rules can take a few minutes to propagate. Wait before you request the URI.
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.
To confirm that the function runs, request the URI set in the rule:
The response body is the string the function returns:
The function now runs on every request that matches the rule criteria.