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.
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:
- An Edgio account. Sign up for free.
- An Edgio property. Learn how to create a property.
- Node.js. View supported versions and installation steps.
- Edgio CLI.
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.js2import {Router} from '@edgio/core';34export default new Router().get('/some-route/:someParam', ({compute}) => {5 compute((req, res) => {6 // Here you can access the following information about the request:7 // ================================================================89 // To get the request path and query string10 const url = req.url; // e.g /some/path?foo=bar1112 // To get the request body as a string13 const body = req.body;1415 // To get the request method:16 const method = req.method;1718 // To get the headers sent from the browser:19 const headers = req.getHeaders(); // keys are always lower-case2021 // To get the value of a specific request header:22 const someHeader = req.getHeader('some-header'); // the header name is case-insensitive2324 // To check if https was used25 const isHttps = req.secure;2627 // To access query parameters28 const {id, name} = req.query; // for example if the query string is ?id=1&name=Mark2930 // To access path parameters:31 const {someParam} = req.params;3233 // To specify the response:34 // ================================================================3536 // To set a response header:37 res.setHeader('content-type', 'application/json');3839 // To set the response body, use:40 res.body = 'some string';4142 // 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.js2module.exports = {3 // ... other config options45 origins: [6 {7 // The name of the backend origin8 name: 'origin',910 // Uncomment the following to override the host header sent from the browser when connecting to the origin11 // override_host_header: 'example.com',1213 // The list of origin hosts to which to connect14 hosts: [15 {16 // The domain name or IP address of the origin server17 location: 'example.com',18 },19 ],2021 // Uncomment the following to configure a shield22 // 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.js2import {Router} from '@edgio/core';34export 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 origin8 // Here you can modify the following information about the request:9 // ================================================================1011 // To alter the request URL:12 req.url = '/some/path?foo=bar';1314 // To alter the request body:15 req.body = JSON.stringify({foo: 'bar'});1617 // To alter the request method:18 req.method = 'POST';1920 // To get the headers sent from the browser:21 const headers = req.getHeaders();2223 // To get the value of an incoming request header:24 const someHeader = req.getHeader('some-header'); // the header name is case-insensitive2526 // To set a request header:27 req.setHeader('content-type', 'application/json'); // the header name is case-insensitive2829 // To remove a request header:30 req.removeHeader('some-header'); // the header name is case-insensitive3132 // To access query parameters33 const {id, name} = req.query; // for example if the query string is ?id=1&name=Mark3435 // 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';4142 // To set a header:43 res.setHeader('content-type', 'application/json'); // the header name is case-insensitive4445 // To remove a header:46 res.removeHeader('some-header'); // the header name is case-insensitive4748 // To get the headers returned from the origin. Keys are always lower case49 const headers = res.getHeaders();5051 // To get a specific header returned from the origin:52 const cookie = res.getHeader('cookie'); // the header name is case-insensitive53 },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.js2import {Router} from '@edgio/core';34export default new Router().get('/', ({cache, compute}) => {5 cache({6 edge: {7 maxAgeSeconds: 60 * 60, // cache for one hour at the edge8 staleWhileRevalidateSeconds: 60 * 60 * 24, // and continue to serve stale responses for up to 24 hours while fetching a fresh response9 },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:
- Your project must comply with all applicable Edgio Performance limitations.
- Your project must comply with Edgio Sites limitations.