# Replicating to Amazon S3 Using Litestream

*September 29, 2024*
 — by Flaviu Vlaicu

> Replicating to Amazon S3 Using Litestream


# 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](/images/2024-09/amazon-user-details.jpg)

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](/images/2024-09/amazon-set-permissions.jpg)

A more restrictive policy would be to only allow the following

![Amazon Set Policy](/images/2024-09/amazon-set-policy.jpg)

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](/images/2024-09/amazon-access-key.jpg)

![Amazon Success](/images/2024-09/amazon-success.jpg)

### 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](/images/2024-09/amazon-create-bucket.jpg)

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:

```bash {class="my-class" id="my-codeblock" lineNos=inline tabWidth=2}
export AWS_ACCESS_KEY_ID=AKIAxxxxxxxxxxxxxxxx
export 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.

```bash {class="my-class" id="my-codeblock" lineNos=inline tabWidth=2}
litestream 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.

```bash {class="my-class" id="my-codeblock" lineNos=inline tabWidth=2}
litestream restore -o my.db s3://BUCKETNAME/PATHNAME
```










---
*Source: [https://vlaicu.io/posts/replicating-to-amazon-s3-using-litestream/](https://vlaicu.io/posts/replicating-to-amazon-s3-using-litestream/)*
