fix(cloudformation): honor DynamoDB on-demand billing mode (#7931)

This commit is contained in:
Rashen Anupama authored and GitHub committed 2026-11-10 17:06:48 -04:00
1 parent 77cb014f9d
commit b3de4b3179
11 files changed
+52 -9

No files matched your search

@@ -230,10 +230,13 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner {
}
Scope scope = new Scope(ctx.accountId(), region);
boolean onDemand = "PAY_PER_REQUEST".equals(ctx.resolveOptional(props, "BillingMode"));
TableDefinition table;
boolean created = false;
try {
table = dynamoDb.tables().createTable(scope, tableName, keySchema, attrDefs, null, null, gsis, lsis);
table = onDemand
? dynamoDb.tables().createOnDemandTable(scope, tableName, keySchema, attrDefs, gsis, lsis)
: dynamoDb.tables().createTable(scope, tableName, keySchema, attrDefs, null, null, gsis, lsis);
created = true;
} catch (AwsException e) {
if (!"ResourceInUseException".equals(e.getErrorCode())) {
@@ -635,7 +635,11 @@ public class DynamoDbService {
TableDefinition table = new TableDefinition(tableName, keySchema, attributeDefinitions,
region, regionResolver.getAccountId());
if (readCapacity != null && writeCapacity != null) {
if ("PAY_PER_REQUEST".equals(billingMode)) {
table.setBillingMode(billingMode);
table.getProvisionedThroughput().setReadCapacityUnits(0L);
table.getProvisionedThroughput().setWriteCapacityUnits(0L);
} else if (readCapacity != null && writeCapacity != null) {
table.getProvisionedThroughput().setReadCapacityUnits(readCapacity);
table.getProvisionedThroughput().setWriteCapacityUnits(writeCapacity);
}
@@ -144,11 +144,7 @@ public class NativeDynamoDbTableService {
table.setDeletionProtectionEnabled(Boolean.TRUE.equals(settings.deletionProtectionEnabled()));
if ("PAY_PER_REQUEST".equals(billingMode)) {
table.setBillingMode("PAY_PER_REQUEST");
table.getProvisionedThroughput().setReadCapacityUnits(0L);
table.getProvisionedThroughput().setWriteCapacityUnits(0L);
} else {
if (!"PAY_PER_REQUEST".equals(billingMode)) {
table.setBillingMode("PROVISIONED");
}
@@ -411,6 +411,15 @@ public class DynamoDbLocalBackend implements DynamoDbBackend {
return table(call(scope, "CreateTable", body).path("TableDescription"));
}
@Override
public TableDefinition createOnDemandTable(Scope scope, String tableName, List<KeySchemaElement> keySchema,
List<AttributeDefinition> attributeDefinitions,
List<GlobalSecondaryIndex> globalSecondaryIndexes,
List<LocalSecondaryIndex> localSecondaryIndexes) {
return createTable(scope, tableName, keySchema, attributeDefinitions, null, null, globalSecondaryIndexes,
localSecondaryIndexes);
}
private ObjectNode index(ArrayNode indexes, String indexName, List<KeySchemaElement> keySchema,
String projectionType, List<String> nonKeyAttributes) {
ObjectNode index = indexes.addObject().put("IndexName", indexName);
@@ -24,6 +24,11 @@ public interface DynamoDbTableAccess {
Long writeCapacity, List<GlobalSecondaryIndex> globalSecondaryIndexes,
List<LocalSecondaryIndex> localSecondaryIndexes);
TableDefinition createOnDemandTable(Scope scope, String tableName, List<KeySchemaElement> keySchema,
List<AttributeDefinition> attributeDefinitions,
List<GlobalSecondaryIndex> globalSecondaryIndexes,
List<LocalSecondaryIndex> localSecondaryIndexes);
TableDefinition describeTable(Scope scope, String tableName);
Optional<TableDefinition> findTable(Scope scope, String tableName);
@@ -171,6 +171,16 @@ public class NativeDynamoDbBackend implements DynamoDbBackend {
scope.region()));
}
@Override
public TableDefinition createOnDemandTable(Scope scope, String tableName, List<KeySchemaElement> keySchema,
List<AttributeDefinition> attributeDefinitions,
List<GlobalSecondaryIndex> globalSecondaryIndexes,
List<LocalSecondaryIndex> localSecondaryIndexes) {
return RequestScopes.callAs(scope.accountId(), () -> dynamoDbService.createTable(tableName, keySchema,
attributeDefinitions, null, null, globalSecondaryIndexes, localSecondaryIndexes,
List.of(), "PAY_PER_REQUEST", scope.region()));
}
@Override
public TableDefinition describeTable(Scope scope, String tableName) {
return RequestScopes.callAs(scope.accountId(), () -> dynamoDbService.describeTable(tableName, scope.region()));
@@ -83,6 +83,9 @@ class CloudFormationDynamoDbGlobalTableIntegrationTest {
assertStackStatus(stackName, "CREATE_COMPLETE");
JsonPath described = describeTable(tableName).jsonPath();
assertEquals("PAY_PER_REQUEST", described.getString("Table.BillingModeSummary.BillingMode"));
assertEquals(0L, described.getLong("Table.ProvisionedThroughput.ReadCapacityUnits"));
assertEquals(0L, described.getLong("Table.ProvisionedThroughput.WriteCapacityUnits"));
String tableArn = described.getString("Table.TableArn");
String tableId = described.getString("Table.TableId");
assertTrue(tableArn != null && tableArn.startsWith("arn:aws:dynamodb:"), "TableArn: " + tableArn);
@@ -93,6 +93,11 @@ class CloudFormationDynamoDbTimeToLiveIntegrationTest {
cloudFormation(stack, "CreateStack", TEMPLATE.formatted(table, "dev", TTL_ENABLED, ""));
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status());
dynamoDb("DescribeTable", "{\"TableName\": \"" + table + "\"}")
.statusCode(200)
.body("Table.BillingModeSummary.BillingMode", equalTo("PAY_PER_REQUEST"))
.body("Table.ProvisionedThroughput.ReadCapacityUnits", equalTo(0))
.body("Table.ProvisionedThroughput.WriteCapacityUnits", equalTo(0));
describeTimeToLive(table)
.body("TimeToLiveDescription.TimeToLiveStatus", equalTo("ENABLED"))
.body("TimeToLiveDescription.AttributeName", equalTo("expiresAt"));
@@ -58,6 +58,8 @@ class DynamoDbStreamSpecificationCfnProvisionerTest {
created.setTableArn(TABLE_ARN);
when(dynamoDbService.createTable(anyString(), anyList(), anyList(), any(), any(),
anyList(), anyList(), anyString())).thenReturn(created);
when(dynamoDbService.createTable(anyString(), anyList(), anyList(), any(), any(),
anyList(), anyList(), anyList(), anyString(), anyString())).thenReturn(created);
}
/** Make provisioning take the update path: the table already exists, with a live stream. */
@@ -91,6 +91,14 @@ public final class RecordingDynamoDbBackend implements DynamoDbItemAccess, Dynam
return record("createTable", scope);
}
@Override
public TableDefinition createOnDemandTable(Scope scope, String tableName, List<KeySchemaElement> keySchema,
List<AttributeDefinition> attributeDefinitions,
List<GlobalSecondaryIndex> globalSecondaryIndexes,
List<LocalSecondaryIndex> localSecondaryIndexes) {
return record("createOnDemandTable", scope);
}
@Override
public TableDefinition describeTable(Scope scope, String tableName) {
return record("describeTable", scope);
@@ -157,7 +157,6 @@ AWS::Cognito::UserPoolClient AuthSessionValidity accepted and ignored
AWS::Cognito::UserPoolClient EnablePropagateAdditionalUserContextData accepted and ignored
AWS::Cognito::UserPoolDomain Routing accepted and ignored, as the CognitoCfnProvisioner javadoc records
AWS::Config::ConfigRule Compliance accepted and ignored
AWS::DynamoDB::GlobalTable BillingMode accepted and ignored
AWS::DynamoDB::GlobalTable GlobalTableSourceArn accepted and ignored
AWS::DynamoDB::GlobalTable GlobalTableWitnesses accepted and ignored
AWS::DynamoDB::GlobalTable MultiRegionConsistency accepted and ignored
@@ -169,7 +168,6 @@ AWS::DynamoDB::GlobalTable VectorIndexes accepted and ignored
AWS::DynamoDB::GlobalTable WarmThroughput accepted and ignored
AWS::DynamoDB::GlobalTable WriteOnDemandThroughputSettings accepted and ignored
AWS::DynamoDB::GlobalTable WriteProvisionedThroughputSettings accepted and ignored
AWS::DynamoDB::Table BillingMode accepted and ignored
AWS::DynamoDB::Table ContributorInsightsSpecification accepted and ignored
AWS::DynamoDB::Table DeletionProtectionEnabled accepted and ignored
AWS::DynamoDB::Table ImportSourceSpecification accepted and ignored
1 AWS::ApiGateway::ApiKey StageKeys accepted and ignored, as the ApiGatewayApiKeyCfnProvisioner javadoc records
157 AWS::Cognito::UserPoolClient EnablePropagateAdditionalUserContextData accepted and ignored
158 AWS::Cognito::UserPoolDomain Routing accepted and ignored, as the CognitoCfnProvisioner javadoc records
159 AWS::Config::ConfigRule Compliance accepted and ignored
AWS::DynamoDB::GlobalTable BillingMode accepted and ignored
160 AWS::DynamoDB::GlobalTable GlobalTableSourceArn accepted and ignored
161 AWS::DynamoDB::GlobalTable GlobalTableWitnesses accepted and ignored
162 AWS::DynamoDB::GlobalTable MultiRegionConsistency accepted and ignored
168 AWS::DynamoDB::GlobalTable WarmThroughput accepted and ignored
169 AWS::DynamoDB::GlobalTable WriteOnDemandThroughputSettings accepted and ignored
170 AWS::DynamoDB::GlobalTable WriteProvisionedThroughputSettings accepted and ignored
AWS::DynamoDB::Table BillingMode accepted and ignored
171 AWS::DynamoDB::Table ContributorInsightsSpecification accepted and ignored
172 AWS::DynamoDB::Table DeletionProtectionEnabled accepted and ignored
173 AWS::DynamoDB::Table ImportSourceSpecification accepted and ignored