> The index of all pages is at https://docs.afffast.com/llms.txt.

# Log drain to AWS S3

Write the logs of your workspace to an S3 bucket, or to an S3-compatible store, as gzip NDJSON objects.

The type `s3` of a [log drain](https://docs.afffast.com/docs/log-drains.md) writes the records to an S3 bucket. It also works with S3-compatible stores such as Cloudflare R2 and MinIO. You need an API key with the scope `log_drains:write`.

## Make the log drain

```bash
curl https://api.afffast.com/v1/log_drains \
  -H "Authorization: Bearer sk_live_..." \
  -H "Content-Type: application/json" \
  -d '{
    "type": "s3",
    "label": "Archive",
    "config": {
      "bucket": "my-logs",
      "region": "eu-west-1",
      "prefix": "afffast"
    },
    "credentials": {
      "access_key_id": "AKIA...",
      "secret_access_key": "..."
    },
    "sources": ["api_request", "sync_run"]
  }'
```

| Field                           | Meaning                                                                                                                          |
| ------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `config.bucket`                 | The name of the bucket.                                                                                                          |
| `config.region`                 | The region of the bucket, for example `eu-west-1`. Use `auto` for Cloudflare R2.                                                 |
| `config.prefix`                 | Optional. The folder in the bucket.                                                                                              |
| `config.endpoint`               | Optional. The `https` URL of an S3-compatible store. Leave it empty for AWS. It must not point to a private or internal address. |
| `config.path_style`             | Optional. `true` uses `https://host/bucket/key`. MinIO and some other stores need it.                                            |
| `credentials.access_key_id`     | The ID of the access key. The API never returns it.                                                                              |
| `credentials.secret_access_key` | The secret of the access key. The API never returns it.                                                                          |

`POST /v1/log_drains/{id}/test` writes one small object under `<prefix>/_afffast_test/`.

## The IAM policy

The access key needs one permission: `s3:PutObject` on the prefix. It needs no read, list or delete permission. Use this policy and change the bucket and the prefix:

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "s3:PutObject",
      "Resource": "arn:aws:s3:::my-logs/afffast/*"
    }
  ]
}
```

Without a prefix, use `arn:aws:s3:::my-logs/*`.

## The objects

Each batch is one object. The key is:

```text
<prefix>/<source>/YYYY/MM/DD/HH/<batch-id>.ndjson.gz
```

- `<source>` is `api_request`, `delivery`, `sync_run` or `click`. An object has the records of one source.
- `YYYY/MM/DD/HH` is the UTC time of the first record of the batch.
- `<batch-id>` is a hash of the IDs of the records. A retry of a batch writes the **same key**, so it replaces the same object. It makes no second object.
- The object is gzip data (`Content-Encoding: gzip`, `Content-Type: application/x-ndjson`). The text is NDJSON: one record in each line.

A batch has 5000 records or 5 MB of NDJSON at most. The API checks the log drain about every 30 seconds. A source with new records writes one object (or more, for a big backlog) at each check. A source with a low volume writes up to 2 small objects each minute. Use an S3 lifecycle rule to join or expire the objects.

## Errors

| Answer of S3                                                  | What the API does                                                    |
| ------------------------------------------------------------- | -------------------------------------------------------------------- |
| `AccessDenied`, `InvalidAccessKeyId`, `SignatureDoesNotMatch` | The API stops the log drain at once (`failing`) and says the reason. |
| `NoSuchBucket`, wrong region or endpoint                      | The API stops the log drain at once (`failing`) and says the reason. |
| `SlowDown`, 5xx, timeout, network error                       | The API tries again with a growing wait.                             |

Delivery is **at least once**. A batch that is sent again has the same key, so the object stays the same.
