Fastly Compute

This document describes integrating with the Spec Platform through a Fastly worker.

Prerequisites

  • Ensure your development environment meets the following requirements:

  • 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 Spec Platform in Config.

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, config)
  );
});

Check Edge Workers page for config options

Last updated