Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dynamodb): add option to skip waiting for global replication to finish #16983

Merged
merged 3 commits into from
Oct 15, 2021
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
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-dynamodb/lib/replica-handler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ export async function isCompleteHandler(event: IsCompleteRequest): Promise<IsCom
const replicas = data.Table?.Replicas ?? [];
const regionReplica = replicas.find(r => r.RegionName === event.ResourceProperties.Region);
const replicaActive = !!(regionReplica?.ReplicaStatus === 'ACTIVE');
const skipReplicationCompletedWait = event.ResourceProperties.SkipReplicationCompletedWait ?? false;

switch (event.RequestType) {
case 'Create':
case 'Update':
// Complete when replica is reported as ACTIVE
return { IsComplete: tableActive && replicaActive };
return { IsComplete: tableActive && (replicaActive || skipReplicationCompletedWait) };
case 'Delete':
// Complete when replica is gone
return { IsComplete: tableActive && regionReplica === undefined };
Expand Down
21 changes: 19 additions & 2 deletions packages/@aws-cdk/aws-dynamodb/lib/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,22 @@ export interface TableOptions extends SchemaOptions {
*/
readonly replicationTimeout?: Duration;

/**
* Indicates whether CloudFormation stack waits for replication to finish.
* If set to false, the CloudFormation resource will mark the resource as
* created and replication will be completed asynchronously. This property is
* ignored if replicationRegions property is not set.
*
* DO NOT UNSET this property if adding/removing multiple replicationRegions
* in one deployment, as CloudFormation only supports one region replication
* at a time. CDK overcomes this limitation by waiting for replication to
* finish before starting new replicationRegion.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-globaltable.html#cfn-dynamodb-globaltable-replicas
* @default true
*/
readonly waitForReplicationToFinish?: boolean;

/**
* Whether CloudWatch contributor insights is enabled.
*
Expand Down Expand Up @@ -1152,7 +1168,7 @@ export class Table extends TableBase {
}

if (props.replicationRegions && props.replicationRegions.length > 0) {
this.createReplicaTables(props.replicationRegions, props.replicationTimeout);
this.createReplicaTables(props.replicationRegions, props.replicationTimeout, props.waitForReplicationToFinish);
}
}

Expand Down Expand Up @@ -1494,7 +1510,7 @@ export class Table extends TableBase {
*
* @param regions regions where to create tables
*/
private createReplicaTables(regions: string[], timeout?: Duration) {
private createReplicaTables(regions: string[], timeout?: Duration, waitForReplicationToFinish?: boolean) {
const stack = Stack.of(this);

if (!Token.isUnresolved(stack.region) && regions.includes(stack.region)) {
Expand Down Expand Up @@ -1524,6 +1540,7 @@ export class Table extends TableBase {
properties: {
TableName: this.tableName,
Region: region,
SkipReplicationCompletedWait: waitForReplicationToFinish === undefined ? undefined : !waitForReplicationToFinish,
},
});
currentRegion.node.addDependency(
Expand Down
54 changes: 54 additions & 0 deletions packages/@aws-cdk/aws-dynamodb/test/dynamodb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2438,6 +2438,60 @@ describe('global', () => {
});
});

test('create replicas without waiting to finish replication', () => {
// GIVEN
const stack = new Stack();

// WHEN
new Table(stack, 'Table', {
partitionKey: {
name: 'id',
type: AttributeType.STRING,
},
replicationRegions: [
'eu-west-2',
'eu-central-1',
],
waitForReplicationToFinish: false,
});

// THEN
expect(stack).toHaveResource('Custom::DynamoDBReplica', {
Properties: {
TableName: {
Ref: 'TableCD117FA1',
},
Region: 'eu-west-2',
SkipReplicationCompletedWait: true,
},
Condition: 'TableStackRegionNotEqualseuwest2A03859E7',
}, ResourcePart.CompleteDefinition);

expect(stack).toHaveResource('Custom::DynamoDBReplica', {
Properties: {
TableName: {
Ref: 'TableCD117FA1',
},
Region: 'eu-central-1',
SkipReplicationCompletedWait: true,
},
Condition: 'TableStackRegionNotEqualseucentral199D46FC0',
}, ResourcePart.CompleteDefinition);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a new test here, passing waitForReplicationToFinish as false.


expect(SynthUtils.toCloudFormation(stack).Conditions).toEqual({
TableStackRegionNotEqualseuwest2A03859E7: {
'Fn::Not': [
{ 'Fn::Equals': ['eu-west-2', { Ref: 'AWS::Region' }] },
],
},
TableStackRegionNotEqualseucentral199D46FC0: {
'Fn::Not': [
{ 'Fn::Equals': ['eu-central-1', { Ref: 'AWS::Region' }] },
],
},
});
});

test('grantReadData', () => {
const stack = new Stack();
const table = new Table(stack, 'Table', {
Expand Down