Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Fastly Compute

Prerequisites

Instructions

Configure the Worker

  1. Create a new Fastly Compute project, or open an existing project.
  2. Install the Spec Proxy Fastly Worker Package
    1. npm i –save @specprotected/spec-proxy-fastly-worker
  3. Open the index.js file
    1. If this is your first worker, Spec recommends using the example code for your Worker.
    2. If you have existing workers, Spec's Product Success will help merge the Spec components into your Worker.
  4. Run fastly compute build to compile the JavaScript to a WASM binary.

Configuring Fastly Compute

  1. Log in to Fastly:
    After installing the Fastly CLI, log into your Fastly account. Navigate to the “Compute” section and select “Create Compute Service”.
  2. 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).
  3. 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.
  4. 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.
  5. 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.

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");