Edgio

Cloud Functions

Due to upgrades to the Cloud Functions infrastructure, Edgio CLI version 7.13.3 and earlier are now deprecated and will undergo end-of-life on January 10, 2025. After which, you will be unable to deploy using Edgio CLI version 7.13.3 and earlier. Additionally, existing deployments that use Cloud Functions may experience degraded performance.
Learn how to upgrade.
Edgio makes it easy to develop, test, and deploy Cloud Functions without a JavaScript framework. Simply declare your routes and use the compute, or proxy with the transformResponse option, methods to compute responses based on your own custom logic.

Availability

Edgio allows you to cache and deliver your content through any of our points of presence (POPs) around the world. However, your serverless code will be computed within one of the following global regions:
  • Americas: Eastern US and Western US
  • Europe: Ireland, UK, Western Europe, Northern Europe, Central Europe
  • Asia: Japan
  • Oceania: Australia
Enterprise customers may choose the region where their workloads will run. Alternatively, if you are using our free tier, then your workloads will run in the Eastern US region.
Edgio ensures high availability for your computing workloads by:
  • Running your code within two separate data centers.
  • Setting up automatic DNS failover between those data centers.
  • Load balancing computing requests between redundant processes.
  • Applying an Origin Shield to our Cloud Functions workers. If your compute requests are cacheable, then this allows more requests to be served from cache.

Prerequisites

Setup requires:

Install the Edgio CLI

If you have not already done so, install the Edgio CLI.
Bash
1npm i -g @edgio/cli@latest

Getting Started

Run the following command to create an Edgio project that supports Cloud Functions:
Bash
1npx @edgio/cli@latest init \
2 --edgioVersion latest

Responding to requests

Use the compute method to generate a synthetic response:
JavaScript
1// routes.js
2import {Router} from '@edgio/core';
3
4export default new Router().get('/some-route/:someParam', ({compute}) => {
5 compute((req, res) => {
6 // Here you can access the following information about the request:
7 // ================================================================
8
9 // To get the request path and query string
10 const url = req.url; // e.g /some/path?foo=bar
11
12 // To get the request body as a string
13 const body = req.body;
14
15 // To get the request method:
16 const method = req.method;
17
18 // To get the headers sent from the browser:
19 const headers = req.getHeaders(); // keys are always lower-case
20
21 // To get the value of a specific request header:
22 const someHeader = req.getHeader('some-header'); // the header name is case-insensitive
23
24 // To check if https was used
25 const isHttps = req.secure;
26
27 // To access query parameters
28 const {id, name} = req.query; // for example if the query string is ?id=1&name=Mark
29
30 // To access path parameters:
31 const {someParam} = req.params;
32
33 // To specify the response:
34 // ================================================================
35
36 // To set a response header:
37 res.setHeader('content-type', 'application/json');
38
39 // To set the response body, use:
40 res.body = 'some string';
41
42 // To set the response status:
43 res.statusCode = 200;
44 res.statusMessage = 'OK';
45 });
46});
See RouteHelper for all of the methods that you can call when responding to a request.

Modifying a response from the origin

Cloud Functions can be used to modify responses from the origin by using the proxy method with the transformResponse option. First, configure an origin in the edgio.config.js located in the root of your project:
JavaScript
1// edgio.config.js
2module.exports = {
3 // ... other config options
4
5 origins: [
6 {
7 // The name of the backend origin
8 name: 'origin',
9
10 // Uncomment the following to override the host header sent from the browser when connecting to the origin
11 // override_host_header: 'example.com',
12
13 // The list of origin hosts to which to connect
14 hosts: [
15 {
16 // The domain name or IP address of the origin server
17 location: 'example.com',
18 },
19 ],
20
21 // Uncomment the following to configure a shield
22 // shields: { us_east: 'DCD' },
23 },
24 ],
25};
See origins for more configuration options.
To forward a request to the origin and modify the response using a Cloud Function:
JavaScript
1// routes.js
2import {Router} from '@edgio/core';
3
4export default new Router().get('/some-route/:someParam', ({proxy}) => {
5 proxy('origin', {
6 transformRequest: (req) => {
7 // You can optionally transform the request before it is sent to the origin
8 // Here you can modify the following information about the request:
9 // ================================================================
10
11 // To alter the request URL:
12 req.url = '/some/path?foo=bar';
13
14 // To alter the request body:
15 req.body = JSON.stringify({foo: 'bar'});
16
17 // To alter the request method:
18 req.method = 'POST';
19
20 // To get the headers sent from the browser:
21 const headers = req.getHeaders();
22
23 // To get the value of an incoming request header:
24 const someHeader = req.getHeader('some-header'); // the header name is case-insensitive
25
26 // To set a request header:
27 req.setHeader('content-type', 'application/json'); // the header name is case-insensitive
28
29 // To remove a request header:
30 req.removeHeader('some-header'); // the header name is case-insensitive
31
32 // To access query parameters
33 const {id, name} = req.query; // for example if the query string is ?id=1&name=Mark
34
35 // To access path parameters:
36 const {someParam} = req.params;
37 },
38 transformResponse: (res, req) => {
39 // To access or modify the body, use:
40 res.body = 'some string';
41
42 // To set a header:
43 res.setHeader('content-type', 'application/json'); // the header name is case-insensitive
44
45 // To remove a header:
46 res.removeHeader('some-header'); // the header name is case-insensitive
47
48 // To get the headers returned from the origin. Keys are always lower case
49 const headers = res.getHeaders();
50
51 // To get a specific header returned from the origin:
52 const cookie = res.getHeader('cookie'); // the header name is case-insensitive
53 },
54 });
55});
You can also access any of the request fields documented in Responding to requests.

Caching Responses

To improve performance and minimize cost, cache the responses returned by your Cloud Functions whenever possible:
JavaScript
1// routes.js
2import {Router} from '@edgio/core';
3
4export default new Router().get('/', ({cache, compute}) => {
5 cache({
6 edge: {
7 maxAgeSeconds: 60 * 60, // cache for one hour at the edge
8 staleWhileRevalidateSeconds: 60 * 60 * 24, // and continue to serve stale responses for up to 24 hours while fetching a fresh response
9 },
10 });
11 compute((req, res) => {
12 res.setHeader('content-type', 'application/json');
13 res.body = JSON.stringify({message: 'Hello World!'});
14 });
15});
See the cache method for more options.

Running your project locally

Test your app with the Sites on your local machine by running the following command in your project’s root directory:
Bash
1edgio dev
This will start your project in watch mode. Any changes your make to your source code will instantly take effect without restarting.

Deploying your project to Edgio

Deploy your app to the Sites by running the following command in your project’s root directory:
Bash
1edgio deploy
Your initial CDN-as-code deployment will generate system-defined origin configurations along with those defined within your edgio.config.js. Learn more about system-defined origins.

Limits

Edgio Cloud Functions have the following limitations: