Skip to content

Commit

Permalink
Add DeletionPolicy and UpdateReplacePolicy
Browse files Browse the repository at this point in the history
  • Loading branch information
jogold committed Mar 22, 2019
1 parent 2c19587 commit 4920f07
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-rds/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ export interface DatabaseClusterProps {
* @default No parameter group
*/
parameterGroup?: IClusterParameterGroup;

/**
* The CloudFormation policy to apply when the cluster and its instances
* are removed from the stack or replaced during an update.
*
* @default Retain
*/
deleteReplacePolicy?: cdk.DeletionPolicy
}

/**
Expand Down Expand Up @@ -276,6 +284,10 @@ export class DatabaseCluster extends DatabaseClusterBase implements IDatabaseClu
storageEncrypted: props.kmsKey ? true : props.storageEncrypted
});

const deleteReplacePolicy = props.deleteReplacePolicy || cdk.DeletionPolicy.Retain;
cluster.options.deletionPolicy = deleteReplacePolicy;
cluster.options.updateReplacePolicy = deleteReplacePolicy;

this.clusterIdentifier = cluster.ref;
this.clusterEndpoint = new Endpoint(cluster.dbClusterEndpointAddress, cluster.dbClusterEndpointPort);
this.readerEndpoint = new Endpoint(cluster.dbClusterReadEndpointAddress, cluster.dbClusterEndpointPort);
Expand Down Expand Up @@ -314,6 +326,9 @@ export class DatabaseCluster extends DatabaseClusterBase implements IDatabaseClu
dbSubnetGroupName: subnetGroup.ref,
});

instance.options.deletionPolicy = deleteReplacePolicy;
instance.options.updateReplacePolicy = deleteReplacePolicy;

// We must have a dependency on the NAT gateway provider here to create
// things in the right order.
instance.node.addDependency(internetConnected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,9 @@
]
}
]
}
},
"DeletionPolicy": "Retain",
"UpdateReplacePolicy": "Retain"
},
"DatabaseInstance1844F58FD": {
"Type": "AWS::RDS::DBInstance",
Expand All @@ -676,7 +678,9 @@
"VPCPrivateSubnet1DefaultRouteAE1D6490",
"VPCPrivateSubnet2DefaultRouteF4F5CFD2",
"VPCPrivateSubnet3DefaultRoute27F311AE"
]
],
"DeletionPolicy": "Retain",
"UpdateReplacePolicy": "Retain"
},
"DatabaseInstance2AA380DEE": {
"Type": "AWS::RDS::DBInstance",
Expand All @@ -694,7 +698,9 @@
"VPCPrivateSubnet1DefaultRouteAE1D6490",
"VPCPrivateSubnet2DefaultRouteF4F5CFD2",
"VPCPrivateSubnet3DefaultRoute27F311AE"
]
],
"DeletionPolicy": "Retain",
"UpdateReplacePolicy": "Retain"
},
"DatabaseRotationSecurityGroup17736B63": {
"Type": "AWS::EC2::SecurityGroup",
Expand Down
14 changes: 10 additions & 4 deletions packages/@aws-cdk/aws-rds/test/integ.cluster.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,9 @@
]
}
]
}
},
"DeletionPolicy": "Retain",
"UpdateReplacePolicy": "Retain"
},
"DatabaseInstance1844F58FD": {
"Type": "AWS::RDS::DBInstance",
Expand All @@ -502,7 +504,9 @@
"DependsOn": [
"VPCPublicSubnet1DefaultRoute91CEF279",
"VPCPublicSubnet2DefaultRouteB7481BBA"
]
],
"DeletionPolicy": "Retain",
"UpdateReplacePolicy": "Retain"
},
"DatabaseInstance2AA380DEE": {
"Type": "AWS::RDS::DBInstance",
Expand All @@ -520,7 +524,9 @@
"DependsOn": [
"VPCPublicSubnet1DefaultRoute91CEF279",
"VPCPublicSubnet2DefaultRouteB7481BBA"
]
],
"DeletionPolicy": "Retain",
"UpdateReplacePolicy": "Retain"
}
}
}
}
23 changes: 16 additions & 7 deletions packages/@aws-cdk/aws-rds/test/test.cluster.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, haveResource } from '@aws-cdk/assert';
import { expect, haveResource, ResourcePart } from '@aws-cdk/assert';
import ec2 = require('@aws-cdk/aws-ec2');
import kms = require('@aws-cdk/aws-kms');
import cdk = require('@aws-cdk/cdk');
Expand Down Expand Up @@ -26,12 +26,21 @@ export = {

// THEN
expect(stack).to(haveResource('AWS::RDS::DBCluster', {
Engine: "aurora",
DBSubnetGroupName: { Ref: "DatabaseSubnets56F17B9A" },
MasterUsername: "admin",
MasterUserPassword: "tooshort",
VpcSecurityGroupIds: [ {"Fn::GetAtt": ["DatabaseSecurityGroup5C91FDCB", "GroupId"]}]
}));
Properties: {
Engine: "aurora",
DBSubnetGroupName: { Ref: "DatabaseSubnets56F17B9A" },
MasterUsername: "admin",
MasterUserPassword: "tooshort",
VpcSecurityGroupIds: [ {"Fn::GetAtt": ["DatabaseSecurityGroup5C91FDCB", "GroupId"]}]
},
DeletionPolicy: 'Retain',
UpdateReplacePolicy: 'Retain'
}, ResourcePart.CompleteDefinition));

expect(stack).to(haveResource('AWS::RDS::DBInstance', {
DeletionPolicy: 'Retain',
UpdateReplacePolicy: 'Retain'
}, ResourcePart.CompleteDefinition));

test.done();
},
Expand Down

0 comments on commit 4920f07

Please sign in to comment.