Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/yellow-facts-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@aligent/cdk-aspects": patch
---

fix: use addPropertyDeletionOverride to properly clear CDK L2 lazy token producers for DynamoDB throughput properties at synth time
21 changes: 21 additions & 0 deletions packages/cdk-aspects/lib/defaults/dynamodb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,27 @@ describe("DynamoDbDefaultsAspect", () => {
expect(props["ProvisionedThroughput"]).toBeUndefined();
});

it("clears GSI-level ProvisionedThroughput set by CDK L2 defaults", () => {
const table = new Table(stack, "TableWithGSI", {
partitionKey: { name: "pk", type: AttributeType.STRING },
});
table.addGlobalSecondaryIndex({
indexName: "gsi1",
partitionKey: { name: "gsi1pk", type: AttributeType.STRING },
});
app.synth();

const resources = Template.fromStack(stack).findResources(
"AWS::DynamoDB::Table"
);
const props = Object.values(resources)[0].Properties;
const gsis = props["GlobalSecondaryIndexes"];
expect(gsis).toBeDefined();
gsis.forEach((gsi: Record<string, unknown>) => {
expect(gsi["ProvisionedThroughput"]).toBeUndefined();
});
});

it("applies DESTROY removal policy", () => {
makeTable(stack, "MyTable");
app.synth();
Expand Down
18 changes: 14 additions & 4 deletions packages/cdk-aspects/lib/defaults/dynamodb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RemovalPolicy, type IAspect } from "aws-cdk-lib";
import { RemovalPolicy, Stack, type IAspect } from "aws-cdk-lib";
import {
BillingMode,
CfnTable,
Expand Down Expand Up @@ -126,11 +126,21 @@ export class DynamoDbDefaultsAspect implements IAspect {

cfnTable.billingMode = cfnTable.billingMode ?? billingMode;

// Clear conflicting throughput config to prevent CloudFormation errors
// Use addPropertyDeletionOverride instead of direct assignment to bypass
// CDK L2 lazy token producers that re-resolve at synth time.
if (cfnTable.billingMode === BillingMode.PAY_PER_REQUEST) {
cfnTable.provisionedThroughput = undefined;
cfnTable.addPropertyDeletionOverride("ProvisionedThroughput");

const gsis = Stack.of(node).resolve(cfnTable.globalSecondaryIndexes);
if (Array.isArray(gsis)) {
gsis.forEach((_, i) => {
cfnTable.addPropertyDeletionOverride(
`GlobalSecondaryIndexes.${i}.ProvisionedThroughput`
);
});
}
} else if (cfnTable.billingMode === BillingMode.PROVISIONED) {
cfnTable.onDemandThroughput = undefined;
cfnTable.addPropertyDeletionOverride("OnDemandThroughput");
}

// Stamp throughput as a default, not an override: the first predicate asks
Expand Down