Hosting files on Fastly *sort of*


#1

Fastly doesn’t provide file hosting services, but here’s a fun way to serve files on Fastly without needing a backend.

I make small in-browser applications sometimes and I need a place to put them. I don’t actually need a backend and I don’t want to go through the trouble of hardening, maintaining or even paying for a server.

You can actually host static files on fastly and even mix them in with a dynamic application with Edge Dictionaries.
This is not too hard to do actually.

This is done through combining synthetics and Dictionaries.

On a service that you want to host your files you need to create a dictionary with this api. I mostly name mine pages_dictionary.

After creating the dictionary you need to add two sections of custom vcl to get this to work.
First in vcl_recv you need to lookup the pages and be ready to return a synthetic.

    if ( table.lookup(pages_dictonary, req.url ) {
      error 900;
    }

Then in vcl_error you need to lookup and return the correct page in the dictionary.

  if (obj.status == 900 ) {
    set obj.status = 200;
    set obj.response = "OK";
    synthetic table.lookup(pages_dictonary, req.url );
    return(deliver);
  }

This simple block will find the page in your dictionary and return it.

With this setup you can add URLs to your service via the create dictionary item api.
And you can update those pages with the update dictionary item api.

Note the first forwardslash of the URL needs to be replaces the %2f or the Fastly api will not update the contents of the page.

After setting this up you can create a list of URLs and there content and server them all from the edge without any service updates. Also this can be easily integration with CI or build scripts.

Cheers,
Jack