Skip to content

Deploying a URL Shortener with Cloudflare Workers ​

2022-06-29 · david-flanagan

Cloudflare Workers provides a serverless execution environment that allows you to create entirely new applications or augment existing ones without configuring or maintaining infrastructure. They support NodeJS and WebAssembly (WASM), as well as any language that can compile to WASM.

Delivered from over 250 locations worldwide, Cloudflare could be the best way to bring down that latency that's plaguing your customers. Claiming 0ms for cold starts, automatic scaling, 100k free requests per day, and edge storage built-in: Cloudflare offers a pretty compelling edge compute platform for serverless workloads.

Let's see how we can deploy a low-latency serverless URL shortener to Cloudflare Workers with Pulumi.

Setting Up ​

You'll need a Pulumi project setup with the Cloudflare package added to your Pulumi project and an API token configured.

shell
pulumi new typescript
npm install @pulumi/cloudflare

Now that we've added the package, we need to configure it. You'll need your Cloudflare accountId and an API Token. The permissions you need for the API token are:

  • All Accounts
    • Workers Tail:Read
    • Workers Scripts:Edit
    • Account Settings:Read
  • All Zones
    • Zone Settings:Edit
    • Zone:Edit
    • Workers Routes:Edit
    • SSL and Certificates:Edit
    • DNS:Edit
  • All Users
    • User Details:Read

You can reduce the scope of "All Zones", but this means you cannot create a Zone with Pulumi and instead would need to use getZone, described below.

shell
pulumi config set --secret cloudflare:apiToken
pulumi config set cloudflare:accountId

Our URL Shortener ​

We're going to use plain old JavaScript for our worker code, as this means we don't need to transpile anything down. Our URL Shortener will store domains, short tokens, and long URLs in a JavaScript object. We'll also provide a DEFAULT_URL in-case we can't match the path requested.

We need to keep our worker source code separate from our Pulumi code, so we'll be keeping this contained within a file called worker.js.

javascript
// worker.js
const DEFAULT_URL = "https://youtube.com/pulumitv"

const redirects = {
  "pulumi.tv": {
    "modern-infrastructure": {
        to: "https://www.youtube.com/playlist?list=PLyy8Vx2ZoWloyj3V5gXzPraiKStO2GGZw"
    }
  },
};

addEventListener("fetch", async (event) => {
  event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
  const requestUrl = new URL(request.url);
  const domain = requestUrl.host;
  const path = requestUrl.pathname.substring(1);

  console.log(`Handling request on domain ${domain} for ${path}`);

  if (!(domain in redirects)) {
    console.log(`Domain '${domain}' not found`);
    return Response.redirect(DEFAULT_URL);
  }

  if (path === "") {
    return Response.redirect(DEFAULT_URL);
  }

  const paths = redirects[domain];

  if (path in paths) {
    console.log(`Redirecting too ${paths[path].to}`);
    return Response.redirect(paths[path].to);
  }

  console.log(`Path '${path}' not found`);
  return Response.redirect(DEFAULT_URL);
}

Deploying Our Worker ​

Now on to our Pulumi code. We need to read our JavaScript code and send it to Cloudflare Workers via the WorkerScript resource.

typescript
// index.ts
import * as cloudflare from "@pulumi/cloudflare";
import * as fs from "fs";

const worker = new cloudflare.WorkerScript(
  "url-shortener",
  {
    name: "url-shortener",
    content: fs.readFileSync("worker.js").toString(),
  },
);

Where's the Types?! ​

If you wanted to use TypeScript for the worker code for some guarantees around the short/long URL objects, you can definitely do so. This does require an additional step to transpile the TypeScript to JavaScript though. Remember to update the Pulumi code to read the ts extension.

typescript
// worker.ts
interface Redirect {
  to: string;
}

interface Redirects {
  [domain: string]: {
    [path: string]: Redirect;
  };
}

const redirects: Redirects = {
  "pulumi.tv": {
    "/": {
      to: "https://youtube.com/pulumitv",
    },
    "modern-infrastructure": {
        to: "https://www.youtube.com/playlist?list=PLyy8Vx2ZoWloyj3V5gXzPraiKStO2GGZw"
    }
  },
};

You'll also need a tsconfig.json:

json
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "lib": ["ES2020"],
    "module": "ES2020",
    "moduleResolution": "node",
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "outDir": "dist",
    "pretty": true,
    "sourceMap": true,
    "strict": true,
    "target": "ES2020",
    "outDir": "dist",
    "types": ["@cloudflare/workers-types"]
  },
  "include": ["index.ts"]
}

Then we can transpile the code with a local.Command resource.

We need to add our command package and then add a local command to our code.

shell
npm install @pulumi/command
typescript
// index.ts
import * as command from "@pulumi/command";

const compileWorker = new command.local.Command("compile-worker", {
  create:
    "yarn run tsc >/dev/null && cat ./dist/index.js",
  dir: "../worker",
});

You could then use the stdout output, compileWorker.stdout, from our local command resource for the content input on the WorkerScript resource, instead of fs.readFileSync.

Hooking Up a Domain ​

Cloudflare Workers allow us to connect a domain name, instead of using the generated workers.dev subdomain. To do so with Pulumi, we need to create or fetch a Cloudflare Zone.

Create a Zone(s) ​

Once we create the cloudflare.Zone resource, we also need to create a cloudflare.ZoneSettingsOverride resource to ensure that we enable:

  • alwaysUseHttps: "on"
  • automaticHttpsRewrites: "on"
  • ssl: "strict"
  • universalSsl: "on".

Without these settings, our traffic will be available over HTTP and ... come on, it's 2022 already.

typescript
// index.ts
const zone = new cloudflare.Zone("pulumi.tv", {
    zone: "pulumi.tv",
    plan: "free",
});

const zoneSettings = new cloudflare.ZoneSettingsOverride("pulumi.tv", {
    zoneId: z.id,
    settings: {
        alwaysUseHttps: "on",
        automaticHttpsRewrites: "on",
        ssl: "strict",
        minTlsVersion: "1.2",
        universalSsl: "on",
    },
});

Fetch a Zone ​

typescript
// index.ts
const zone = await cloudflare.getZone({
  name: "pulumi.tv",
});

Creating the Routes ​

In-order to actually send traffic to our workers from these zones, we need to setup the DNS records and create the routes. While Cloudflare does provide a web UI to "connect" a custom domain, in beta, this isn't yet available over the Cloudflare API.

Instead, we need to use a proxied DNS record on a subdomain, or domain apex, that points to workers.dev.

typescript
// index.ts
const record = new cloudflare.Record(
  "pulumi.tv",
  {
    zoneId: zone.id,
    name: "@",
    type: "CNAME",
    value: "workers.dev",
    proxied: true,
  },
  {
    deleteBeforeReplace: true,
  }
);

const route = new cloudflare.WorkerRoute(
  "pulumi.tv",
  {
    zoneId: zone.id,
    pattern: pulumi.interpolate`${zone.zone}/*`,
    scriptName: worker.name,
  }
);

Fin ​

Lastly, we can run pulumi up and run a curl 😀

shell
❯ pulumi up

     Type                                      Name                    Status
 +   pulumi:pulumi:Stack                       short-links-production  created
 +   ├─ cloudflare:index:Zone                  pulumi.tv               created
 +   ├─ cloudflare:index:WorkerScript          links                   created
 +   ├─ cloudflare:index:ZoneSettingsOverride  pulumi.tv               created
 +   ├─ cloudflare:index:Record                pulumi.tv               created
 +   └─ cloudflare:index:WorkerRoute           pulumi.tv               created

Resources:
    + 7 created

Duration: 27s

❯ curl -I https://pulumi.tv
HTTP/2 302
date: Wed, 29 Jun 2022 21:57:20 GMT
location: https://youtube.com/pulumitv

❯ curl -I https://pulumi.tv/modern-infrastructure
HTTP/2 302
date: Wed, 29 Jun 2022 21:57:20 GMT
location: https://www.youtube.com/playlist?list=PLyy8Vx2ZoWloyj3V5gXzPraiKStO2GGZw

That's it! Deploying your own URL Shortener with some JavaScript and Cloudflare Workers with Pulumi.

Complete Code ​

typescript
// index.ts
import * as pulumi from "@pulumi/pulumi";
import * as cloudflare from "@pulumi/cloudflare";
import * as fs from "fs";


const zone = new cloudflare.Zone("pulumi.tv", {
    "pulumi.tv",
    plan: "free",
});

const zoneSettings = new cloudflare.ZoneSettingsOverride("pulumi.tv", {
    zoneId: zone.id,
    settings: {
        alwaysUseHttps: "on",
        automaticHttpsRewrites: "on",
        ssl: "strict",
        minTlsVersion: "1.2",
        universalSsl: "on",
    },
});

const worker = new cloudflare.WorkerScript(
  "pulumi.tv",
  {
    name: "pulumi.tv",
    content: fs.readFileSync("worker.js").toString(),
  }
);

const record = new cloudflare.Record(
    "pulumi.tv",
    {
        zoneId: zone.id,
        name: "@",
        type: "CNAME",
        value: "workers.dev",
        proxied: true,
    },
    {
        deleteBeforeReplace: true,
    }
);

const workerRoute = new cloudflare.WorkerRoute(
    "pulumi.tv",
    {
        zoneId: zone.id,
        pattern: pulumi.interpolate`${zone.zone}/*`,
        scriptName: worker.name,
    }
);

Content from the official Pulumi documentation (github.com/pulumi/docs).