diff --git a/packages/@aws-cdk/aws-rds/lib/instance.ts b/packages/@aws-cdk/aws-rds/lib/instance.ts index 9b94564122ff2..4ecf19a270374 100644 --- a/packages/@aws-cdk/aws-rds/lib/instance.ts +++ b/packages/@aws-cdk/aws-rds/lib/instance.ts @@ -3,6 +3,7 @@ import * as events from '@aws-cdk/aws-events'; import * as iam from '@aws-cdk/aws-iam'; import * as kms from '@aws-cdk/aws-kms'; import * as logs from '@aws-cdk/aws-logs'; +import { ILogGroup, LogGroup } from '@aws-cdk/aws-logs'; import * as s3 from '@aws-cdk/aws-s3'; import * as secretsmanager from '@aws-cdk/aws-secretsmanager'; import { ArnComponents, ArnFormat, Duration, FeatureFlags, IResource, Lazy, RemovalPolicy, Resource, Stack, Token, Tokenization } from '@aws-cdk/core'; @@ -644,6 +645,11 @@ abstract class DatabaseInstanceNew extends DatabaseInstanceBase implements IData protected enableIamAuthentication?: boolean; + /** + * List of LogGroups when CloudWatch log output is enabled. + */ + public abstract readonly logGroups: 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 +768,20 @@ abstract class DatabaseInstanceNew extends DatabaseInstanceBase implements IData }; } - protected setLogRetention() { + protected setLogRetention(): ILogGroup[] { + const logGroups: ILogGroup[] = []; if (this.cloudwatchLogsExports && this.cloudwatchLogsRetention) { for (const log of this.cloudwatchLogsExports) { + const logGroupName = `/aws/rds/instance/${this.instanceIdentifier}/${log}`; + logGroups.push(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 +1016,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 +1047,7 @@ export class DatabaseInstance extends DatabaseInstanceSource implements IDatabas this.secret = secret.attach(this); } - this.setLogRetention(); + this.logGroups = this.setLogRetention(); } } @@ -1073,6 +1084,7 @@ export class DatabaseInstanceFromSnapshot extends DatabaseInstanceSource impleme public readonly dbInstanceEndpointPort: string; public readonly instanceEndpoint: Endpoint; public readonly secret?: secretsmanager.ISecret; + public readonly logGroups: ILogGroup[]; constructor(scope: Construct, id: string, props: DatabaseInstanceFromSnapshotProps) { super(scope, id, props); @@ -1113,7 +1125,7 @@ export class DatabaseInstanceFromSnapshot extends DatabaseInstanceSource impleme this.secret = secret.attach(this); } - this.setLogRetention(); + this.logGroups = this.setLogRetention(); } } @@ -1161,6 +1173,7 @@ export class DatabaseInstanceReadReplica extends DatabaseInstanceNew implements public readonly dbInstanceEndpointPort: string; public readonly instanceEndpoint: Endpoint; public readonly engine?: IInstanceEngine = undefined; + public readonly logGroups: ILogGroup[]; protected readonly instanceType: ec2.InstanceType; constructor(scope: Construct, id: string, props: DatabaseInstanceReadReplicaProps) { @@ -1197,7 +1210,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([