Functions quickstart
Create your first function in Azion Console, the Azion CLI, or the API, instantiate it on an application, and trigger it with a Rules Engine rule.
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-worldURI. - 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.
Select the interface you will use. The prerequisites and every stage below follow that choice.
Prerequisites
- 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.
Create the function
The function holds the code that answers the request. This one returns the string Hello World!.
To create the function in Azion Console:
Access Azion Console > Products Menu > Libraries > Functions.
Enter a name for the function. For example: hello-world.
In the Code tab, paste the following code:
The function is saved and available to instantiate on an application.
Instantiate the function on your application
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.
To bind the function to an application in Azion Console:
Enter a name for the instance. For example: hello-world instance.
Select the hello-world function.
The new instance appears in the Functions Instances tab.
Add the rule that runs the function
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:
Enter a name for the rule. For example: Run hello-world.
In the Criteria section, select the ${uri} variable.
Enter /hello-world as the argument.
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.
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.
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.