Skip to content

DatabaseCluster: MonitoringRole not created by default when using readers and writers #25941

@hertino76

Description

@hertino76

Describe the bug

I've a Typescript app where I developed a library to create an Aurora Mysql database, this library was created with aws-cdk v2.68, we used this to create a database in production using azure DevOps and cloudFormation (we've a functional database), we used the InstanceProps property, and then we saw a message that this element is deprecated in a new version.

Then we updated to aws-cdk v2.82, and I replaced instanceProps according to the migration recommendations just changing the location of the properties as the code below: (https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_rds-readme.html#migrating-from-instanceprops)

Although no other change was made, there is no way to preserve the Instance IDs when we migrated to using "Writer and Reader" elements and also use the monitoringInterval element.

` Code :

 let cluster = new rds.DatabaseCluster(scope, 'DBCluster', {
  clusterIdentifier: props.nomCluster,
  engine: rds.DatabaseClusterEngine.auroraMysql({ version: varVersion }),
  defaultDatabaseName: props.nomDB,
  credentials: rds.Credentials.fromSecret(this.dbSecret),
  parameterGroup: this.parameterGroupCluster,
  monitoringInterval: Duration.seconds(60),
  copyTagsToSnapshot: false,
  storageEncrypted: true,
  backtrackWindow: Duration.hours(6),
  backup: {
    retention: Duration.days(RetentionDays.ONE_WEEK),
    preferredWindow: '01:00-03:00',
  },
  cloudwatchLogsExports: ['audit'],
  preferredMaintenanceWindow: 'Sat:03:00-Sun:11:00',
  deletionProtection: true,
  removalPolicy: RemovalPolicy.RETAIN,
  vpc: props.vpc,
  vpcSubnets: props.vpcSubnetsData,
  securityGroups: [props.securityGroup],

  writer : rds.ClusterInstance.serverlessV2('instance1', {
    parameterGroup: this.parameterGroupInst,
    publiclyAccessible: false,
    autoMinorVersionUpgrade: true,
    allowMajorVersionUpgrade: false,
    enablePerformanceInsights: true
  }),  ...`

Then we compiled the code, and when we tried to deploy the infrastructure we got several differences with the infra deployed before, and there shouldn't be any differences, in the photo below you can see the differences show, I consider that the one that causes the problem is the first element, the MonitoringRole element which will be destroyed, even though this element was never uses in the code, which means that the application must re-create the instance when deploying and, as a result, deestroy our functional implementation.

aws diff results:

image

Expected Behavior

aws diff shows zero differences when I upgrade to Writer and Reader elements

Current Behavior

aws diff show theses differences :

image

Reproduction Steps

  1. Create a library app (appA-Lib) with Typescript, npm and aws-cdk.
  2. First version of "Lib" application with InstanceProps element:
import { Environment, Duration, RemovalPolicy, } from 'aws-cdk-lib'
import { RetentionDays } from 'aws-cdk-lib/aws-logs'

import * as sm from 'aws-cdk-lib/aws-secretsmanager';
import * as rds from 'aws-cdk-lib/aws-rds';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { Construct } from 'constructs';

export interface IAuroraDbProps {
  environment?: any,
  nomCluster: string,
  nomBD: string,
  parameterGroupCluster: any,
  parameterGroupInst: any,
  SG: any, 
  vpc: any
}

var varVersion: rds.AuroraMysqlEngineVersion;

export class AuroraDb extends Construct {
  public readonly dbSecret: sm.Secret;
  public readonly cluster: rds.DatabaseCluster;

  constructor(scope: Construct, id: string, props: IAuroraDbProps) {
    super(scope, id);

    this.dbSecret = new sm.Secret(scope, 'Secret', {
      description: 'Secret DB',
      generateSecretString: {
        secretStringTemplate: JSON.stringify({ username: 'admin2' }),
        excludePunctuation: true,
        includeSpace: false,
        generateStringKey: 'password',
        excludeCharacters: '/@"\' '      }
    });

    varVersion = rds.AuroraMysqlEngineVersion.VER_3_02_1;
    
    let cluster = new rds.DatabaseCluster(scope, 'ClusterDB', {
      clusterIdentifier: `${props.nomCluster}`,
      engine: rds.DatabaseClusterEngine.auroraMysql({ version: varVersion }),
      defaultDatabaseName: props.nomBD,
      instances: 2,
      credentials: rds.Credentials.fromSecret(this.dbSecret),
      parameterGroup: this.parameterGroupCluster,

      monitoringInterval: Duration.seconds(60),
      copyTagsToSnapshot: false,
      storageEncrypted: true,
      backtrackWindow: Duration.hours(6),
      backup: {
        retention: Duration.days(RetentionDays.ONE_WEEK),
        preferredWindow: '01:00-03:00',
      },
      cloudwatchLogsExports: ['audit'],
      preferredMaintenanceWindow: 'Sat:03:00-Sun:11:00',,
      deletionProtection: true,
      removalPolicy: RemovalPolicy.RETAIN,

      instanceProps: {
        instanceType: new ec2.InstanceType('serverless'),
        vpc: props.vpc,
        vpcSubnets: props.vpcSubnetsData,
        securityGroups: [props.securityGroup],
        parameterGroup: this.parameterGroupInst,
        publiclyAccessible: false,
        autoMinorVersionUpgrade: true,
        allowMajorVersionUpgrade: false,
        enablePerformanceInsights: true        
      }
    });
}
}
  1. Build the library version.
  2. Create a Typescript applicaton (appB) using the created library and deploy a database with this code.
  3. Upgate to aws-cdk v2.82 (although I believe that the initial version may be v2.82)
  4. Second version of the Library application using Writer and Reader elements:
import { Environment, Duration, RemovalPolicy, } from 'aws-cdk-lib'
import { RetentionDays } from 'aws-cdk-lib/aws-logs'

import * as sm from 'aws-cdk-lib/aws-secretsmanager';
import * as rds from 'aws-cdk-lib/aws-rds';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { Construct } from 'constructs';

export interface IAuroraDbProps {
  environment?: any,
  nomCluster: string,
  nomBD: string,
  parameterGroupCluster: any,
  parameterGroupInst: any,
  SG: any, 
  vpc: any
}

var varVersion: rds.AuroraMysqlEngineVersion;

export class AuroraDb extends Construct {
  public readonly dbSecret: sm.Secret;
  public readonly cluster: rds.DatabaseCluster;

  constructor(scope: Construct, id: string, props: IAuroraDbProps) {
    super(scope, id);

    this.dbSecret = new sm.Secret(scope, 'Secret', {
      description: 'Secret DB',
      generateSecretString: {
        secretStringTemplate: JSON.stringify({ username: 'admin2' }),
        excludePunctuation: true,
        includeSpace: false,
        generateStringKey: 'password',
        excludeCharacters: '/@"\' '      }
    });

    varVersion = rds.AuroraMysqlEngineVersion.VER_3_02_1;
    
    let cluster = new rds.DatabaseCluster(scope, 'ClusterDB', {
      clusterIdentifier: `${props.nomCluster}`,
      engine: rds.DatabaseClusterEngine.auroraMysql({ version: varVersion }),
      defaultDatabaseName: props.nomBD,
      credentials: rds.Credentials.fromSecret(this.dbSecret),
      parameterGroup: this.parameterGroupCluster,

      monitoringInterval: Duration.seconds(60),
      copyTagsToSnapshot: false,
      storageEncrypted: true,
      backtrackWindow: Duration.hours(6),
      backup: {
        retention: Duration.days(RetentionDays.ONE_WEEK),
        preferredWindow: '01:00-03:00',
      },
      cloudwatchLogsExports: ['audit'],
      preferredMaintenanceWindow: 'Sat:03:00-Sun:11:00',,
      deletionProtection: true,
      removalPolicy: RemovalPolicy.RETAIN,

      writer : rds.ClusterInstance.serverlessV2(`${props.nomCluster}` + 'instance1', {
        parameterGroup: this.parameterGroupInst,
        publiclyAccessible: false,
        autoMinorVersionUpgrade: true,
        allowMajorVersionUpgrade: false,
        enablePerformanceInsights: true
      }),
    readers : [
        rds.ClusterInstance.serverlessV2(`${props.nomCluster}` + 'instance2', {scaleWithWriter: true}),
      ],
      serverlessV2MinCapacity: minCap,
      serverlessV2MaxCapacity: maxCap,

    });
}
}
  1. Build the library version.
  2. Update the application (appB) using the new version of the library.
  3. Try to deploy a Aurora MySql database with this code --> you will get Differences

Possible Solution

Check why aws-cdk finds differences in the MonitoringRole element when we update these elements and we use monitoringInterval. The problem isn't version 2.82 itself, because I did a test with this version without changing the InstanceProps element and the deployment works fine, the error doesn't show up until I change the element to Writer.

Additional Information/Context

No response

CDK CLI Version

2.82.0 (build 3a8648a)

Framework Version

No response

Node.js Version

npm: '9.3.1', node: '18.14.0',

OS

Windows 10 Entreprise 21H2

Language

Typescript

Language Version

Version 5.0.4

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-rdsRelated to Amazon Relational DatabasebugThis issue is a bug.effort/smallSmall work item – less than a day of effortgood first issueRelated to contributions. See CONTRIBUTING.mdp1

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions