Servd's New Asset Platform

Posted: 16th Jun 2020

As part of our green transition (and generally trying to make things better) we've just launched an update to the Servd Assets and Helpers Plugin in the Craft Plugin Store. This update defaults to using the new assets platform but still supports projects which are using the legacy system, so no need to panic.

Why Are We Making This Change?

Our legacy assets platform ran on AWS and made use of lambda functions in order to perform image transforms on demand. We had a few issues with this:

  • We want to move away from AWS as part of our shift to using renewable energy
  • The previous solution was a big ole' lump of code which was difficult to follow and update, we weren't even using half of it
  • The functions would directly return the image data without any caching which resulted in issues when the returned image data was larger than the maximum output body size of a lambda execution (6MB)

So we wanted to move to a codebase which was a little more focused and organised and also change our backing services away from AWS lambda.

What's Our New Solution?

We've been moving several of our other background services from AWS to a mixture of Backblaze and Google Cloud. So it made sense to use the same services for our new Assets Platform.

Google Cloud runs on 100% renewables and offers Cloud Functions - a direct replacement to AWS's Lambda functions. The only significant difference is the shape of the object that's passed into your function when it is executed.

Backblaze offer robust, affordable, cloud object storage with an S3 compatible API, so it was easy for us to make the switch. Their EU servers (which we use) are also powered by 100% renewables.

As well as new infrastructure we needed to find a new solution for the codebase itself. There's been a lot of talk recently in the Craft discord channel about serverless-sharp which is written and maintained by Venveo. This is a similar solution to our previous cloud function, but conforms to the cleaner Imgix URL pattern and has a few additional features. The primary reason we liked it was that the codebase was clean and maintained.

We had a problem though! The existing codebase assumed the functions were going to be deployed to AWS Lambda functions, something we were trying to avoid, and they also return all of the image content within the response body (potential max body size issues).

So we forked the repo and made a few changes to it:

  • Rewrote some of the request parsing to make it compatible with Google Cloud Functions
  • Changed things so that the output image is sent back to object storage rather than returned directly
  • Returned the new transformed image's URL as output

We then combined this with a custom Cloudflare Worker script which:

  • Checks if the transformed images exists in object storage already and returns it directly if it does
  • If not, forward the request to the cloud function
  • Gets the new image URL from from function response and grabs that, then returns it

This ensures that each unique image transform is only ever created once and is subsequently served from either the CDN or object storage cache - because CDN caches flush more than you expect them to. This should result in a higher percentage of users receiving a lightning fast response for all of your transformed images.

How Can I Start Using It?

All Servd projects have access to the Assets Platform. All you need to do to start using it is install the Servd Assets and Helpers plugin which will use your existing Craft Assets Transforms to get to work. No custom template tags required. It'll even work with GraphQL queries which return image transform URLs directly!

P.S Check the image at the top of this page to see an example of our new image URL structure.

If you've been tripped up by image transforms using all of your CPU, or setting up a serverless image transform service yourself doesn't sound like fun, Servd offers all of that out-of-the-box, with zero config required.