Fastly Compute
Prerequisites
- Ensure your development environment meets the following requirements:
- Node.js installed.
- Node Package Manager (NPM) or yarn.
- An existing Fastly account is configured to handle your application’s traffic.
- Fastly Compute CLI installed locally.
- The Spec Worker Library.
- If this is your first Worker, then please use the example code.
- If your workers already have functionality, Spec's Product Success will help merge the Spec components into your Worker.
- Check out the configuration options for the Realtime Engine worker in #configuration.
Instructions
Configure the Worker
- Create a new Fastly Compute project, or open an existing project.
- Install the Spec Proxy Fastly Worker Package
npm i –save @specprotected/spec-proxy-fastly-worker
- Open the
index.js
file- If this is your first worker, Spec recommends using the example code for your Worker.
- If you have existing workers, Spec's Product Success will help merge the Spec components into your Worker.
- Run
fastly compute build
to compile the JavaScript to a WASM binary.
Configuring Fastly Compute
- Log in to Fastly:
After installing the Fastly CLI, log into your Fastly account. Navigate to the “Compute” section and select “Create Compute Service”. - Configure the Compute Service:
- Enter the domain for the traffic that Spec will be protecting.
- Set up two "Host origin" backends:
- Application Server Origin
- Spec Proxy URL (spec-internal): This URL will be provided by Spec. Its prefix will match the domain being protected.
- Set the "Override Host" value to the Host address (likely the same for both backends).
- Generate an API Token:
- Create an API token for the Fastly CLI authentication.
- Ensure the token has global scope access and set its expiration to conform to your compliance requirements.
- Authenticate the Fastly CLI:
- Run
fastly profile create
in your terminal. - Enter your API key when prompted. This stores your Fastly API key in a configuration file for future commands, avoiding the need to provide the token with each command.
- Run
- Deploy the Project to Fastly Compute:
- After saving your configuration changes, publish the project to a Fastly Compute service by running
fastly compute deploy
in your terminal.
- After saving your configuration changes, publish the project to a Fastly Compute service by running
Once these steps are completed, the Spec Fastly Compute Service will be live!
Fastly Worker Code
Spec provides an integration library for Fastly Workers, which is hosted on NPM as a public TypeScript package. This library can be integrated into your current Fastly compute implementation, or can be added to a fresh configuration.
Example Fastly Worker Code
/// <reference types="@fastly/js-compute" />
// Import required Spec methods from NPM package
import {
specProxyProcess,
setFastlyBackends,
} from "@specprotected/spec-proxy-fastly-worker";
// Sets backends for the Fastly to route traffic
// map of URL hostnames to Fastly backends
setFastlyBackends({
// This is your origin destination backend
"www.example.com": "origin",
// This is your Spec Proxy deployment backend. URL provided by Spec
"www.example.com.spec-internal.com": "spec-proxy",
});
// Spec Worker configuration
const config = {
inlineMode: true,
// due to Fastly backend services, disableSpecProxy must be set to false.
disableSpecProxy: false,
};
addEventListener("fetch", (event) => {
event.respondWith(
specProxyProcess(event, confi)
);
});
Check Edge Workers page for config options
Cache Control
Fastly has a lot of available cache options, and possible configuration for serving content as close to the requester as possible. Spec by default does not do any caching of requests. In Mirror Mode, we set the following header so Fastly will not cache our response that is backgrounded from the worker:
"Cache-Control": "no-store, private, max-age=0, no-cache"
It may be necessary to also set the following cache control headers in the worker configuration to also to Fastly from the worker level to disable caching from Spec:
event.request.headers.set("Surrogate-Control", "max-age=0");
event.request.headers.set("Cache-Control", "no-store, private, max-age=0, no-cache");