Replicating to Amazon S3 Bucket

This guide will show you how to use Amazon S3 as a database replica path for Litestream. You will need an Amazon AWS account to complete this guide.

Setup

Create an IAM user

You will need to set up a user with programmatic access to work with Amazon S3. From the AWS Console , go to the IAM service. Next, click Users from the left-hand navigation and then click the Add Userbutton. Enter a name for your user and make sure to enable Programmatic Access. Then click the Next button.

Amazon User Details

On the permissions screen, click on “Attach existing policies directly”, then search for “S3” and choose AmazonS3FullAccess. You can also specify a more restrictive policy as described later in this guide

Amazon Permissions

A more restrictive policy would be to only allow the following

Amazon Set Policy

Then click the Next button twice and then click the Create user button. This will create the user and display credentials for programmatic access. You will need to save the “Access key ID” and “Secret access key” for later use in this guide.

Amazon Access Key

Amazon Success

Create a bucket

Once you have a user created, go to the S3 service in the AWS Console. Click the “Create bucket” button. You’ll need to choose a globally unique bucket name and choose a region to store the bucket data.

Amazon Create Bucket

Then click the “Create bucket” button at the bottom of the screen. Your bucket has now been created.

Command line usage

You can replicate to S3 from the command line by setting environment variables with the credentials you obtained after creating your IAM user:

1export AWS_ACCESS_KEY_ID=AKIAxxxxxxxxxxxxxxxx
2export AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Then you can specify your bucket as a replica URL on the command line. For example, you can replicate a database to your bucket with the following command. Be sure to replace /path/to/db with the local file path of your database, replace BUCKETNAME with the name of your bucket, and replace PATHNAME with the path you want to store your replica within your bucket.

1litestream replicate /path/to/db s3://BUCKETNAME/PATHNAME

You can later restore your database from S3 to a local my.db path with the following command.

1litestream restore -o my.db s3://BUCKETNAME/PATHNAME