fix(cloudformation): refuse a provisioned DynamoDB table without capacity (#9126)

* fix(cloudformation): refuse a provisioned DynamoDB table without capacity
* chore(cloudformation): share the on-demand createTable test matcher
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-20 23:15:25 -04:00
1 parent a87e8e47e0
commit d22ea96ecf
9 files changed
+225 -80

No files matched your search

@@ -282,6 +282,10 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner {
throw new AwsException("InvalidRequest",
"Property ProvisionedThroughput can't be used with PAY_PER_REQUEST BillingMode.", 400);
}
if (plainTable && !onDemand && (tableThroughput.capacity() == null
|| indexThroughput.values().stream().anyMatch(spec -> spec.capacity() == null))) {
throw new AwsException("InvalidRequest", "Property ProvisionedThroughput cannot be empty", 400);
}
boolean throughputDeclared = isDeclared(tableThroughput) || indexThroughput.values().stream()
.anyMatch(DynamoDbCfnProvisioner::isDeclared);
TableDefinition table;
@@ -286,6 +286,42 @@ class CloudFormationDynamoDbThroughputIntegrationTest {
CfnStackWaits.deleteStack(stack, CFN_AUTH);
}
@Test
void aCreateWithoutBillingModeAndCapacityFailsTheCreate() {
String stack = "cfn-ddb-tp-nocap-" + suffix();
String table = "tp-nocap-" + suffix();
cloudFormation(stack, "CreateStack", template(table, "", ""));
assertEquals("ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status());
assertTrue(CfnStackWaits.failedResourceReason(stack, CFN_AUTH)
.contains("Property ProvisionedThroughput cannot be empty"));
describe(table).statusCode(400).body("__type", containsString("ResourceNotFoundException"));
CfnStackWaits.deleteStack(stack, CFN_AUTH);
}
@Test
void anUpdateThatRemovesTheCapacityFailsAndKeepsTheOldCapacity() {
String stack = "cfn-ddb-tp-remove-" + suffix();
String table = "tp-remove-" + suffix();
cloudFormation(stack, "CreateStack", template(table, capacity(2, 2), capacity(1, 1)));
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status());
cloudFormation(stack, "UpdateStack", template(table, capacity(2, 2), ""));
assertEquals("UPDATE_ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status());
assertTrue(CfnStackWaits.failedResourceReason(stack, CFN_AUTH)
.contains("Property ProvisionedThroughput cannot be empty"));
describe(table)
.body("Table.ProvisionedThroughput.ReadCapacityUnits", equalTo(1))
.body("Table.ProvisionedThroughput.WriteCapacityUnits", equalTo(1))
.body("Table.GlobalSecondaryIndexes[0].ProvisionedThroughput.ReadCapacityUnits", equalTo(2));
CfnStackWaits.deleteStack(stack, CFN_AUTH);
}
private static String suffix() {
return Long.toString(System.nanoTime(), 36);
}
@@ -2004,7 +2004,8 @@ class CloudFormationIntegrationTest {
],
"Projection": {"ProjectionType": "KEYS_ONLY"}
}
]
],
"BillingMode": "PAY_PER_REQUEST"
}
}
}
@@ -2868,7 +2869,8 @@ class CloudFormationIntegrationTest {
],
"KeySchema": [
{"AttributeName": "pk", "KeyType": "HASH"}
]
],
"BillingMode": "PAY_PER_REQUEST"
}
},
"TableNameParam": {
@@ -3041,7 +3043,8 @@ class CloudFormationIntegrationTest {
],
"KeySchema": [
{"AttributeName": "id", "KeyType": "HASH"}
]
],
"BillingMode": "PAY_PER_REQUEST"
}
}
}
@@ -3230,7 +3233,8 @@ class CloudFormationIntegrationTest {
],
"KeySchema": [
{"AttributeName": "id", "KeyType": "HASH"}
]
],
"BillingMode": "PAY_PER_REQUEST"
}
},
"TableB": {
@@ -3241,7 +3245,8 @@ class CloudFormationIntegrationTest {
],
"KeySchema": [
{"AttributeName": "id", "KeyType": "HASH"}
]
],
"BillingMode": "PAY_PER_REQUEST"
}
}
}
@@ -3354,7 +3359,8 @@ class CloudFormationIntegrationTest {
],
"KeySchema": [
{"AttributeName": "pk", "KeyType": "HASH"}
]
],
"BillingMode": "PAY_PER_REQUEST"
}
},
"ArnParam": {
@@ -4579,7 +4585,8 @@ class CloudFormationIntegrationTest {
],
"KeySchema": [
{"AttributeName": "pk", "KeyType": "HASH"}
]
],
"BillingMode": "PAY_PER_REQUEST"
}
}
}
@@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test;
import java.util.Map;
import java.util.function.Function;
import static io.floci.aws.services.cloudformation.provisioners.DynamoDbCreateCalls.onDemandCreate;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -64,8 +65,7 @@ class DynamoDbStreamSpecificationCfnProvisionerTest {
/** 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()))
when(onDemandCreate(dynamoDbService))
.thenThrow(new AwsException("ResourceInUseException", "Table already exists", 400));
when(dynamoDbService.describeTable(TABLE, "us-east-1"))
.thenReturn(streamed("NEW_AND_OLD_IMAGES"));
@@ -134,7 +134,7 @@ class DynamoDbStreamSpecificationCfnProvisionerTest {
.thenReturn(streamed("KEYS_ONLY"));
provisionTable("""
{"TableName":"stream-repro",
{"TableName":"stream-repro","BillingMode":"PAY_PER_REQUEST",
"KeySchema":[{"AttributeName":"id","KeyType":"HASH"}],
"AttributeDefinitions":[{"AttributeName":"id","AttributeType":"S"}],
"StreamSpecification":{"StreamViewType":"KEYS_ONLY"}}
@@ -154,7 +154,7 @@ class DynamoDbStreamSpecificationCfnProvisionerTest {
.thenReturn(streamed("NEW_AND_OLD_IMAGES"));
provisionTable("""
{"TableName":"stream-repro",
{"TableName":"stream-repro","BillingMode":"PAY_PER_REQUEST",
"KeySchema":[{"AttributeName":"id","KeyType":"HASH"}],
"AttributeDefinitions":[{"AttributeName":"id","AttributeType":"S"}],
"StreamSpecification":{}}
@@ -168,7 +168,7 @@ class DynamoDbStreamSpecificationCfnProvisionerTest {
@Test
void tableWithoutStreamSpecificationGetsNoStreamAndNoStreamArnAttribute() throws Exception {
StackResource resource = provisionTable("""
{"TableName":"stream-repro",
{"TableName":"stream-repro","BillingMode":"PAY_PER_REQUEST",
"KeySchema":[{"AttributeName":"id","KeyType":"HASH"}],
"AttributeDefinitions":[{"AttributeName":"id","AttributeType":"S"}]}
""");
@@ -188,7 +188,7 @@ class DynamoDbStreamSpecificationCfnProvisionerTest {
when(dynamoDbService.disableStream(TABLE, "us-east-1")).thenReturn(streamDisabled());
StackResource resource = reprovisionTable("""
{"TableName":"stream-repro",
{"TableName":"stream-repro","BillingMode":"PAY_PER_REQUEST",
"KeySchema":[{"AttributeName":"id","KeyType":"HASH"}],
"AttributeDefinitions":[{"AttributeName":"id","AttributeType":"S"}]}
""", Map.of("Arn", TABLE_ARN, "StreamArn", STREAM_ARN));
@@ -208,7 +208,7 @@ class DynamoDbStreamSpecificationCfnProvisionerTest {
.thenReturn(streamed("NEW_AND_OLD_IMAGES"));
StackResource resource = provisionTable("""
{"TableName":"stream-repro",
{"TableName":"stream-repro","BillingMode":"PAY_PER_REQUEST",
"KeySchema":[{"AttributeName":"id","KeyType":"HASH"}],
"AttributeDefinitions":[{"AttributeName":"id","AttributeType":"S"}],
"StreamSpecification":{"StreamViewType":"NEW_AND_OLD_IMAGES"}}
@@ -222,7 +222,7 @@ class DynamoDbStreamSpecificationCfnProvisionerTest {
@Test
void streamlessTableIsNotDisabledAgain() throws Exception {
provisionTable("""
{"TableName":"stream-repro",
{"TableName":"stream-repro","BillingMode":"PAY_PER_REQUEST",
"KeySchema":[{"AttributeName":"id","KeyType":"HASH"}],
"AttributeDefinitions":[{"AttributeName":"id","AttributeType":"S"}]}
""");
@@ -241,7 +241,7 @@ class DynamoDbStreamSpecificationCfnProvisionerTest {
.thenReturn(streamed("KEYS_ONLY"));
StackResource resource = reprovisionTable("""
{"TableName":"stream-repro",
{"TableName":"stream-repro","BillingMode":"PAY_PER_REQUEST",
"KeySchema":[{"AttributeName":"id","KeyType":"HASH"}],
"AttributeDefinitions":[{"AttributeName":"id","AttributeType":"S"}],
"StreamSpecification":{"StreamViewType":"KEYS_ONLY"}}
@@ -18,6 +18,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import static io.floci.aws.services.cloudformation.provisioners.DynamoDbCreateCalls.onDemandCreate;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -50,7 +51,8 @@ class DynamoDbCfnProvisionerTest {
private static final String TABLE_ARN = "arn:aws:dynamodb:us-east-1:000000000000:table/" + TABLE_NAME;
private static final String TABLE_ID = "2d4c5c3a-1b6e-4a1d-9f0a-7b1c2d3e4f50";
private static final String STREAM_ARN = TABLE_ARN + "/stream/2026-01-01T00:00:00.000";
private static final String TTL_PROPS = "{\"TableName\":\"orders\",\"TimeToLiveSpecification\":"
private static final String TTL_PROPS = "{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"TimeToLiveSpecification\":"
+ "{\"AttributeName\":\"expiresAt\",\"Enabled\":true}}";
private final DynamoDbService dynamoDb = mock(DynamoDbService.class);
@@ -117,11 +119,11 @@ class DynamoDbCfnProvisionerTest {
/** The registry schema declares only Arn and StreamArn read-only on a plain table; TableId belongs to the global table. */
@Test
void tablePublishesOnlyArnWithoutAStream() throws Exception {
when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))
when(onDemandCreate(dynamoDb))
.thenReturn(table(false));
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
provisioner.provision(r, props("{\"TableName\":\"orders\"}"), ctx());
provisioner.provision(r, props("{\"TableName\":\"orders\",\"BillingMode\":\"PAY_PER_REQUEST\"}"), ctx());
assertEquals(TABLE_NAME, r.getPhysicalId());
assertEquals(Set.of("Arn"), r.getAttributes().keySet());
@@ -194,12 +196,13 @@ class DynamoDbCfnProvisionerTest {
@Test
void streamedTablePublishesStreamArn() throws Exception {
when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))
when(onDemandCreate(dynamoDb))
.thenReturn(table(false));
when(dynamoDb.enableStream(TABLE_NAME, "NEW_AND_OLD_IMAGES", "us-east-1")).thenReturn(table(true));
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
provisioner.provision(r, props("{\"TableName\":\"orders\",\"StreamSpecification\":"
provisioner.provision(r, props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"StreamSpecification\":"
+ "{\"StreamViewType\":\"NEW_AND_OLD_IMAGES\"}}"), ctx());
assertEquals(Set.of("Arn", "StreamArn"), r.getAttributes().keySet());
@@ -315,28 +318,28 @@ class DynamoDbCfnProvisionerTest {
String priorName = "my-stack-Orders-0123456789ab";
TableDefinition existing = table(false);
existing.setTableName(priorName);
when(dynamoDb.createTable(eq(priorName), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))
when(onDemandCreate(dynamoDb, priorName, "us-east-1"))
.thenThrow(new AwsException("ResourceInUseException", "Table already exists", 400));
when(dynamoDb.describeTable(priorName, "us-east-1")).thenReturn(existing);
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
r.setPhysicalId(priorName);
provisioner.provision(r, props("{}"), updateCtx(priorName));
provisioner.provision(r, props("{\"BillingMode\":\"PAY_PER_REQUEST\"}"), updateCtx(priorName));
assertEquals(priorName, r.getPhysicalId());
verify(dynamoDb).createTable(eq(priorName), anyList(), anyList(), any(), any(), anyList(), anyList(),
eq("us-east-1"));
onDemandCreate(verify(dynamoDb), priorName, "us-east-1");
verify(dynamoDb, atLeastOnce()).describeTable(priorName, "us-east-1");
}
@Test
void tagsAreAppliedAndStaleOnesRemoved() throws Exception {
when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))
when(onDemandCreate(dynamoDb))
.thenReturn(table(false));
when(dynamoDb.listTagsOfResource(TABLE_ARN, "us-east-1")).thenReturn(Map.of("old", "1", "env", "dev"));
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
provisioner.provision(r, props("{\"TableName\":\"orders\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}]}"),
provisioner.provision(r, props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}]}"),
ctx());
verify(dynamoDb).untagResource(TABLE_ARN, List.of("old"), "us-east-1");
@@ -351,19 +354,20 @@ class DynamoDbCfnProvisionerTest {
/** The update path: the table already exists and reports {@code current}. */
private void existingTable(TableDefinition current) {
when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))
when(onDemandCreate(dynamoDb))
.thenThrow(new AwsException("ResourceInUseException", "Table already exists", 400));
when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(current);
}
@Test
void createWithTimeToLiveEnablesItOnce() throws Exception {
when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))
when(onDemandCreate(dynamoDb))
.thenReturn(table(false));
when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(table(false));
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
provisioner.provision(r, props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":"
provisioner.provision(r, props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"TimeToLiveSpecification\":"
+ "{\"AttributeName\":\"expiresAt\",\"Enabled\":true}}"), ctx());
verify(dynamoDb).updateTimeToLive(TABLE_NAME, "expiresAt", true, "us-east-1");
@@ -371,7 +375,7 @@ class DynamoDbCfnProvisionerTest {
@Test
void enabledGivenAsAStringIsResolvedThroughTheEngine() throws Exception {
when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))
when(onDemandCreate(dynamoDb))
.thenReturn(table(false));
when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(table(false));
ProvisionContext ctx = ctx();
@@ -384,7 +388,8 @@ class DynamoDbCfnProvisionerTest {
});
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
provisioner.provision(r, props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":"
provisioner.provision(r, props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"TimeToLiveSpecification\":"
+ "{\"AttributeName\":\"expiresAt\",\"Enabled\":{\"Ref\":\"TtlOn\"}}}"), ctx);
verify(dynamoDb).updateTimeToLive(TABLE_NAME, "expiresAt", true, "us-east-1");
@@ -396,7 +401,8 @@ class DynamoDbCfnProvisionerTest {
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
r.setPhysicalId(TABLE_NAME);
provisioner.provision(r, props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":"
provisioner.provision(r, props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"TimeToLiveSpecification\":"
+ "{\"AttributeName\":\"expiresAt\",\"Enabled\":true}}"), updateCtx(TABLE_NAME));
verify(dynamoDb, never()).updateTimeToLive(anyString(), any(), anyBoolean(), anyString());
@@ -408,7 +414,8 @@ class DynamoDbCfnProvisionerTest {
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
r.setPhysicalId(TABLE_NAME);
provisioner.provision(r, props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":{\"Enabled\":false}}"),
provisioner.provision(r, props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"TimeToLiveSpecification\":{\"Enabled\":false}}"),
updateCtx(TABLE_NAME));
verify(dynamoDb).updateTimeToLive(TABLE_NAME, "expiresAt", false, "us-east-1");
@@ -420,7 +427,8 @@ class DynamoDbCfnProvisionerTest {
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
r.setPhysicalId(TABLE_NAME);
provisioner.provision(r, props("{\"TableName\":\"orders\"}"), updateCtx(TABLE_NAME, TTL_PROPS));
provisioner.provision(r, props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\"}"), updateCtx(TABLE_NAME, TTL_PROPS));
verify(dynamoDb).updateTimeToLive(TABLE_NAME, "expiresAt", false, "us-east-1");
}
@@ -432,8 +440,9 @@ class DynamoDbCfnProvisionerTest {
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
r.setPhysicalId(TABLE_NAME);
provisioner.provision(r, props("{\"TableName\":\"orders\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}]}"),
updateCtx(TABLE_NAME, "{\"TableName\":\"orders\"}"));
provisioner.provision(r, props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}]}"),
updateCtx(TABLE_NAME, "{\"TableName\":\"orders\",\"BillingMode\":\"PAY_PER_REQUEST\"}"));
verify(dynamoDb, never()).updateTimeToLive(anyString(), any(), anyBoolean(), anyString());
}
@@ -441,7 +450,8 @@ class DynamoDbCfnProvisionerTest {
@Test
void timeToLiveWithoutEnabledFailsModelValidationBeforeTheTableIsCreated() throws Exception {
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
JsonNode props = props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":{\"AttributeName\":\"exp\"}}");
JsonNode props = props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"TimeToLiveSpecification\":{\"AttributeName\":\"exp\"}}");
ProvisionContext ctx = ctx();
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx));
@@ -455,7 +465,8 @@ class DynamoDbCfnProvisionerTest {
@Test
void timeToLiveWithANonBooleanEnabledFailsModelValidationBeforeTheTableIsCreated() throws Exception {
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
JsonNode props = props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":"
JsonNode props = props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"TimeToLiveSpecification\":"
+ "{\"AttributeName\":\"exp\",\"Enabled\":\"tru\"}}");
ProvisionContext ctx = ctx();
@@ -470,7 +481,8 @@ class DynamoDbCfnProvisionerTest {
@Test
void aBooleanTimeToLiveFailsTemplateValidationBeforeTheTableIsCreated() throws Exception {
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
JsonNode props = props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":false}");
JsonNode props = props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"TimeToLiveSpecification\":false}");
ProvisionContext ctx = ctx();
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx));
@@ -483,7 +495,8 @@ class DynamoDbCfnProvisionerTest {
@Test
void aListTimeToLiveFailsTemplateValidationBeforeTheTableIsCreated() throws Exception {
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
JsonNode props = props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":[]}");
JsonNode props = props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"TimeToLiveSpecification\":[]}");
ProvisionContext ctx = ctx();
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx));
@@ -498,7 +511,8 @@ class DynamoDbCfnProvisionerTest {
existingTable(withTimeToLive(table(false), "expiresAt"));
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
r.setPhysicalId(TABLE_NAME);
JsonNode props = props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":\"off\"}");
JsonNode props = props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"TimeToLiveSpecification\":\"off\"}");
ProvisionContext ctx = updateCtx(TABLE_NAME, TTL_PROPS);
assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx));
@@ -514,7 +528,8 @@ class DynamoDbCfnProvisionerTest {
r.setPhysicalId(TABLE_NAME);
// Left by an earlier successful update; it must not make the engine skip its own restore.
r.getAttributes().put(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR, "{}");
JsonNode props = props("{\"TableName\":\"orders\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}],"
JsonNode props = props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}],"
+ "\"TimeToLiveSpecification\":{\"AttributeName\":\"deleteAfter\",\"Enabled\":true}}");
ProvisionContext ctx = updateCtx(TABLE_NAME);
@@ -546,7 +561,8 @@ class DynamoDbCfnProvisionerTest {
r.setPhysicalId(TABLE_NAME);
r.getAttributes().put("Arn", TABLE_ARN);
r.getAttributes().put("StreamArn", STREAM_ARN);
JsonNode props = props("{\"TableName\":\"orders\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}],"
JsonNode props = props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}],"
+ "\"TimeToLiveSpecification\":{\"AttributeName\":\"expiresAt\",\"Enabled\":true}}");
ProvisionContext ctx = updateCtx(TABLE_NAME);
@@ -569,7 +585,7 @@ class DynamoDbCfnProvisionerTest {
}
private void createdTableWhoseTimeToLiveCallFails() {
when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))
when(onDemandCreate(dynamoDb))
.thenReturn(table(false));
when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(table(false));
doThrow(new AwsException("LimitExceededException", "boom", 400))
@@ -684,7 +700,7 @@ class DynamoDbCfnProvisionerTest {
@Test
void successfulCreateLeavesNoOwnershipMarker() throws Exception {
when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))
when(onDemandCreate(dynamoDb))
.thenReturn(table(false));
when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(table(false));
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
@@ -698,7 +714,8 @@ class DynamoDbCfnProvisionerTest {
@Test
void enabledTimeToLiveWithoutAnAttributeNameFailsBeforeTheTableIsCreated() throws Exception {
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
JsonNode props = props("{\"TableName\":\"orders\",\"TimeToLiveSpecification\":{\"Enabled\":true}}");
JsonNode props = props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"TimeToLiveSpecification\":{\"Enabled\":true}}");
ProvisionContext ctx = ctx();
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx));
@@ -718,7 +735,8 @@ class DynamoDbCfnProvisionerTest {
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
r.setPhysicalId(TABLE_NAME);
r.getAttributes().put("StreamArn", STREAM_ARN);
provisioner.provision(r, props("{\"TableName\":\"orders\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}],"
provisioner.provision(r, props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}],"
+ "\"TimeToLiveSpecification\":{\"AttributeName\":\"expiresAt\",\"Enabled\":true}}"),
updateCtx(TABLE_NAME));
// The table as the update left it: retagged, streamless and expiring.
@@ -741,7 +759,8 @@ class DynamoDbCfnProvisionerTest {
existingTable(table(false));
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
r.setPhysicalId(TABLE_NAME);
provisioner.provision(r, props("{\"TableName\":\"orders\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}]}"),
provisioner.provision(r, props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}]}"),
updateCtx(TABLE_NAME));
when(dynamoDb.listTagsOfResource(TABLE_ARN, "us-east-1")).thenReturn(Map.of("env", "prod"));
doThrow(new AwsException("InternalServerError", "boom", 500))
@@ -758,14 +777,15 @@ class DynamoDbCfnProvisionerTest {
*/
@Test
void replacingUpdateDropsAStaleSnapshotAndReportsNoRollback() throws Exception {
when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))
when(onDemandCreate(dynamoDb))
.thenReturn(table(false));
when(dynamoDb.describeTable(TABLE_NAME, "us-east-1")).thenReturn(table(false));
StackResource r = resource("AWS::DynamoDB::Table", "Orders");
r.setPhysicalId("orders-v1");
r.getAttributes().put(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR, "{}");
provisioner.provision(r, props("{\"TableName\":\"orders\"}"), updateCtx("orders-v1"));
provisioner.provision(r, props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\"}"), updateCtx("orders-v1"));
assertFalse(r.getAttributes().containsKey(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR));
assertFalse(provisioner.rollbackUpdate(r));
@@ -0,0 +1,31 @@
package io.floci.aws.services.cloudformation.provisioners;
import io.floci.aws.services.dynamodb.DynamoDbService;
import io.floci.aws.services.dynamodb.model.TableDefinition;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
/** The {@code createTable} call the provisioner makes for a {@code PAY_PER_REQUEST} table. */
public final class DynamoDbCreateCalls {
private DynamoDbCreateCalls() {
}
/**
* Any table in any region. Pass the mock to stub it with {@code when(...)}, or
* {@code verify(mock, ...)} to check it.
*/
public static TableDefinition onDemandCreate(DynamoDbService dynamoDb) {
return dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(),
anyList(), eq("PAY_PER_REQUEST"), anyString());
}
/** The named table in the given region. */
public static TableDefinition onDemandCreate(DynamoDbService dynamoDb, String tableName, String region) {
return dynamoDb.createTable(eq(tableName), anyList(), anyList(), any(), any(), anyList(), anyList(),
anyList(), eq("PAY_PER_REQUEST"), eq(region));
}
}
@@ -20,13 +20,12 @@ import org.junit.jupiter.params.provider.ValueSource;
import java.util.HashMap;
import java.util.List;
import static io.floci.aws.services.cloudformation.provisioners.DynamoDbCreateCalls.onDemandCreate;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -49,13 +48,16 @@ class DynamoDbKinesisStreamSpecificationCfnProvisionerTest {
private static final String OTHER_STREAM_ARN = "arn:aws:kinesis:us-east-1:000000000000:stream/orders-audit";
private static final String MILLISECOND = KinesisStreamingDestination.PRECISION_MILLISECOND;
private static final String MICROSECOND = KinesisStreamingDestination.PRECISION_MICROSECOND;
private static final String DECLARED = "{\"TableName\":\"orders\",\"KinesisStreamSpecification\":"
private static final String DECLARED = "{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"KinesisStreamSpecification\":"
+ "{\"StreamArn\":\"" + STREAM_ARN + "\"}}";
private static final String DECLARED_MICROSECOND = "{\"TableName\":\"orders\",\"KinesisStreamSpecification\":"
private static final String DECLARED_MICROSECOND = "{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"KinesisStreamSpecification\":"
+ "{\"StreamArn\":\"" + STREAM_ARN + "\",\"ApproximateCreationDateTimePrecision\":\"MICROSECOND\"}}";
private static final String DECLARED_OTHER = "{\"TableName\":\"orders\",\"KinesisStreamSpecification\":"
private static final String DECLARED_OTHER = "{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"KinesisStreamSpecification\":"
+ "{\"StreamArn\":\"" + OTHER_STREAM_ARN + "\"}}";
private static final String UNDECLARED = "{\"TableName\":\"orders\"}";
private static final String UNDECLARED = "{\"TableName\":\"orders\",\"BillingMode\":\"PAY_PER_REQUEST\"}";
private final DynamoDbService dynamoDb = mock(DynamoDbService.class);
private final NativeDynamoDbTableService tableService = mock(NativeDynamoDbTableService.class);
@@ -113,13 +115,13 @@ class DynamoDbKinesisStreamSpecificationCfnProvisionerTest {
}
private void newTable() {
when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))
when(onDemandCreate(dynamoDb))
.thenReturn(table());
when(dynamoDb.describeTable(TABLE_NAME, REGION)).thenReturn(table());
}
private void existingTable(TableDefinition current) {
when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))
when(onDemandCreate(dynamoDb))
.thenThrow(new AwsException("ResourceInUseException", "Table already exists", 400));
when(dynamoDb.describeTable(TABLE_NAME, REGION)).thenReturn(current);
}
@@ -154,28 +156,28 @@ class DynamoDbKinesisStreamSpecificationCfnProvisionerTest {
@Test
void blockWithoutAStreamArnFailsModelValidationBeforeTheTableIsCreated() throws Exception {
StackResource r = resource();
JsonNode props = props("{\"TableName\":\"orders\",\"KinesisStreamSpecification\":{}}");
JsonNode props = props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"KinesisStreamSpecification\":{}}");
ProvisionContext ctx = ctx();
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx));
assertEquals("InvalidRequest", e.getErrorCode());
verify(dynamoDb, never()).createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(),
anyString());
onDemandCreate(verify(dynamoDb, never()));
}
@Test
void unknownPrecisionFailsModelValidationBeforeTheTableIsCreated() throws Exception {
StackResource r = resource();
JsonNode props = props("{\"TableName\":\"orders\",\"KinesisStreamSpecification\":"
JsonNode props = props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"KinesisStreamSpecification\":"
+ "{\"StreamArn\":\"" + STREAM_ARN + "\",\"ApproximateCreationDateTimePrecision\":\"SECOND\"}}");
ProvisionContext ctx = ctx();
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx));
assertEquals("InvalidRequest", e.getErrorCode());
verify(dynamoDb, never()).createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(),
anyString());
onDemandCreate(verify(dynamoDb, never()));
}
@ParameterizedTest
@@ -183,7 +185,8 @@ class DynamoDbKinesisStreamSpecificationCfnProvisionerTest {
void blockThatIsNotAnObjectFailsTemplateValidationBeforeTheTableIsCreated(String value, String found)
throws Exception {
StackResource r = resource();
JsonNode props = props("{\"TableName\":\"orders\",\"KinesisStreamSpecification\":" + value + "}");
JsonNode props = props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"KinesisStreamSpecification\":" + value + "}");
ProvisionContext ctx = ctx();
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx));
@@ -198,7 +201,8 @@ class DynamoDbKinesisStreamSpecificationCfnProvisionerTest {
void updateWithABlockThatIsNotAnObjectLeavesTheDeclaredDestinationActive() throws Exception {
existingTable(table(active(STREAM_ARN, MILLISECOND)));
StackResource r = existingResource();
JsonNode props = props("{\"TableName\":\"orders\",\"KinesisStreamSpecification\":\"off\"}");
JsonNode props = props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"KinesisStreamSpecification\":\"off\"}");
ProvisionContext ctx = updateCtx(DECLARED);
assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx));
@@ -232,7 +236,8 @@ class DynamoDbKinesisStreamSpecificationCfnProvisionerTest {
void createWithAStreamNameInsteadOfItsArnFailsAsNotStabilizedAndDeletesTheTable() throws Exception {
newTable();
StackResource r = resource();
JsonNode props = props("{\"TableName\":\"orders\",\"KinesisStreamSpecification\":"
JsonNode props = props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"KinesisStreamSpecification\":"
+ "{\"StreamArn\":\"orders-events-stream-name-long-enough-1\"}}");
ProvisionContext ctx = ctx();
@@ -253,7 +258,8 @@ class DynamoDbKinesisStreamSpecificationCfnProvisionerTest {
throws Exception {
newTable();
StackResource r = resource();
JsonNode props = props("{\"TableName\":\"orders\",\"KinesisStreamSpecification\":"
JsonNode props = props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"KinesisStreamSpecification\":"
+ "{\"StreamArn\":\"" + streamArn + "\"}}");
ProvisionContext ctx = ctx();
@@ -280,7 +286,8 @@ class DynamoDbKinesisStreamSpecificationCfnProvisionerTest {
void updateDeclaringAStreamArnTheTableAlreadyForwardsToStillChecksIt() throws Exception {
existingTable(table(active("events", MILLISECOND)));
StackResource r = existingResource();
JsonNode props = props("{\"TableName\":\"orders\",\"KinesisStreamSpecification\":{\"StreamArn\":\"events\"}}");
JsonNode props = props("{\"TableName\":\"orders\","
+ "\"BillingMode\":\"PAY_PER_REQUEST\",\"KinesisStreamSpecification\":{\"StreamArn\":\"events\"}}");
ProvisionContext ctx = updateCtx(UNDECLARED);
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx));
@@ -36,6 +36,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
/**
@@ -82,12 +83,17 @@ class DynamoDbThroughputCfnProvisionerTest {
+ "\"ProvisionedThroughput\":{\"ReadCapacityUnits\":3,\"WriteCapacityUnits\":3},"
+ "\"WarmThroughput\":{\"ReadUnitsPerSecond\":2,\"WriteUnitsPerSecond\":2}}";
private static final String INDEX_WARM = "{\"TableName\":\"orders\","
+ "\"ProvisionedThroughput\":{\"ReadCapacityUnits\":1,\"WriteCapacityUnits\":1},"
+ "\"GlobalSecondaryIndexes\":[{\"IndexName\":\"gix1\","
+ "\"ProvisionedThroughput\":{\"ReadCapacityUnits\":3,\"WriteCapacityUnits\":3},"
+ "\"WarmThroughput\":{\"ReadUnitsPerSecond\":1,\"WriteUnitsPerSecond\":1}}]}";
private static final String INDEX_WARM_CHANGED = "{\"TableName\":\"orders\","
+ "\"ProvisionedThroughput\":{\"ReadCapacityUnits\":1,\"WriteCapacityUnits\":1},"
+ "\"GlobalSecondaryIndexes\":[{\"IndexName\":\"gix1\","
+ "\"ProvisionedThroughput\":{\"ReadCapacityUnits\":3,\"WriteCapacityUnits\":3},"
+ "\"WarmThroughput\":{\"ReadUnitsPerSecond\":2,\"WriteUnitsPerSecond\":2}}]}";
private static final String UNDECLARED = "{\"TableName\":\"orders\"}";
private static final String EMPTY_CAPACITY_MESSAGE = "Property ProvisionedThroughput cannot be empty";
private final DynamoDbService dynamoDb = mock(DynamoDbService.class);
private final NativeDynamoDbTableService tableService = mock(NativeDynamoDbTableService.class);
@@ -344,17 +350,49 @@ class DynamoDbThroughputCfnProvisionerTest {
}
@Test
void aTemplateWithoutThroughputCreatesTheTableAsBefore() throws Exception {
when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(),
anyString())).thenReturn(table(5, 5, false));
when(dynamoDb.describeTable(TABLE_NAME, REGION)).thenReturn(table(5, 5, false));
void aTemplateWithoutBillingModeAndCapacityFailsBeforeTheTableIsCreated() throws Exception {
AwsException e = assertThrows(AwsException.class,
() -> provisioner.provision(resource("AWS::DynamoDB::Table"), props(UNDECLARED), ctx()));
provisioner.provision(resource("AWS::DynamoDB::Table"), props(UNDECLARED), ctx());
assertEquals("InvalidRequest", e.getErrorCode());
assertEquals(EMPTY_CAPACITY_MESSAGE, e.getMessage());
verifyNoTableCreated();
verifyNoInteractions(dynamoDb);
}
verify(dynamoDb).createTable(eq(TABLE_NAME), anyList(), anyList(), eq(null), eq(null), anyList(),
anyList(), eq(REGION));
verify(tableService, never()).createTable(anyString(), anyList(), anyList(), anyBoolean(), any(),
anyList(), anyList(), anyString());
@Test
void provisionedBillingModeWithoutCapacityFailsBeforeTheTableIsCreated() throws Exception {
String template = "{\"TableName\":\"orders\",\"BillingMode\":\"PROVISIONED\"}";
AwsException e = assertThrows(AwsException.class,
() -> provisioner.provision(resource("AWS::DynamoDB::Table"), props(template), ctx()));
assertEquals(EMPTY_CAPACITY_MESSAGE, e.getMessage());
verifyNoTableCreated();
}
@Test
void anIndexWithoutCapacityFailsBeforeTheTableIsCreated() throws Exception {
String template = "{\"TableName\":\"orders\","
+ "\"ProvisionedThroughput\":{\"ReadCapacityUnits\":1,\"WriteCapacityUnits\":1},"
+ "\"GlobalSecondaryIndexes\":[{\"IndexName\":\"gix1\"}]}";
AwsException e = assertThrows(AwsException.class,
() -> provisioner.provision(resource("AWS::DynamoDB::Table"), props(template), ctx()));
assertEquals(EMPTY_CAPACITY_MESSAGE, e.getMessage());
verifyNoTableCreated();
}
@Test
void anUpdateThatRemovesTheCapacityFailsBeforeAnyUpdate() throws Exception {
existingTable(table(1, 2, false));
AwsException e = assertThrows(AwsException.class,
() -> provisioner.provision(existingResource(), props(UNDECLARED), updateCtx(PROVISIONED)));
assertEquals(EMPTY_CAPACITY_MESSAGE, e.getMessage());
verify(tableService, never()).updateThroughput(anyString(), any(), anyString());
}
@Test
@@ -364,6 +402,7 @@ class DynamoDbThroughputCfnProvisionerTest {
when(dynamoDb.describeTable(TABLE_NAME, REGION)).thenReturn(table(5, 5, false));
provisioner.provision(resource("AWS::DynamoDB::GlobalTable"), props(PROVISIONED), ctx());
provisioner.provision(resource("AWS::DynamoDB::GlobalTable"), props(UNDECLARED), ctx());
verify(tableService, never()).createTable(anyString(), anyList(), anyList(), anyBoolean(), any(),
anyList(), anyList(), anyString());
@@ -96,7 +96,8 @@ class DynamoDbFacadeTest {
DynamoDbCfnProvisioner provisioner = new DynamoDbCfnProvisioner(facade);
StackResource table = cfnResource("Orders", "AWS::DynamoDB::Table");
provisioner.provision(table, mapper.readTree("{\"TableName\":\"orders\"}"),
provisioner.provision(table, mapper.readTree("{\"TableName\":\"orders\",\"ProvisionedThroughput\":"
+ "{\"ReadCapacityUnits\":1,\"WriteCapacityUnits\":1}}"),
cfnContext(DEFAULT_ACCOUNT, OTHER_REGION));
provisioner.delete("AWS::DynamoDB::Table", "orders", OTHER_REGION);