Skip to content

Commit

Permalink
Added logGroups Properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mokonist committed Aug 19, 2022
1 parent 2502321 commit 492b6f0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
23 changes: 18 additions & 5 deletions packages/@aws-cdk/aws-rds/lib/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1036,7 +1047,7 @@ export class DatabaseInstance extends DatabaseInstanceSource implements IDatabas
this.secret = secret.attach(this);
}

this.setLogRetention();
this.logGroups = this.setLogRetention();
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1113,7 +1125,7 @@ export class DatabaseInstanceFromSnapshot extends DatabaseInstanceSource impleme
this.secret = secret.attach(this);
}

this.setLogRetention();
this.logGroups = this.setLogRetention();
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -1197,7 +1210,7 @@ export class DatabaseInstanceReadReplica extends DatabaseInstanceNew implements

instance.applyRemovalPolicy(props.removalPolicy ?? RemovalPolicy.SNAPSHOT);

this.setLogRetention();
this.logGroups = this.setLogRetention();
}
}

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 @@ -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([
Expand Down

0 comments on commit 492b6f0

Please sign in to comment.