Configuring automatic backups for Contentful
In this tutorial I will explain how to set up automatic backups for your Contentful Space(s) using an AWS Lambda Function and CloudWatch for scheduling. Contentful maintains a Command Line Interface (CLI) which offers a space export
command that lets you download a backup of your Space.
If you are interested in reading all the different flags/options that you can pass to the space export command go to the REAMDE on Github, but this is basically how it works:
It will create a backup which is a JSON file with the whole space structure (content model, entries, assets, roles & permissions and webhooks).
The CLI space export
command works great but we have bigger plans, we want to automate this process by scheduling a routine to do it for us.
Luckily there is yet another tool called the Contentful export tool. Which is a Javascript library that helps you to backup your Contentful space (as the space export
CLI) in a programmatic way.
The plan
Generate a backup of your Space. By writing a NodeJS script that can collect the export options, download the backup, save it as a JSON file locally.
Upload the backup to AWS S3. Once we have the JSON dump locally, the plan is to upload it to a S3 Bucket where we can collect the backups.
Lambda function. We will wrap our backup script in a Lambda function so we can have the advantage of trigger it in a variety of ways. For now one good option is AWS CloudWatch which can be configured to run as a Cron Job.
Execute the backup script every day. For the purpose of this tutorial, I want to schedule my script on a daily basis. You are free to adapt it to your needs.
Implementation
Let’s create a new AWS Lambda function from the AWS Console.
You will quickly notice that there are three ways to create a lambda deployment package:
Use the built in editor
Upload a .zip file
Upload a file from Amazon S3
We will use the Upload a .zip file option which means that we will have to upload a zip file that contains our function .js
file and the node_modules
required for our function.
Create a package.json
file with the below dependencies:
package.json
Create another file called index.js
This file will contain our main function, for now we are only downloading our backup file to our local tmp
folder:
index.js
Note that we are using Environment variables on the script, you can set these ENV
variables on the AWS Lambda function page.
We still need to upload the dump file to a S3 bucket, so go ahead to the AWS Console, create a new bucket (or pick an existing one).
Once you have your bucket in place, we will need to set up the communication between our lambda function and the S3 bucket. In order to do that we have two options …
Use your API credentials in the lambda function to talk to S3.
Or set up a new IAM Role.
I will lean towards the IAM Role option just to keep things clean and simple on our function. So again from the AWS Console, go to the Security, Identity, & Compliance section and choose IAM.
Create the new IAM role and make sure to attach the AmazonS3 policy to it.
One last step to make it work, link the new IAM Role to your lambda execution option:
Awesome! We just allowed our lambda function to talk directly with our S3 bucket. Let’s write the uploader part then, this is how the final function looks like:
Scheduling the Contentful backup function
Let’s pick a trigger for our lambda function, especifically a CloudWatch Event.
Go ahead, create the trigger and schedule it as you wish, I used the daily option:
Schedule expression: rate(1 day)
Super, make sure to compress your deployment package (index.js
and node_modules
), upload the zip file to the lambda’s function code section and test it!
This is how the final solution looks like on the AWS lambda page.
I hope this function helps you to have a continuous backup for your space.
You can try this and other Contentful functionality with our community edition for free.