fix(dynamodb): honor MICROSECOND ApproximateCreationDateTimePrecision for Kinesis destinations and streams false enablement (#3874)

* fix(dynamodb): honor MICROSECOND ApproximateCreationDateTimePrecision for Kinesis destinations

Store the precision requested in EnableKinesisStreamingConfiguration, echo it in the
enable response, and return it from DescribeKinesisStreamingDestination, so Terraform
no longer destroys and recreates the destination on every apply.
Stamp ApproximateCreationDateTime in epoch microseconds for MICROSECOND destinations,
with a shared eventID per change across destinations.
Reject precisions other than MILLISECOND and MICROSECOND with ValidationException.
Add forwarder unit and end-to-end integration coverage, and document the behavior.

* fix(dynamodb): stop enabling DynamoDB Streams with a Kinesis destination

EnableKinesisStreamingDestination turned on DynamoDB Streams for the table.
Kinesis forwarding does not need it, and Terraform saw stream_enabled drift
on aws_dynamodb_table on every run.
Leave the table's stream setting as the caller configured it.

* test(dynamodb): add Terraform compat test for Kinesis destination precision

Apply a Kinesis streaming destination with MICROSECOND precision and check that
it reads back, that a second plan is clean, and that a precision change
replaces the destination once and then settles.
This commit is contained in:
Attila Kis authored and GitHub committed 2026-09-19 15:55:44 -04:00
1 parent b10b81d253
commit 5d23a70cc0
9 files changed
+467 -34

No files matched your search

@@ -474,7 +474,7 @@ class DynamoDbKinesisStreamingIntegrationTest {
@Test
@Order(40)
void enableAutoEnablesStreamsIfDisabled() {
void enableLeavesDynamoDbStreamsDisabled() {
given()
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
.contentType(DYNAMODB_CONTENT_TYPE)
@@ -511,6 +511,177 @@ class DynamoDbKinesisStreamingIntegrationTest {
.post("/")
.then()
.statusCode(200)
.body("Table.StreamSpecification.StreamEnabled", equalTo(true));
.body("Table.StreamSpecification.StreamEnabled", not(equalTo(true)))
.body("Table.LatestStreamArn", nullValue());
}
@Test
@Order(50)
void enableWithInvalidPrecisionFails() {
given()
.header("X-Amz-Target", "DynamoDB_20120810.EnableKinesisStreamingDestination")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("{\"TableName\": \"StreamingTable\", \"StreamArn\": \"" + kinesisStreamArn + "\","
+ " \"EnableKinesisStreamingConfiguration\": {\"ApproximateCreationDateTimePrecision\": \"NANOSECOND\"}}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ValidationException"));
}
@Test
@Order(51)
void microsecondPrecisionIsStoredReportedAndApplied() throws Exception {
given()
.header("X-Amz-Target", "Kinesis_20131202.CreateStream")
.contentType(KINESIS_CONTENT_TYPE)
.body("""
{"StreamName": "ddb-streaming-micro", "ShardCount": 1}
""")
.when()
.post("/")
.then()
.statusCode(200);
String microStreamArn = given()
.header("X-Amz-Target", "Kinesis_20131202.DescribeStreamSummary")
.contentType(KINESIS_CONTENT_TYPE)
.body("""
{"StreamName": "ddb-streaming-micro"}
""")
.when()
.post("/")
.then()
.statusCode(200)
.extract().jsonPath().getString("StreamDescriptionSummary.StreamARN");
given()
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "MicroTable",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"BillingMode": "PAY_PER_REQUEST"
}
""")
.when()
.post("/")
.then()
.statusCode(200);
given()
.header("X-Amz-Target", "DynamoDB_20120810.EnableKinesisStreamingDestination")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("{\"TableName\": \"MicroTable\", \"StreamArn\": \"" + microStreamArn + "\","
+ " \"EnableKinesisStreamingConfiguration\": {\"ApproximateCreationDateTimePrecision\": \"MICROSECOND\"}}")
.when()
.post("/")
.then()
.statusCode(200)
.body("DestinationStatus", equalTo("ACTIVE"))
.body("EnableKinesisStreamingConfiguration.ApproximateCreationDateTimePrecision", equalTo("MICROSECOND"));
given()
.header("X-Amz-Target", "DynamoDB_20120810.DescribeKinesisStreamingDestination")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{"TableName": "MicroTable"}
""")
.when()
.post("/")
.then()
.statusCode(200)
.body("KinesisDataStreamDestinations[0].ApproximateCreationDateTimePrecision", equalTo("MICROSECOND"));
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{"TableName": "MicroTable", "Item": {"pk": {"S": "m1"}}}
""")
.when()
.post("/")
.then()
.statusCode(200);
awaitCdcDelivery();
String shardIterator = given()
.header("X-Amz-Target", "Kinesis_20131202.GetShardIterator")
.contentType(KINESIS_CONTENT_TYPE)
.body("""
{"StreamName": "ddb-streaming-micro", "ShardId": "shardId-000000000000", "ShardIteratorType": "TRIM_HORIZON"}
""")
.when()
.post("/")
.then()
.statusCode(200)
.extract().jsonPath().getString("ShardIterator");
Response recordsResponse = given()
.header("X-Amz-Target", "Kinesis_20131202.GetRecords")
.contentType(KINESIS_CONTENT_TYPE)
.body("{\"ShardIterator\": \"" + shardIterator + "\", \"Limit\": 10}")
.when()
.post("/");
recordsResponse.then().statusCode(200);
String decoded = new String(Base64.getDecoder().decode(recordsResponse.jsonPath().getString("Records[0].Data")));
JsonNode dynamodb = new ObjectMapper().readTree(decoded).get("dynamodb");
assertEquals("MICROSECOND", dynamodb.get("ApproximateCreationDateTimePrecision").asText());
long timestamp = dynamodb.get("ApproximateCreationDateTime").asLong();
long nowMicros = System.currentTimeMillis() * 1_000L;
assertTrue(timestamp > nowMicros - 60_000_000L && timestamp <= nowMicros + 5_000_000L,
"ApproximateCreationDateTime should be in microseconds (recent), got: " + timestamp);
}
@Test
@Order(52)
void reEnableReplacesTheStoredPrecision() {
String microStreamArn = given()
.header("X-Amz-Target", "Kinesis_20131202.DescribeStreamSummary")
.contentType(KINESIS_CONTENT_TYPE)
.body("""
{"StreamName": "ddb-streaming-micro"}
""")
.when()
.post("/")
.then()
.statusCode(200)
.extract().jsonPath().getString("StreamDescriptionSummary.StreamARN");
given()
.header("X-Amz-Target", "DynamoDB_20120810.DisableKinesisStreamingDestination")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("{\"TableName\": \"MicroTable\", \"StreamArn\": \"" + microStreamArn + "\"}")
.when()
.post("/")
.then()
.statusCode(200);
// No configuration on re-enable means the AWS default, not the previously stored MICROSECOND.
given()
.header("X-Amz-Target", "DynamoDB_20120810.EnableKinesisStreamingDestination")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("{\"TableName\": \"MicroTable\", \"StreamArn\": \"" + microStreamArn + "\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("EnableKinesisStreamingConfiguration.ApproximateCreationDateTimePrecision", equalTo("MILLISECOND"));
given()
.header("X-Amz-Target", "DynamoDB_20120810.DescribeKinesisStreamingDestination")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{"TableName": "MicroTable"}
""")
.when()
.post("/")
.then()
.statusCode(200)
.body("KinesisDataStreamDestinations.size()", equalTo(1))
.body("KinesisDataStreamDestinations[0].DestinationStatus", equalTo("ACTIVE"))
.body("KinesisDataStreamDestinations[0].ApproximateCreationDateTimePrecision", equalTo("MILLISECOND"));
}
}
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.dynamodb;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
@@ -12,6 +13,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
@@ -184,6 +186,44 @@ class KinesisStreamingForwarderTest {
assertTrue(forwarder.forwardingStats().isEmpty());
}
// ──────────────────────────── ApproximateCreationDateTimePrecision ────────────────────────────
@Test
void stampsEachDestinationAtItsConfiguredPrecision() throws Exception {
String microArn = "arn:aws:kinesis:us-east-1:000000000000:stream/micro-stream";
TableDefinition table = tableWithDestination(STREAM_ARN);
table.getKinesisStreamingDestinations().add(new KinesisStreamingDestination(microArn,
KinesisStreamingDestination.PRECISION_MICROSECOND));
Map<String, byte[]> sent = new HashMap<>();
when(kinesisService.putRecordForAccount(anyString(), anyString(), any(byte[].class), anyString(), anyString()))
.thenAnswer(inv -> {
sent.put(inv.getArgument(1), inv.getArgument(2));
return "seq-1";
});
forwarder.forward("INSERT", null, item("k1"), table, "us-east-1", ACCOUNT);
scheduler.runUntilQuiet();
JsonNode milli = objectMapper.readTree(sent.get("test-stream"));
JsonNode micro = objectMapper.readTree(sent.get("micro-stream"));
assertEquals("MILLISECOND", milli.path("dynamodb").path("ApproximateCreationDateTimePrecision").asText());
assertEquals("MICROSECOND", micro.path("dynamodb").path("ApproximateCreationDateTimePrecision").asText());
long millis = milli.path("dynamodb").path("ApproximateCreationDateTime").asLong();
long micros = micro.path("dynamodb").path("ApproximateCreationDateTime").asLong();
assertEquals(millis, micros / 1_000L, "both stamps describe the same instant");
assertEquals(milli.path("eventID").asText(), micro.path("eventID").asText(),
"one change is one event across destinations");
}
@Test
void microsecondStampKeepsSubMillisecondResolution() {
Instant t = Instant.ofEpochSecond(1_789_736_088L, 383_456_789L);
assertEquals(1_789_736_088_383L,
KinesisStreamingForwarder.approximateCreationDateTime(t, "MILLISECOND"));
assertEquals(1_789_736_088_383_456L,
KinesisStreamingForwarder.approximateCreationDateTime(t, "MICROSECOND"));
}
// ──────────────────────────── happy-path drain ────────────────────────────
@Test