As well as serving content from a storage zone, Bunny CDN can also serve from other origins. For example, S3-compatible storage solutions.
For example, this website is stored on Linode Object Storage, and served via Bunny CDN.
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
Step 1: Create a new bucket.
In the Linode dashboard, create a new storage bucket with a unique name.
Step 2: Upload your content.
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
Step 3: Set a bucket policy for your 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
Step 4: Set up bucket web hosting.
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
Note: If deploying a single-page application (SPA), then you may wish to specify the error document too by passing in --ws-error=index.html
to the above command.
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.
Security headers
To set security headers via Bunny, please refer to the Bunny hosting note.