Skip to content

Commit

Permalink
feat(rds): validate backup retention for read replica instances (#17569)
Browse files Browse the repository at this point in the history
Automatic backups of read replica instances are only supported for MySQL
and MariaDB.

See https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_ReadRepl.html#USER_ReadRepl.Overview.Differences

Closes #17356


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
jogold authored Nov 19, 2021
1 parent 79a674e commit 9b2158b
Show file tree
Hide file tree
Showing 6 changed files with 604 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/@aws-cdk/aws-rds/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ new rds.DatabaseInstanceReadReplica(this, 'ReadReplica', {
});
```

Automatic backups of read replica instances are only supported for MySQL and MariaDB. By default,
automatic backups are disabled for read replicas and can only be enabled (using `backupRetention`)
if also enabled on the source instance.

Creating a "production" Oracle database instance with option and parameter groups:

[example of setting up a production oracle instance](test/integ.instance.lit.ts)
Expand Down
12 changes: 12 additions & 0 deletions packages/@aws-cdk/aws-rds/lib/instance-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ export interface IInstanceEngine extends IEngine {
/** The application used by this engine to perform rotation for a multi-user scenario. */
readonly multiUserRotationApplication: secretsmanager.SecretRotationApplication;

/**
* Whether this engine supports automatic backups of a read replica instance.
*
* @default false
*/
readonly supportsReadReplicaBackups?: boolean;

/**
* Method called when the engine is used to create a new instance.
*/
Expand All @@ -123,6 +130,7 @@ abstract class InstanceEngineBase implements IInstanceEngine {
public readonly singleUserRotationApplication: secretsmanager.SecretRotationApplication;
public readonly multiUserRotationApplication: secretsmanager.SecretRotationApplication;
public readonly engineFamily?: string;
public readonly supportsReadReplicaBackups?: boolean;

private readonly features?: InstanceEngineFeatures;

Expand Down Expand Up @@ -320,6 +328,8 @@ export interface MariaDbInstanceEngineProps {
}

class MariaDbInstanceEngine extends InstanceEngineBase {
public readonly supportsReadReplicaBackups = true;

constructor(version?: MariaDbEngineVersion) {
super({
engineType: 'mariadb',
Expand Down Expand Up @@ -533,6 +543,8 @@ export interface MySqlInstanceEngineProps {
}

class MySqlInstanceEngine extends InstanceEngineBase {
public readonly supportsReadReplicaBackups = true;

constructor(version?: MysqlEngineVersion) {
super({
engineType: 'mysql',
Expand Down
8 changes: 7 additions & 1 deletion packages/@aws-cdk/aws-rds/lib/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ export interface DatabaseInstanceNewProps {
* When creating a read replica, you must enable automatic backups on the source
* database instance by setting the backup retention to a value other than zero.
*
* @default Duration.days(1)
* @default - Duration.days(1) for source instances, disabled for read replicas
*/
readonly backupRetention?: Duration;

Expand Down Expand Up @@ -1143,6 +1143,12 @@ export class DatabaseInstanceReadReplica extends DatabaseInstanceNew implements
constructor(scope: Construct, id: string, props: DatabaseInstanceReadReplicaProps) {
super(scope, id, props);

if (props.sourceDatabaseInstance.engine
&& !props.sourceDatabaseInstance.engine.supportsReadReplicaBackups
&& props.backupRetention) {
throw new Error(`Cannot set 'backupRetention', as engine '${engineDescription(props.sourceDatabaseInstance.engine)}' does not support automatic backups for read replicas`);
}

const instance = new CfnDBInstance(this, 'Resource', {
...this.newCfnProps,
// this must be ARN, not ID, because of https://github.com/terraform-providers/terraform-provider-aws/issues/528#issuecomment-391169012
Expand Down
20 changes: 20 additions & 0 deletions packages/@aws-cdk/aws-rds/test/instance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,26 @@ describe('instance', () => {
});
});

test('throws with backupRetention on a read replica if engine does not support it', () => {
// GIVEN
const instanceType = ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.SMALL);
const backupRetention = cdk.Duration.days(5);
const source = new rds.DatabaseInstance(stack, 'Source', {
engine: rds.DatabaseInstanceEngine.postgres({ version: rds.PostgresEngineVersion.VER_13 }),
backupRetention,
instanceType,
vpc,
});

expect(() => {
new rds.DatabaseInstanceReadReplica(stack, 'Replica', {
sourceDatabaseInstance: source,
backupRetention,
instanceType,
vpc,
});
}).toThrow(/Cannot set 'backupRetention', as engine 'postgres-13' does not support automatic backups for read replicas/);
});
});

test.each([
Expand Down
Loading

0 comments on commit 9b2158b

Please sign in to comment.