💾 Archived View for wilw.capsule.town › notes › bunny-hosting-s3.gmi captured on 2024-05-26 at 14:57:26. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-04-26)

-=-=-=-=-=-=-

🏡 Home

Back to notes

Hosting S3 content via Bunny CDN

Last updated on 23 April 2023

As well as serving content from a storage zone [1], Bunny CDN [2] can also serve from other origins. For example, S3-compatible storage solutions.

1

2

For example, this website is stored on Linode Object Storage [3], and served via Bunny CDN.

3

In this note I describe how to achieve this. The note uses Linode as an object storage provider, but any S3-compatible storage provider should work the same.

Phase 1: Create and configure the bucket

In the Linode dashboard, create a new storage bucket with a unique name.

Upload your built web assets to the bucket. I recommend using `s3cmd` (which can be installed on a Mac using `brew install s3cmd`). Once installed, run `s3cmd --configure` to set up a config file.

Now, to upload a directory to your bucket, you can run:

s3cmd sync --no-mime-magic --guess-mime-type path/to/content/* s3://BUCKET

Although Bunny can be configured to authenticate requests to the S3 bucket (origin), I find it far easier to just make the bucket public (as long as you don't need to store anything sensitive). To do so, a bucket policy like this can be created:

{
 "Statement": [
  {
    "Effect": "Allow",
    "Principal": {
      "AWS": [
        "*"
      ]
    },
    "Action": [
      "s3:GetObject"
    ],
    "Resource": [
      "arn:aws:s3::BUCKET*"
    ]
  }
 ]
}

And then applied with:

s3cmd setpolicy bucket-policy.json s3://BUCKET

The S3 host can be configured more appropriately as a website by setting-up a default index and error page.

For example, to setup a default index file (to `index.html`) use:

s3cmd ws-create --ws-index=index.html s3://BUCKET

And that's it for the bucket setup.

Phase 2: Setup the Bunny pull zone

In the Bunny CDN dashboard, create a new pull zone. When configuring the origin, use the URL to your bucket's website endpoint.

E.g. for a bucket on Linode's `eu-central-1` region you can use `http://BUCKET.website-eu-central-1.linodeobjects.com`.

Finally, set-up the DNS for your pull zone as needed.

Deploy script

Here's an example deploy script (which I currently use for this website) for uploading web content to the bucket and clearing the Bunny CDN cache.

#!/bin/bash

rm -rf public 2>&1

echo "Building site..."
hugo -D
echo "Uploading site..."
s3cmd -c ~/.s3personal sync --no-mime-magic --guess-mime-type public/* s3://BUCKET
echo "Clearing CDN cache..."
curl -X POST -H "AccessKey: $BUNNY_PERSONAL" https://api.bunny.net/pullzone/ID/purgeCache
echo "Done."

For information on the `-c` flag, refer to this note [4].

4

Security headers

To set security headers via Bunny, please refer to the Bunny hosting [5] note.

5

Back to notes