Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws-rds): add logGroups properties #21678

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
Expand Down
22 changes: 17 additions & 5 deletions packages/@aws-cdk/aws-rds/lib/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}

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

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

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -1197,7 +1209,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