From 87d50e770dea9f0562303f4422ae5785aefc92d0 Mon Sep 17 00:00:00 2001 From: mokocm Date: Fri, 19 Aug 2022 17:12:25 +0900 Subject: [PATCH 1/2] Added logGroups Properties --- packages/@aws-cdk/aws-rds/lib/instance.ts | 22 ++++++++++++++----- .../@aws-cdk/aws-rds/test/instance.test.ts | 20 +++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/packages/@aws-cdk/aws-rds/lib/instance.ts b/packages/@aws-cdk/aws-rds/lib/instance.ts index 9b94564122ff2..2133f95abb42a 100644 --- a/packages/@aws-cdk/aws-rds/lib/instance.ts +++ b/packages/@aws-cdk/aws-rds/lib/instance.ts @@ -644,6 +644,11 @@ abstract class DatabaseInstanceNew extends DatabaseInstanceBase implements IData protected enableIamAuthentication?: boolean; + /** + * List of LogGroups when CloudWatch log output is enabled. + */ + public abstract readonly logGroups: logs.ILogGroup[]; + constructor(scope: Construct, id: string, props: DatabaseInstanceNewProps) { // RDS always lower-cases the ID of the database, so use that for the physical name // (which is the name used for cross-environment access, so it needs to be correct, @@ -762,16 +767,20 @@ abstract class DatabaseInstanceNew extends DatabaseInstanceBase implements IData }; } - protected setLogRetention() { + protected setLogRetention(): logs.ILogGroup[] { + const logGroups: logs.ILogGroup[] = []; if (this.cloudwatchLogsExports && this.cloudwatchLogsRetention) { for (const log of this.cloudwatchLogsExports) { + const logGroupName = `/aws/rds/instance/${this.instanceIdentifier}/${log}`; + logGroups.push(logs.LogGroup.fromLogGroupName(this, `LogGroup${this.instanceIdentifier}${log}`, logGroupName)); new logs.LogRetention(this, `LogRetention${log}`, { - logGroupName: `/aws/rds/instance/${this.instanceIdentifier}/${log}`, + logGroupName, retention: this.cloudwatchLogsRetention, role: this.cloudwatchLogsRetentionRole, }); } } + return logGroups; } } @@ -1006,6 +1015,7 @@ export class DatabaseInstance extends DatabaseInstanceSource implements IDatabas public readonly dbInstanceEndpointPort: string; public readonly instanceEndpoint: Endpoint; public readonly secret?: secretsmanager.ISecret; + public readonly logGroups: logs.ILogGroup[]; constructor(scope: Construct, id: string, props: DatabaseInstanceProps) { super(scope, id, props); @@ -1036,7 +1046,7 @@ export class DatabaseInstance extends DatabaseInstanceSource implements IDatabas this.secret = secret.attach(this); } - this.setLogRetention(); + this.logGroups = this.setLogRetention(); } } @@ -1073,6 +1083,7 @@ export class DatabaseInstanceFromSnapshot extends DatabaseInstanceSource impleme public readonly dbInstanceEndpointPort: string; public readonly instanceEndpoint: Endpoint; public readonly secret?: secretsmanager.ISecret; + public readonly logGroups: logs.ILogGroup[]; constructor(scope: Construct, id: string, props: DatabaseInstanceFromSnapshotProps) { super(scope, id, props); @@ -1113,7 +1124,7 @@ export class DatabaseInstanceFromSnapshot extends DatabaseInstanceSource impleme this.secret = secret.attach(this); } - this.setLogRetention(); + this.logGroups = this.setLogRetention(); } } @@ -1161,6 +1172,7 @@ export class DatabaseInstanceReadReplica extends DatabaseInstanceNew implements public readonly dbInstanceEndpointPort: string; public readonly instanceEndpoint: Endpoint; public readonly engine?: IInstanceEngine = undefined; + public readonly logGroups: logs.ILogGroup[]; protected readonly instanceType: ec2.InstanceType; constructor(scope: Construct, id: string, props: DatabaseInstanceReadReplicaProps) { @@ -1197,7 +1209,7 @@ export class DatabaseInstanceReadReplica extends DatabaseInstanceNew implements instance.applyRemovalPolicy(props.removalPolicy ?? RemovalPolicy.SNAPSHOT); - this.setLogRetention(); + this.logGroups = this.setLogRetention(); } } diff --git a/packages/@aws-cdk/aws-rds/test/instance.test.ts b/packages/@aws-cdk/aws-rds/test/instance.test.ts index 8ddc2b92dd697..ae1dcfeb51517 100644 --- a/packages/@aws-cdk/aws-rds/test/instance.test.ts +++ b/packages/@aws-cdk/aws-rds/test/instance.test.ts @@ -1684,6 +1684,26 @@ describe('instance', () => { Engine: 'postgres', }); }); + + test('If log output is enabled, expected logGroups Properties can be obtained', () => { + // GIVEN + const cloudwatchLogsExports = ['error', 'general', 'slowquery', 'audit']; + + // WHEN + const instance = new rds.DatabaseInstance(stack, 'Instance', { + engine: rds.DatabaseInstanceEngine.mysql({ version: rds.MysqlEngineVersion.VER_8_0_26 }), + vpc, + cloudwatchLogsExports, + cloudwatchLogsRetention: logs.RetentionDays.ONE_MONTH, + }); + + // THEN + expect(instance.logGroups.length).toEqual(cloudwatchLogsExports.length); + + instance.logGroups.forEach((logGroup, i) => { + expect(logGroup.logGroupName).toEqual(`/aws/rds/instance/${instance.instanceIdentifier}/${cloudwatchLogsExports[i]}`); + }); + }); }); test.each([ From 7a590df575768d74f458e248f73150ce3b74d01e Mon Sep 17 00:00:00 2001 From: mokocm Date: Fri, 19 Aug 2022 17:59:56 +0900 Subject: [PATCH 2/2] Described in README about logGroups --- packages/@aws-cdk/aws-rds/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/@aws-cdk/aws-rds/README.md b/packages/@aws-cdk/aws-rds/README.md index 813a5f33dcca4..9c7ba57d98636 100644 --- a/packages/@aws-cdk/aws-rds/README.md +++ b/packages/@aws-cdk/aws-rds/README.md @@ -544,6 +544,10 @@ const instance = new rds.DatabaseInstance(this, 'Instance', { cloudwatchLogsExports: ['postgresql'], // Export the PostgreSQL logs // ... }); + +instance.logGroups.foreach(logGroup => { + // You can get log group +}) ``` ## Option Groups