# DynamoDB

DynamoDB is the AWS-native shared driver. The same `dynamo` driver field works for [cluster state](https://almaforge.com/storage.md#cluster-state) and [audit events](https://almaforge.com/storage.md#audit-events). It is the natural fit when the Auth Service runs on AWS and you want a managed control-plane store without running PostgreSQL or etcd yourself.

| Storage concern                         | Supported |
| --------------------------------------- | --------- |
| Cluster state (`name: state`)           | yes       |
| Audit events (`name: audit-*`)          | yes       |
| Session recordings (`name: recordings`) | no        |

## TL;DR[​](#tldr "Direct link to TL;DR")

1. Pick DynamoDB when the cluster already lives on AWS and you want AWS-managed durability, point-in-time recovery, and IAM-based access control.
2. Write one `spec.dynamo` `StorageConfig` for `name: state` and separate `audit-*` resources for each audit-events table.
3. Pair DynamoDB with [S3](https://almaforge.com/storage/s3.md) for session recordings.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

Before you start, make sure you have:

* An AWS region reachable from every Auth Service host.
* An IAM role attached to each Auth host, or static AWS keys if you must use them.
* Permission to create DynamoDB tables, or pre-created tables for state and audit with the AlmaForge schema applied.
* A design that stays within DynamoDB Streams limits. A single DynamoDB state backend supports at most two Auth Service instances.

## Configuration[​](#configuration "Direct link to Configuration")

### Step 1. Pick the table layout[​](#step-1-pick-the-table-layout "Direct link to Step 1. Pick the table layout")

Use DynamoDB when the cluster already lives on AWS and you want AWS-managed durability, point-in-time recovery, and IAM-based access control. It is the cleanest match for EC2, EKS, and other AWS-native deployments.

Use a different table for cluster state and audit events. The schemas differ, and reusing the state table breaks the driver.

### Step 2. Write the state backend resource[​](#step-2-write-the-state-backend-resource "Direct link to Step 2. Write the state backend resource")

Every Auth Service instance points at the same state table:

[`state.yaml`](https://almaforge.com/examples/storage/dynamodb/state.yaml):

examples/storage/dynamodb/state.yaml

```yaml
# DynamoDB cluster-state backend with on-demand billing and backups.

apiVersion: almaforge.com/v1

kind: StorageConfig

metadata:

  name: state

spec:

  dynamo:

    region: us-east-1

    tableName: cluster-state

    billingMode: PAY_PER_REQUEST

    continuousBackups: true
```

| Field                | Required                 | Default           | Notes                                                                                 |
| -------------------- | ------------------------ | ----------------- | ------------------------------------------------------------------------------------- |
| `region`             | yes                      | none              | AWS region of the DynamoDB table.                                                     |
| `tableName`          | yes                      | none              | DynamoDB table used for cluster state. AlmaForge will create it if it does not exist. |
| `accessKey`          | no                       | none              | Static AWS access key. Prefer an IAM role instead.                                    |
| `secretKey`          | no                       | none              | Static AWS secret key. Prefer an IAM role instead.                                    |
| `billingMode`        | no                       | `PAY_PER_REQUEST` | `PAY_PER_REQUEST` or `PROVISIONED`.                                                   |
| `readCapacityUnits`  | no                       | `10`              | Used when `billingMode: PROVISIONED`.                                                 |
| `writeCapacityUnits` | no                       | `10`              | Used when `billingMode: PROVISIONED`.                                                 |
| `continuousBackups`  | no                       | `false`           | Enables DynamoDB point-in-time recovery.                                              |
| `autoScaling`        | no                       | `false`           | Enables DynamoDB auto-scaling policy management.                                      |
| `readMinCapacity`    | when `autoScaling: true` | none              | Auto-scaling lower bound for reads.                                                   |
| `readMaxCapacity`    | when `autoScaling: true` | none              | Auto-scaling upper bound for reads.                                                   |
| `readTargetValue`    | when `autoScaling: true` | none              | Auto-scaling target ratio for reads.                                                  |
| `writeMinCapacity`   | when `autoScaling: true` | none              | Auto-scaling lower bound for writes.                                                  |
| `writeMaxCapacity`   | when `autoScaling: true` | none              | Auto-scaling upper bound for writes.                                                  |
| `writeTargetValue`   | when `autoScaling: true` | none              | Auto-scaling target ratio for writes.                                                 |
| `bufferSize`         | no                       | `1024`            | Event watcher buffer capacity.                                                        |
| `pollStreamPeriod`   | no                       | `1s`              | Poll interval for the change-feed stream.                                             |
| `retryPeriod`        | no                       | `10s`             | Retry interval for backend failures.                                                  |

### Step 3. Write any audit-events resources[​](#step-3-write-any-audit-events-resources "Direct link to Step 3. Write any audit-events resources")

The same `dynamo` driver field works for audit-events instances:

[`audit.yaml`](https://almaforge.com/examples/storage/dynamodb/audit.yaml):

examples/storage/dynamodb/audit.yaml

```yaml
# DynamoDB destination for audit events.

apiVersion: almaforge.com/v1

kind: StorageConfig

metadata:

  name: audit-primary

spec:

  dynamo:

    region: us-east-1

    tableName: cluster-audit
```

You can fan out across multiple regions or accounts by adding more `audit-*` instances:

[`audit-fanout.yaml`](https://almaforge.com/examples/storage/dynamodb/audit-fanout.yaml):

examples/storage/dynamodb/audit-fanout.yaml

```yaml
# Two DynamoDB destinations that each receive every audit event.

apiVersion: almaforge.com/v1

kind: StorageConfig

metadata:

  name: audit-primary

spec:

  dynamo:

    region: us-east-1

    tableName: cluster-audit

---

apiVersion: almaforge.com/v1

kind: StorageConfig

metadata:

  name: audit-failover

spec:

  dynamo:

    region: us-west-2

    tableName: cluster-audit
```

Most production audit configs only set `region` and `tableName`. The autoscaling and capacity knobs apply primarily when the state instance runs in `PROVISIONED` mode.

Retention lives on the driver block as `auditRetentionPeriod`. DynamoDB honors it via per-item TTL.

### Step 4. Restart and verify[​](#step-4-restart-and-verify "Direct link to Step 4. Restart and verify")

Restart the Auth Service after updating the bootstrap file, then verify the active resources:

******Terminal

```bash
sudo systemctl restart almad
alma get storageconfig/state -o yaml
alma get storageconfig/audit-primary -o yaml
```

The configuration is loaded when the output names the intended state and audit tables. Sign in again and confirm that the login event appears in the audit log to check the write path.

### URI form[​](#uri-form "Direct link to URI form")

Equivalent URI strings, suitable for an environment variable or a CLI flag. The URI host is the table name. Everything else is a query parameter.

State backend:

text

```text
dynamodb://cluster-state?region=us-east-1&billing_mode=pay_per_request&continuous_backups=true
```

Audit backend with provisioned capacity and retention:

text

```text
dynamodb://cluster-events?region=us-east-1&read_capacity_units=10&write_capacity_units=20&audit_retention_period=720h
```

Canonical query keys are snake\_case. The typed JSON-tag form (`billingMode`, `readCapacityUnits`, etc.) is accepted as an alias. See [Authentication](#authentication).

## Troubleshooting[​](#troubleshooting "Direct link to Troubleshooting")

### A third Auth Service instance is lagging or getting throttled[​](#a-third-auth-service-instance-is-lagging-or-getting-throttled "Direct link to A third Auth Service instance is lagging or getting throttled")

That is the DynamoDB Streams reader limit. AWS caps a single stream shard at two simultaneous readers before it starts throttling. If you need more than two Auth instances, move the state backend to [PostgreSQL](https://almaforge.com/storage/postgresql.md).

### State and audit share one table[​](#state-and-audit-share-one-table "Direct link to State and audit share one table")

Split them. The schemas differ, and audit writes fail against the state table.

### Autoscaling settings do nothing[​](#autoscaling-settings-do-nothing "Direct link to Autoscaling settings do nothing")

`autoScaling` and the capacity targets are ignored when `billingMode: PAY_PER_REQUEST`. Switch to `PROVISIONED` if you want the autoscaling knobs to take effect.

## Operations[​](#operations "Direct link to Operations")

### Authentication[​](#authentication "Direct link to Authentication")

Use an IAM role on the Auth host whenever possible. That keeps credentials out of the bootstrap file and follows normal AWS operational practice.

Static AWS keys via `accessKey` and `secretKey` are supported but should be the exception. The driver also honors the standard AWS SDK credential provider chain: environment variables, instance profile, and `~/.aws/credentials`.

The IAM role attached to the Auth host needs DynamoDB read, write, and admin permissions on the configured tables, DynamoDB Streams permissions, and application-autoscaling permissions if `autoScaling: true`.

### Durability and limits[​](#durability-and-limits "Direct link to Durability and limits")

Set `continuousBackups: true` if you want DynamoDB point-in-time recovery.

DynamoDB item size is capped at 400 KB. Oversized audit events exceed the driver limit and cannot be stored as a single item.

Session recordings do not live in DynamoDB. Pair this driver with [S3](https://almaforge.com/storage/s3.md) for the recordings instance.

## References[​](#references "Direct link to References")

* [Storage backends overview](https://almaforge.com/storage.md).
* [High availability](https://almaforge.com/high-availability.md#recommended-production-floor), including the two-Auth-instance limit for DynamoDB state.
* [Break-glass access](https://almaforge.com/break-glass.md).
* [S3](https://almaforge.com/storage/s3.md).
* [AWS DynamoDB Streams documentation](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.html).
