Trigger builds in AWS Amplify with DynamoDB

Trigger builds in AWS Amplify with DynamoDB

I'm building a thing. I'll write more about that thing in the near future. But, in the meantime, I thought I'd write a little about one small other thing I learned along the way.

The thing I am building has two main components.

  1. A back-end workflow that uses AWS Step Functions to periodically orchestrate fetching and processing some data before storing it in an Amazon DynamoDB table.
  2. A front-end website built using Next.js that displays the data as a static website, hosted on AWS Amplify.

The front-end needs to be rebuilt whenever there is new data to display. With AWS Amplify, you can easily create a Webhook to invoke a new build. Here's how it works.

In my SAM template, I configure DynamoDB streams like this:

Then I need an AWS Lambda function to trigger off the DynamoDB stream. It's defined in my SAM template like this:

The function code itself is super simple. It just makes a POST request to the Webhook.

There is one little trick I am doing here you may have noticed. I am storing the Amplify Webhook URL in AWS Systems Manager Parameter Store and setting it as an environmental variable in my Lambda function. This way I don't have to hard-code anything into my SAM template.

AMPLIFY_WEBHOOK_URL: '{{resolve:ssm:/new-thing/amplify-webhook-url:1}}'

Now, whenever my State Machine runs, it updates DynamoDB, which triggers a Lambda, which tells Amplify to rebuild the site. Yay!