diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java index 69c0ac5e8f..aa8bf0a664 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java @@ -2199,9 +2199,37 @@ public class CloudFormationResourceProvisioner { } table = dynamoDbService.describeTable(tableName, region); } + + // A template that declares StreamSpecification wants a stream. Unlike the DynamoDB API, + // the CloudFormation property carries no StreamEnabled flag — declaring the block IS the + // request — so its presence alone turns the stream on. Without this the table is created + // streamless and an event source mapping polls its ARN forever. + // + // Removing the block on an update is the inverse request: the stream is reconciled off, + // or a table updated out of streaming would keep emitting records to whatever still holds + // its ARN. + JsonNode streamSpec = props != null ? props.path("StreamSpecification") : null; + if (streamSpec != null && streamSpec.isObject()) { + String viewType = streamSpec.has("StreamViewType") + ? engine.resolve(streamSpec.get("StreamViewType")) + : null; + table = dynamoDbService.enableStream(tableName, viewType, region); + } else if (table.isStreamEnabled()) { + table = dynamoDbService.disableStream(tableName, region); + } + r.setPhysicalId(tableName); r.getAttributes().put("Arn", table.getTableArn()); - r.getAttributes().put("StreamArn", table.getTableArn() + "/stream/2024-01-01T00:00:00.000"); + // Only a live stream has an ARN worth handing to Fn::GetAtt. Publishing one unconditionally + // resolved to nothing on a streamless table; publishing the retained ARN of a stream that + // has since been switched off would resolve to something no longer running. An update + // starts from the previous attributes, so the stale entry has to be removed rather than + // merely left unwritten. + if (table.isStreamEnabled() && table.getStreamArn() != null) { + r.getAttributes().put("StreamArn", table.getStreamArn()); + } else { + r.getAttributes().remove("StreamArn"); + } } // ── Lambda ──────────────────────────────────────────────────────────────── diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java index 65544d32f5..bc515bb90c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java @@ -213,6 +213,12 @@ public class DynamoDbJsonHandler { table.setKmsMasterKeyArn(defaultKmsMasterKeyArn(region)); } + // Everything above mutates the table AFTER createTable stored it, and a persistent + // backend serializes on write rather than holding the live object. Flush once so the + // stream, billing, class, tag and SSE settings survive a restart -- stream state + // especially, since startup rebuilds streams from the persisted table. + dynamoDbService.persistTable(tableName, table, region); + ObjectNode response = objectMapper.createObjectNode(); response.set("TableDescription", tableToNode(table)); return Response.ok(response).build(); @@ -1277,6 +1283,11 @@ public class DynamoDbJsonHandler { } } + // Same flush as CreateTable: the stream mutations above land after updateTable's write, + // so without this a retargeted view type is rebuilt from the stale persisted value on + // restart and records resume the old image shape. + dynamoDbService.persistTable(tableName, table, region); + if (!addRegions.isEmpty() || !removeRegions.isEmpty() || !updateRegions.isEmpty()) { table = dynamoDbService.applyReplicaUpdates(tableName, addRegions, removeRegions, updateRegions, region); } diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java index 3a6c36cc4b..3bd27955d8 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java @@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.dynamodb.model.ExportSummary; import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex; import io.github.hectorvent.floci.services.dynamodb.model.LocalSecondaryIndex; import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement; +import io.github.hectorvent.floci.services.dynamodb.model.StreamDescription; import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition; import io.github.hectorvent.floci.services.dynamodb.model.ConditionalCheckFailedException; import io.github.hectorvent.floci.services.s3.S3Service; @@ -58,6 +59,17 @@ public class DynamoDbService { private static final Logger LOG = Logger.getLogger(DynamoDbService.class); + /** + * View type applied when a stream is requested without an explicit StreamViewType. + * + *
Not an AWS default: the CloudFormation schema marks StreamViewType required inside
+ * StreamSpecification, and the DynamoDB API documents no default either. This mirrors the
+ * lenient handling {@code DynamoDbJsonHandler} already applies on CreateTable/UpdateTable
+ * ({@code path("StreamViewType").asText("NEW_AND_OLD_IMAGES")}), so an under-specified
+ * template gets a working stream instead of a rejection, and every entry point agrees.
+ */
+ private static final String DEFAULT_STREAM_VIEW_TYPE = "NEW_AND_OLD_IMAGES";
+
private final StorageBackend Callers that reach DynamoDB through the service rather than the JSON handler — notably
+ * CloudFormation provisioning — need this: the handler enables the stream inline on
+ * CreateTable/UpdateTable, and without an equivalent entry point a table created by any other
+ * path is left streamless. A null {@code viewType} falls back to
+ * {@link #DEFAULT_STREAM_VIEW_TYPE}, matching the leniency the JSON handler already applies
+ * rather than any documented AWS default.
+ *
+ * @return the updated table, or the unchanged table when no stream service is wired.
+ */
+ public TableDefinition enableStream(String tableName, String viewType, String region) {
+ TableDefinition table = describeTable(tableName, region);
+ if (streamService == null) {
+ return table;
+ }
+ String effectiveViewType = (viewType == null || viewType.isBlank())
+ ? DEFAULT_STREAM_VIEW_TYPE
+ : viewType;
+ StreamDescription sd = streamService.enableStream(
+ table.getTableName(), table.getTableArn(), effectiveViewType, region);
+ table.setStreamEnabled(true);
+ table.setStreamArn(sd.getStreamArn());
+ table.setStreamViewType(effectiveViewType);
+ persistTable(tableName, table, region);
+ return table;
+ }
+
+ /**
+ * Turns the table's stream off and persists the result, so it stops producing records.
+ *
+ * The counterpart to {@link #enableStream}, for the same service-layer callers. The stream
+ * ARN is retained on the table, matching what UpdateTable does through the JSON handler: AWS
+ * keeps reporting {@code LatestStreamArn} for a disabled stream.
+ *
+ * @return the updated table, or the unchanged table when no stream service is wired.
+ */
+ public TableDefinition disableStream(String tableName, String region) {
+ TableDefinition table = describeTable(tableName, region);
+ if (streamService == null) {
+ return table;
+ }
+ streamService.disableStream(table.getTableName(), region);
+ table.setStreamEnabled(false);
+ persistTable(tableName, table, region);
+ return table;
+ }
+
public void deleteTable(String tableName, String region) {
String canonicalTableName = canonicalTableName(region, tableName);
String storageKey = regionKey(region, canonicalTableName);
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbStreamService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbStreamService.java
index 45794fa9fb..6739f1e74e 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbStreamService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbStreamService.java
@@ -77,6 +77,14 @@ public class DynamoDbStreamService {
String key = streamKey(region, tableName);
StreamDescription existing = streams.get(key);
if (existing != null && "ENABLED".equals(existing.getStreamStatus())) {
+ // Re-enabling a live stream with a different view type retargets it. The records this
+ // stream emits are built from the description's view type, so leaving it untouched
+ // would keep producing the old shape while the table reports the requested one.
+ if (viewType != null && !viewType.equals(existing.getStreamViewType())) {
+ existing.setStreamViewType(viewType);
+ LOG.infov("Stream view type for table {0} in region {1} is now {2}",
+ tableName, region, viewType);
+ }
return existing;
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/DynamoDbStreamSpecificationCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/DynamoDbStreamSpecificationCfnProvisionerTest.java
new file mode 100644
index 0000000000..9db11d2ed1
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/DynamoDbStreamSpecificationCfnProvisionerTest.java
@@ -0,0 +1,260 @@
+package io.github.hectorvent.floci.services.cloudformation;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
+import io.github.hectorvent.floci.services.cloudformation.provisioners.CloudFormationResourceRegistry;
+import io.github.hectorvent.floci.services.dynamodb.DynamoDbService;
+import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyList;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * A template that declares {@code StreamSpecification} on {@code AWS::DynamoDB::Table} must get a
+ * streamed table.
+ *
+ * The CloudFormation property carries no {@code StreamEnabled} flag — unlike the DynamoDB API,
+ * declaring the block IS the request — so provisioning read it as absent and created the table
+ * streamless. DescribeTable then returned no {@code LatestStreamArn} and an event source mapping
+ * registered against the expected stream ARN polled "Stream not found" forever, while the plain
+ * UpdateTable API on the same runtime enabled streams correctly.
+ */
+class DynamoDbStreamSpecificationCfnProvisionerTest {
+
+ private static final String TABLE = "stream-repro";
+ private static final String TABLE_ARN =
+ "arn:aws:dynamodb:us-east-1:000000000000:table/" + TABLE;
+ private static final String STREAM_ARN = TABLE_ARN + "/stream/2026-01-01T00:00:00.000";
+
+ private final ObjectMapper mapper = new ObjectMapper();
+ private DynamoDbService dynamoDbService;
+ private CloudFormationResourceProvisioner provisioner;
+
+ @BeforeEach
+ void setUp() {
+ dynamoDbService = mock(DynamoDbService.class);
+ provisioner = new CloudFormationResourceProvisioner(
+ null, null, null, dynamoDbService, null, null, null, null, null, null,
+ null, null, null, null, null, null, mapper, null, null, null, null, null,
+ null, null, null, null, null, null, null, null, null, null, null,
+ new CloudFormationResourceRegistry(List.of()));
+
+ TableDefinition created = new TableDefinition();
+ created.setTableName(TABLE);
+ created.setTableArn(TABLE_ARN);
+ when(dynamoDbService.createTable(anyString(), anyList(), anyList(), any(), any(),
+ anyList(), anyList(), anyString())).thenReturn(created);
+ }
+
+ /** Make provisioning take the update path: the table already exists, with a live stream. */
+ private void existingStreamedTable() {
+ when(dynamoDbService.createTable(anyString(), anyList(), anyList(), any(), any(),
+ anyList(), anyList(), anyString()))
+ .thenThrow(new AwsException("ResourceInUseException", "Table already exists", 400));
+ when(dynamoDbService.describeTable(TABLE, "us-east-1"))
+ .thenReturn(streamed("NEW_AND_OLD_IMAGES"));
+ }
+
+ /** The table the service reports once its stream is switched off (ARN retained, as AWS does). */
+ private TableDefinition streamDisabled() {
+ TableDefinition table = streamed("NEW_AND_OLD_IMAGES");
+ table.setStreamEnabled(false);
+ return table;
+ }
+
+ /** The table the service reports once its stream is on. */
+ private TableDefinition streamed(String viewType) {
+ TableDefinition table = new TableDefinition();
+ table.setTableName(TABLE);
+ table.setTableArn(TABLE_ARN);
+ table.setStreamEnabled(true);
+ table.setStreamArn(STREAM_ARN);
+ table.setStreamViewType(viewType);
+ return table;
+ }
+
+ private StackResource provisionTable(String properties) throws Exception {
+ return provisioner.provision("Table", "AWS::DynamoDB::Table",
+ mapper.readTree(properties), engine(), "us-east-1", "000000000000", "my-stack");
+ }
+
+ /**
+ * An UpdateStack re-provisions with the resource's PREVIOUS attributes already in place, so a
+ * stale StreamArn has to be actively removed rather than merely left unwritten.
+ */
+ private StackResource reprovisionTable(String properties, Map A persistent backend serializes on write rather than holding the live table object, and both
+ * CreateTable and UpdateTable mutate the table AFTER the service has stored it. Without a flush the
+ * on-disk copy keeps the previous stream settings, and because startup rebuilds every stream from
+ * the persisted table, a restart resurrects the old view type and records resume the old image
+ * shape — with nothing in the running system reporting the stale value.
+ */
+class DynamoDbStreamViewTypeDurabilityTest {
+
+ private static final String TABLE = "durable-stream";
+ private static final String REGION = "us-east-1";
+
+ private final ObjectMapper mapper = new ObjectMapper();
+
+ /** A store backed by a real file, so a "restart" is just reading it again. */
+ private StorageBackend