Skip to content

Commit

Permalink
moved default monitoring role instantiation into AuroraCluster constr…
Browse files Browse the repository at this point in the history
…uctor
  • Loading branch information
lpizzinidev committed Jun 26, 2023
1 parent 99a424a commit 7270b30
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
15 changes: 13 additions & 2 deletions packages/aws-cdk-lib/aws-rds/lib/aurora-cluster-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PerformanceInsightRetention } from './props';
import { CfnDBInstance } from './rds.generated';
import { ISubnetGroup } from './subnet-group';
import * as ec2 from '../../aws-ec2';
import { IRole } from '../../aws-iam';
import { IRole, ManagedPolicy, Role, ServicePrincipal } from '../../aws-iam';
import * as kms from '../../aws-kms';
import { IResource, Resource, Duration, RemovalPolicy, ArnFormat } from '../../core';

Expand Down Expand Up @@ -460,6 +460,17 @@ class AuroraClusterInstance extends Resource implements IAuroraClusterInstance {
throw new Error('`enablePerformanceInsights` disabled, but `performanceInsightRetention` or `performanceInsightEncryptionKey` was set');
}

// Generate default monitoring role if not provided
let monitoringRole = props.monitoringRole;
if (!props.monitoringRole && props.monitoringInterval && props.monitoringInterval.toSeconds()) {
monitoringRole = new Role(this, 'MonitoringRole', {
assumedBy: new ServicePrincipal('monitoring.rds.amazonaws.com'),
managedPolicies: [
ManagedPolicy.fromAwsManagedPolicyName('service-role/AmazonRDSEnhancedMonitoringRole'),
],
});
}

const instanceParameterGroup = props.parameterGroup ?? (
props.parameters
? new ParameterGroup(props.cluster, 'InstanceParameterGroup', {
Expand Down Expand Up @@ -492,7 +503,7 @@ class AuroraClusterInstance extends Resource implements IAuroraClusterInstance {
dbSubnetGroupName: props.isFromLegacyInstanceProps ? props.subnetGroup?.subnetGroupName : undefined,
dbParameterGroupName: instanceParameterGroupConfig?.parameterGroupName,
monitoringInterval: props.monitoringInterval && props.monitoringInterval.toSeconds(),
monitoringRoleArn: props.monitoringRole && props.monitoringRole.roleArn,
monitoringRoleArn: monitoringRole && monitoringRole.roleArn,
autoMinorVersionUpgrade: props.autoMinorVersionUpgrade,
allowMajorVersionUpgrade: props.allowMajorVersionUpgrade,
});
Expand Down
20 changes: 5 additions & 15 deletions packages/aws-cdk-lib/aws-rds/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,33 +632,23 @@ abstract class DatabaseClusterNew extends DatabaseClusterBase {
*
* @internal
*/
protected _createInstances(cluster: DatabaseClusterNew, props: DatabaseClusterProps): InstanceConfig {
protected _createInstances(props: DatabaseClusterProps): InstanceConfig {
const instanceEndpoints: Endpoint[] = [];
const instanceIdentifiers: string[] = [];
const readers: IAuroraClusterInstance[] = [];

let monitoringRole = props.monitoringRole;
if (!props.monitoringRole && props.monitoringInterval && props.monitoringInterval.toSeconds()) {
monitoringRole = new Role(cluster, 'MonitoringRole', {
assumedBy: new ServicePrincipal('monitoring.rds.amazonaws.com'),
managedPolicies: [
ManagedPolicy.fromAwsManagedPolicyName('service-role/AmazonRDSEnhancedMonitoringRole'),
],
});
}

// need to create the writer first since writer is determined by what instance is first
const writer = props.writer!.bind(this, this, {
monitoringInterval: props.monitoringInterval,
monitoringRole: monitoringRole,
monitoringRole: props.monitoringRole,
removalPolicy: props.removalPolicy ?? RemovalPolicy.SNAPSHOT,
subnetGroup: this.subnetGroup,
promotionTier: 0, // override the promotion tier so that writers are always 0
});
(props.readers ?? []).forEach(instance => {
const clusterInstance = instance.bind(this, this, {
monitoringInterval: props.monitoringInterval,
monitoringRole: monitoringRole,
monitoringRole: props.monitoringRole,
removalPolicy: props.removalPolicy ?? RemovalPolicy.SNAPSHOT,
subnetGroup: this.subnetGroup,
});
Expand Down Expand Up @@ -999,7 +989,7 @@ export class DatabaseCluster extends DatabaseClusterNew {
throw new Error('writer must be provided');
}

const createdInstances = props.writer ? this._createInstances(this, props) : legacyCreateInstances(this, props, this.subnetGroup);
const createdInstances = props.writer ? this._createInstances(props) : legacyCreateInstances(this, props, this.subnetGroup);
this.instanceIdentifiers = createdInstances.instanceIdentifiers;
this.instanceEndpoints = createdInstances.instanceEndpoints;
}
Expand Down Expand Up @@ -1196,7 +1186,7 @@ export class DatabaseClusterFromSnapshot extends DatabaseClusterNew {
if ((props.writer || props.readers) && (props.instances || props.instanceProps)) {
throw new Error('Cannot provide clusterInstances if instances or instanceProps are provided');
}
const createdInstances = props.writer ? this._createInstances(this, props) : legacyCreateInstances(this, props, this.subnetGroup);
const createdInstances = props.writer ? this._createInstances(props) : legacyCreateInstances(this, props, this.subnetGroup);
this.instanceIdentifiers = createdInstances.instanceIdentifiers;
this.instanceEndpoints = createdInstances.instanceEndpoints;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-rds/test/cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1602,7 +1602,7 @@ describe('cluster', () => {
Template.fromStack(stack).hasResourceProperties('AWS::RDS::DBInstance', {
MonitoringInterval: 60,
MonitoringRoleArn: {
'Fn::GetAtt': ['DatabaseMonitoringRole576991DA', 'Arn'],
'Fn::GetAtt': ['DatabasewriterMonitoringRole23A1E3B6', 'Arn'],
},
});

Expand Down

0 comments on commit 7270b30

Please sign in to comment.