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).
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.
space export CLI) in a programmatic way.
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:
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.
package.json file with the below dependencies:
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
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 …
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:
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 (
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.
I am a Technical Architect and software engineer at Contentful. I'm passionate about understanding customer needs (and constraints) and best practices for content modelling, techniques, SDKs and tools.