Skip to content

Commit 9c82bca

Browse files
feat(rds): add ability to specify PreferredMaintenanceWindow to RDS cluster database instances (#29033)
### Issue # (if applicable) Closes [#16954](#16954) ### Reason for this change Noticed that we were able to specify preferredMaintenanceWindow for a cluster, but unable to do so for the instances created under the cluster. Instead, AWS (semi-)randomly assigns a maintenance window ([doc](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_UpgradeDBInstance.Maintenance.html#Concepts.DBMaintenance)) for the instances, which leads to things being out of sync b/w the cluster and its child instances There are some workarounds as mentioned in the issue above, but those are a little hacky (imo) and I figured adding the preferredMaintenanceWindow as an instance prop is a better long-term solution. Also, it might be hard for other developers to find the workarounds as they are only mentioned in the above issue and aren't available through normal channels (Stack overflow/official CDK docs) ### Description of changes Added optional preferredMaintenanceWindow field under `InstanceProps`, and passed that field in during the creation of the `CfnDBInstance`. Also added a quick unit test ### Description of how you validated changes Added a unit test, did not add integ tests. Ran `yarn build` and `yarn test` Callout: I was unable to run integration tests locally, kept getting errors with `yarn integ --directory packages/aws-cdk-lib/aws-rds` and `yarn integ-runner --directory packages/aws-cdk-lib/aws-rds` - `Error: Cannot find module './integ-runner.js'`, not sure if I'm missing something ### Checklist - [ x ] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) No breaking changes *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 2378635 commit 9c82bca

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

Diff for: packages/aws-cdk-lib/aws-rds/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -1204,3 +1204,23 @@ The `vpc` parameter is optional.
12041204
If not provided, the cluster will be created in the default VPC of the account and region.
12051205
As this VPC is not deployed with AWS CDK, you can't configure the `vpcSubnets`, `subnetGroup` or `securityGroups` of the Aurora Serverless Cluster.
12061206
If you want to provide one of `vpcSubnets`, `subnetGroup` or `securityGroups` parameter, please provide a `vpc`.
1207+
1208+
### Preferred Maintenance Window
1209+
1210+
When creating an RDS cluster, it is possible to (optionally) specify a preferred maintenance window for the cluster as well as the instances under the cluster.
1211+
See [AWS docs](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/USER_UpgradeDBInstance.Maintenance.html#Concepts.DBMaintenance) for more information regarding maintenance windows.
1212+
1213+
The following code snippet shows an example of setting the cluster's maintenance window to 22:15-22:45 (UTC) on Saturdays, but setting the instances' maintenance window
1214+
to 23:15-23:45 on Sundays
1215+
1216+
```ts
1217+
declare const vpc: ec2.Vpc;
1218+
new rds.DatabaseCluster(this, 'DatabaseCluster', {
1219+
engine: rds.DatabaseClusterEngine.AURORA,
1220+
instanceProps: {
1221+
vpc: vpc,
1222+
preferredMaintenanceWindow: 'Sun:23:15-Sun:23:45',
1223+
},
1224+
preferredMaintenanceWindow: 'Sat:22:15-Sat:22:45',
1225+
});
1226+
```

Diff for: packages/aws-cdk-lib/aws-rds/lib/cluster.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,7 @@ function legacyCreateInstances(cluster: DatabaseClusterNew, props: DatabaseClust
14151415
autoMinorVersionUpgrade: instanceProps.autoMinorVersionUpgrade,
14161416
allowMajorVersionUpgrade: instanceProps.allowMajorVersionUpgrade,
14171417
deleteAutomatedBackups: instanceProps.deleteAutomatedBackups,
1418+
preferredMaintenanceWindow: instanceProps.preferredMaintenanceWindow,
14181419
});
14191420

14201421
// For instances that are part of a cluster:

Diff for: packages/aws-cdk-lib/aws-rds/lib/props.ts

+11
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ export interface InstanceProps {
101101
* @default - `true` if `vpcSubnets` is `subnetType: SubnetType.PUBLIC`, `false` otherwise
102102
*/
103103
readonly publiclyAccessible?: boolean;
104+
105+
/**
106+
* A preferred maintenance window day/time range. Should be specified as a range ddd:hh24:mi-ddd:hh24:mi (24H Clock UTC).
107+
*
108+
* Example: 'Sun:23:45-Mon:00:15'
109+
*
110+
* @default - 30-minute window selected at random from an 8-hour block of time for
111+
* each AWS Region, occurring on a random day of the week.
112+
* @see https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/USER_UpgradeDBInstance.Maintenance.html#Concepts.DBMaintenance
113+
*/
114+
readonly preferredMaintenanceWindow?: string;
104115
}
105116

106117
/**

Diff for: packages/aws-cdk-lib/aws-rds/test/cluster.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,30 @@ describe('cluster new api', () => {
209209
],
210210
});
211211
});
212+
213+
test('preferredMaintenanceWindow provided in InstanceProps', () => {
214+
// GIVEN
215+
const stack = testStack();
216+
const vpc = new ec2.Vpc(stack, 'VPC');
217+
218+
const PREFERRED_MAINTENANCE_WINDOW: string = 'Sun:12:00-Sun:13:00';
219+
220+
// WHEN
221+
new DatabaseCluster(stack, 'Database', {
222+
engine: DatabaseClusterEngine.AURORA,
223+
instanceProps: {
224+
vpc: vpc,
225+
preferredMaintenanceWindow: PREFERRED_MAINTENANCE_WINDOW,
226+
},
227+
});
228+
229+
// THEN
230+
const template = Template.fromStack(stack);
231+
// maintenance window is set
232+
template.hasResourceProperties('AWS::RDS::DBInstance', Match.objectLike({
233+
PreferredMaintenanceWindow: PREFERRED_MAINTENANCE_WINDOW,
234+
}));
235+
});
212236
});
213237

214238
describe('migrate from instanceProps', () => {

0 commit comments

Comments
 (0)