AWS Amplify Webhook Requests in Directus

How I added AWS Amplify build triggers to my content management system, Directus

This website, rahtz.net, uses a CI/CD workflow to trigger an automated site build any time code changes are pushed to GitHub. This is great for structural and functional changes to the site, but what about when content gets updated? As described in my article about site speed, content management is completely decoupled from this site’s hosting on AWS Amplify, so there’s no intrinsic method to rebuild the site when content updates are made. Pushing code changes to trigger a site build for new content was not going to be ideal.

In order to close this gap, I took advantage of two features: one from Amplify and one from Directus, the CMS for this website. In Amplify, I first had to set up two incoming webhooks–one for the development site and one for production.

A screenshot of AWS Amplify settings to create a new webhook. It shows the webhook name and the git branch associated with the build.

AWS Amplify > Hosting > Build Settings > Incoming Webhooks

Sending a POST request to these webhooks will trigger the build and release process in Amplify, outside of pushing any code changes in git. Next, I had to configure a way to send requests to the hooks from my CMS. 

In Directus, I used a feature called Flows, which “enables custom, event-driven data processing and task automation within Directus. Each flow is composed of one trigger, followed by a series of operations.” I didn’t want to trigger a site build anytime I updated an article, page, or any other collection. I wanted something more intentional and specific. So I created new singleton collections in Directus (development/production) and tied the webhooks to those. The build collections have only three fields: id, date_updated, and build_note, which acts as a sort of commit message when updating the collection.

A screenshot from Directus showing a new singleton collection with three fields

Creating a new singleton data model

I then created a Flow for both the production site build and development site build. The trigger for the production Flow is an update action on the Trigger Site Build collection. And the operation it performs is a Webhook/Request URL using the webhook address I created in AWS. 

A Directus flow trigger and webhook request operation

At first, the request was not triggering the Amplify build as intended, though I knew the Flow had been activated. Looking at the sample cURL request provided by Amplify, I could see that there was a Content-Type defined in the header of the request.

Terminal window
curl -X POST -d {} "https://webhooks.amplify.us-west-2.amazonaws.com/prod/webhooks?id=XxXxXxXxXxXxXx&token=XxXxXxXxXxXxXx&operation=startbuild" -H "Content-Type:application/json"

cURL example from AWS Amplify

The default webhook request operation in Directus contains no header information, so I had to add a Content-Type header.

A Directus screenshot showing webhook operation settings including a Content-Type header element

Added Content-Type to Headers

Screenshot of Directus showing the config screen for a Content-Type header setting with the value application/json

Specifying the application/json value

Now, anytime I want to relaunch the site with new content, I head over to my Trigger Site Build collection, enter a new message in my build note, then save. This save/update event triggers the flow, which sends a request to Amplify, which builds and releases a new version of the site.

A Directus screenshot showing the update page for the Trigger Site Build collection
A screenshot of AWS Amplify in the middle of a build and deployment process

AWS Amplify build process in progress

Interested in fast, secure, accessible websites?

Get in touch.

*all fields required