1 of 20
2 of 20
3 of 20
4 of 20
5 of 20
6 of 20
7 of 20
8 of 20
9 of 20
10 of 20
11 of 20
12 of 20
13 of 20
14 of 20
15 of 20
16 of 20
17 of 20
18 of 20
19 of 20
20 of 20

site

doc

blog

success stories

Edge Functions

Edit on GitHub

Azion Edge Functions allow you to create event-driven, serverless applications, at the edge of the network, closer to users.

With Edge Functions, you can perform serverless functions in response to events on Edge Nodes of our network, with no need of having or managing servers. Azion Edge Functions automatically provides that for you.

Explore how to use and get the most out of Edge Functions as follows.

  1. About Edge Functions
  2. How it works
  3. Basics
  4. Step-by-step: creating an Edge Function
  5. Edge Functions with multiple files
  6. Activating your settings
  7. Support documentation

1. About Edge Functions

Here are some of the solutions provided by Azion Edge Functions.

You can use functions to handle HTTP in the following Request and Response phases:

  • as soon as a user’s requests are received in the Edge Node (Viewer Request);
  • before the Azion Edge Node forwards the Request to the Origin (Origin Request);
  • as soon as the Edge Node gets the response from the Origin (Origin Response);
  • before the Azion Edge Node forwards the response to the user (Viewer Response).

You can also generate Responses without necessarily having to forward the request to the origin.

By using Edge Functions written in Lua and JavaScript on Azion’s Edge Computing platform, you can create a variety of solutions, for example:

  • inspect cookies to rewrite URLs to different versions of a site for A/B testing;
  • send different objects to your users based on the User-Agent header, which contains information about the device that submitted the request. For example, you can send images in different resolutions to users based on their devices;
  • inspect headers or authorized tokens, inserting a corresponding header and allowing access control before forwarding a request to the origin;
  • add, delete, and modify headers and rewrite the URL path to direct users to different cache objects;
  • generate new HTTP responses to do things like redirect unauthenticated users to login pages, or create and deliver static webpages right from the edge.

See more ways of using Edge Functions in Use Cases.


2. How it works

Create your custom functions or use any of the existing ones provided by Azion, both for Edge Application or Edge Firewall.

The languages currently supported by the platform are Lua and JavaScript.

Edge Functions run during the handling of the request, and the Azion Edge Computing Platform provides a Rules Engine model that can trigger the execution of the Edge Functions code according to the handling phases.

The language-specific Runtime provides a programming interface for interacting and manipulating Request and Response objects to implement the necessary logic.

When instantiating an Edge Function, you can enter parameters that will be passed on to the function, in JSON format, through arguments. You can also define and run tests online to validate its construction.

Edge Functions are performed directly on Azion Edges infrastructure. To use them, they just need to be associated to a Behavior in the Rules Engine. Thus, when a request meets the criteria defined in the Rule Engine rules, the Edge Function will be triggered.

Next, you’ll find more details about the Azion Edge Functions.


3. Basics

You can create Edge Functions and maintain a repository of functions that can be used in the Edge Application or Edge Firewall. Consult the Runtime API according to the Runtime chosen for writing the Edge Function.

In addition to customizing your own functions, you can also choose from the ready-to-use ones provided by Azion or Independent Software Vendors (ISV). Browse the Azion Marketplace catalog on the Real-Time Manager.

Create Edge Functions

Use the Runtime API of your preferred language to write Edge Functions.

JavaScript language

By using the JavaScript Runtime Environment, Edge Functions written by the user go directly into effect without undergoing an internal review because the code runs on limited to isolated resources.

You can modify a function’s behavior without changing the code itself. This means you do not need to hard-code the function. In the Args tab, you can insert JSON parameters by internal code functions. This format allows you to designate variable alternatives to your code that can contain details about a function and request.

Arguments (Args) are dynamic values that can change the Edge Functions running at the Edge Nodes.

You can use the same function in different Edge Applications.

For example, if you build an Edge Function with an argument that controls whether the code needs to send data to S3, and in a particular Edge Application you determine true, at the Functions Instance, in the args, the function will generate a post for an S3 so the results of the request are kept.

Structure:

Arguments are always JSON structures that will be stored in a function configuration through the Args tab in the Functions module.

Instantiating:

In order to instantiate Args, use event.args.<ARG_CREATED>. By using this command you will be able to connect to the Args tab to your code.

Check out our examples page.

Lua language

The Edge Functions that use code written in Lua goes through a thorough review by our software engineers before going into effect. Our goal is to ensure the security and correct use of the Edge Computing platform. We review your code according to the following criteria:

  • use of any global variables that are not allowed. Due to the multi-tenant environment, the Edge Function Lua code should avoid using global variables and shared memory;
  • blocking HTTP calls. Every call to an external service must use the HTTP protocol through asynchronous APIs so that the process is not blocked;
  • the Code or Lib must also pass the luacheck tests.

For further details of each Runtime API and code samples, see the documentation for Runtime APIs.

Edge Functions Instances

According to its initiator type, before associating an execution trigger with Edge Function, it must be instantiated in Edge Application or Edge Firewall. With the Edge Functions module enabled, you can instantiate your Edge Functions for later use in a Rules Engine Rule, through the Functions tab.

Learn more about Edge Functions Instances in the Edge Application and Edge Firewall documentation.

Edge Functions Metrics

We provide real-time information about the performance of your Edge Functions, through Real-Time Metrics.

To access the graphs, follow these steps:

  1. Access the Real-Time Manager. On the Products menu, on the top left. Select Real-Time Metrics from the Edge Analytics menu and click on Edge Functions.
  2. Select the Edge Functions and the Time Range you want to analyze. Click on the Filter button.
  3. You’ll get the total number of invocations per instance of Edge Functions from Edge Firewall, for example.

Read the Real-Time Metrics documentation to learn more about this product.


4. Step-by-step: creating an Edge Function

To see your Edge Function in effectively operation you just need to write, instantiate and associate it with a Behavior Run Function, using a Rule in the Rules Engine:

You are one step closer to creating and running serverless functions in Edge Nodes from the Azion’s powerful distributed network.

To see your Edge Function in effective operation you just need to write, instantiate and associate it with a Behavior Run Function, using a Rule in the Rules Engine:

  1. Log into the Real-Time Manager. On the Edge Computing menu, click on Edge Functions.

  2. Click on Add Function to add a new Edge Function.
  3. Name your function in the Edge Function Name field to be able to save your settings.
  4. In Language, select JavaScript. Copy the following example to the Code tab field.
async function handleRequest(request) {
 return new Response("Hello World!",
   {
       status:200
   })
}
addEventListener("fetch", event => {
 event.respondWith(handleRequest(event.request))
})
  1. In the Function to run field, enter the name of the main function to run in the source code.
  2. Select the Initiator Type, which refers to the type of module where the function will be instantiated and executed. In this example, it refers to Edge Application.
  3. Click on Save to save your settings. You return to the Edge Functions home page, where you see your list of Edge Functions.
  4. Then, access the Products menu, on the top left. Select an Edge Application from your list.
  5. On the Main Settings tab, enable the Edge Functions module in the Edge Application Modules section.
  6. On the Rules Engine tab, create or edit a Rule and in the Behavior section, in the Then field, select Run Function and associate it to the desired Edge Function.
  7. Example of response when running the Behavior Run Function:
Hello World!
  1. Use Real-Time Metrics to check on your metrics. For example, when you want to see the number of Invocations of Edge Functions instances.

Fields marked with an asterisk are required.


5. Edge Functions with multiple files

Azion’s Edge Functions work with a single JS file. In case your Edge Functions has more than one file or uses JS modules, you will need to bundle those files. One of the ways to proceed is to use Webpack — it allows you to bundle JS files into a single file.

Only JS files are supported.

We do not support Node/Deno API native, so your code must solve all dependencies.

Make sure to read our documentation to find out more about Azion’s Runtime API.

Example:

If you have an Edge Function that has a module to fetch data:

  • the index.js has the event listener and imports modules;
  • the modules folder has all modules that are required by your function;
  • the webpack.config.js has the configuration of how to bundle your function.

Requirements:

NodeJS

webpack-cli

Bundle and Upload:

After you code your function, you need to bundle it using the following command:

webpack-cli --config webpack.config.js

It will generate a directory called dist with a single file called main.js. You need to copy the content of this file and paste it into the Real-Time Manager.

File structure:

your_function/
	modules/
		data.js
	index.js
	webpack.config.js

Content:

index.js

import fetchData from "./modules/data"
 
addEventListener("fetch", (event) => {
   event.respondWith(fetchData())
})

modules/data.js

async function fetchData() {
   let data = await fetch("https://httpbin.org/get?username=azionuser&name=Azion&last_login=2021-03-10")
   let json = await data.json()
   return new Response(JSON.stringify(json["args"]), {"status": 200})
}
 
export default fetchData

webpack.config.js

The target must be “webworker” and the mode must be “production”.

module.exports = {
 target: "webworker",
 entry: "./index.js",
 mode: "production",
}

Generated file:

dist/main.js

(()=>{"use strict";addEventListener("fetch",(t=>{t.respondWith(async function(){let t=await fetch("https://httpbin.org/get?username=azionuser&name=Azion&last_login=2021-03-10"),e=await t.json();return new Response(JSON.stringify(e.args),{status:200})}())}))})();

6. Activating your Settings

You will find the following buttons at the bottom of the screen:

  • Active: this option enables or disables your settings on the system.
  • Cancel: With this option, you return to the Edge Functions home page, also discard your edits.
  • Save: Once your selections are complete, click on Save to save your settings.

When you save your settings, you return to the Edge Functions home page, where you see your list of Edge Functions sorted by these options: name, language, initiator type, last editor, last modified, ref. count and active. By clicking on the arrows on those tabs, you can also change how your list is displayed.


7. Support Documentation


Didn’t find what you were looking for? Open a support ticket.