# Send CloudWatch Logs to your own storage through Firehose

Configure an Amazon Data Firehose delivery stream with Send2Bucket’s Firehose drain as its HTTP destination. The drain unpacks CloudWatch Logs batches into individual log events and delivers them to S3, Google Cloud Storage, Iceberg or DuckLake.

## How it works

```text
CloudWatch Logs ──subscription filter─▶ Firehose stream ──HTTP endpoint─▶ /v1/events/firehose ─▶ your destination
VPC Flow Logs, WAF, ALB, …  ──────────▶        │
                                               └─ failed batches ─▶ S3 backup bucket
```

- Firehose authenticates with the stream's access key, which is your Send2Bucket API key, and names the Send2Bucket stream in the URL.
- A CloudWatch Logs record carries many log events; each becomes one line with its log group and stream. Other records become one line per JSON object or text line. [Delivery format](../delivery-format.md#amazon-data-firehose) has the fields.
- Firehose retries a failed request for as long as its retry duration, then writes the batch to an S3 backup bucket.

## 1. Create a stream and a key

Create an ingestion stream in the [dashboard](pathname:///app/), add a destination, and copy the stream's API key and ID.

## 2. Create the Firehose stream

Firehose needs an IAM role that can write failed batches to a backup bucket. With that role and bucket, create a Direct PUT stream:

```bash
aws firehose create-delivery-stream \
  --delivery-stream-name send2bucket \
  --delivery-stream-type DirectPut \
  --http-endpoint-destination-configuration '{
    "EndpointConfiguration": {
      "Url": "https://api.send2bucket.com/v1/events/firehose?stream=<stream_id>",
      "Name": "Send2Bucket",
      "AccessKey": "<key>"
    },
    "RequestConfiguration": {"ContentEncoding": "GZIP"},
    "BufferingHints": {"SizeInMBs": 4, "IntervalInSeconds": 60},
    "RetryOptions": {"DurationInSeconds": 3600},
    "S3BackupMode": "FailedDataOnly",
    "S3Configuration": {
      "RoleARN": "arn:aws:iam::<account_id>:role/<firehose_role>",
      "BucketARN": "arn:aws:s3:::<backup_bucket>"
    },
    "RoleARN": "arn:aws:iam::<account_id>:role/<firehose_role>"
  }'
```

- Keep `SizeInMBs` at 5 or less. Firehose base64-encodes records, which makes a request about a third larger, and Send2Bucket refuses a request over 8 MiB with `413`, which Firehose does not retry.
- Send2Bucket answers every request with the JSON acknowledgement Firehose expects. A refused request's `errorMessage` appears in the stream's error logs when CloudWatch error logging is on.

## 3. Subscribe a log group

CloudWatch Logs needs a role it can assume to put records into the stream: trust `logs.<region>.amazonaws.com` and allow `firehose:PutRecord` and `firehose:PutRecordBatch` on the stream. Then add a subscription filter; an empty pattern sends every event:

```bash
aws logs put-subscription-filter \
  --log-group-name /aws/lambda/checkout \
  --filter-name send2bucket \
  --filter-pattern "" \
  --destination-arn arn:aws:firehose:<region>:<account_id>:deliverystream/send2bucket \
  --role-arn arn:aws:iam::<account_id>:role/<logs_to_firehose_role>
```

CloudWatch Logs sends a control message when the subscription starts; Send2Bucket drops it. Other services that log to Firehose directly, such as VPC Flow Logs, AWS WAF and Application Load Balancer access logs, use the same stream; each record becomes one line per JSON object or text line.

## 4. Query the logs

[DuckDB](../querying/duckdb.md) reads the objects in place. The view reads this stream's folder only:

```sql
INSTALL httpfs; LOAD httpfs;
CREATE SECRET s2b (TYPE s3, PROVIDER credential_chain, REGION '<region>');

CREATE VIEW aws_logs AS
SELECT * FROM read_json(
  's3://<bucket>/<prefix>/s2b/stream=<stream_id>/date=*/hour=*/*.ndjson.gz',
  format = 'newline_delimited',
  hive_partitioning = true,
  columns = {v: 'INTEGER', delivery_batch_id: 'VARCHAR', batch_id: 'VARCHAR',
             received_at: 'TIMESTAMPTZ', signal: 'VARCHAR', data: 'JSON'})
WHERE signal = 'log';
```

Events per log group today:

```sql
SELECT data->>'$.logGroup' AS log_group, count(*) AS events
FROM aws_logs
WHERE date = current_date
GROUP BY ALL
ORDER BY events DESC;
```

Lambda errors, newest first. `timestamp` is the event's time in Unix milliseconds:

```sql
SELECT make_timestamp_ms((data->>'$.timestamp')::BIGINT) AS logged_at,
       data->>'$.logGroup' AS log_group,
       data->>'$.message' AS message
FROM aws_logs
WHERE data->>'$.logGroup' LIKE '/aws/lambda/%' AND data->>'$.message' LIKE '%ERROR%'
ORDER BY logged_at DESC
LIMIT 20;
```

## Notes

- **Duplicates**: Firehose retries a request that failed or timed out, and Send2Bucket stores a retried request again. Each CloudWatch Logs event has a unique `id`; deduplicate on it when it matters.
- **Billing**: usage is the records as stored, not the base64 request, so the encoding costs nothing. Firehose bills its own ingestion separately.
- **Backup bucket**: batches that still fail after the retry duration land there, not in Send2Bucket; check it after an outage or a revoked key.
