How to get the IDatabaseInstance of Amazon Aurora

Viewed 89
    const dbCluster = new rds.DatabaseCluster(this, 'Database', {
      parameterGroup,
      engine: engine,
      credentials: rdsCredentials,
      cloudwatchLogsExports:['slowquery','general','error','audit'],
      backup: backupProps,
      instances:instances,
      storageEncrypted:true,
      removalPolicy: cdk.RemovalPolicy.DESTROY,
      clusterIdentifier: dbInfos['cluster'], //clusterIdentifier,
      defaultDatabaseName :dbInfos['database'], //defaultDatabaseName,
      instanceProps: {
        instanceType: instanceType,
        vpcSubnets:props.vpc.selectSubnets({ subnetType: ec2.SubnetType.PRIVATE_ISOLATED }),
        vpc:props!.vpc,
        securityGroups:[dbSecurityGroup],
      },
      subnetGroup:subnetGroup
    });

    new rds.DatabaseInstanceReadReplica(this, 'ReadReplica', {
      sourceDatabaseInstance: dbCluster, // error

      instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
      vpc,
    });

There comes the error at sourceDatabaseInstance: dbCluster

    Type 'DatabaseCluster' is missing the following properties from type 'IDatabaseInstance': instanceIdentifier, instanceArn, dbInstanceEndpointAddress, dbInstanceEndpointPort, and 6 more.ts(2740)
    instance.d.ts(676, 14): The expected type comes from property 'sourceDatabaseInstance' which is declared here on type 'DatabaseInstanceReadReplicaProps'

Now I am trying to add read replicas to Amazon Aurora:

https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_rds.DatabaseInstanceReadReplica.html

DatabaseInstanceReadReplica accepts the sourceDatabaseInstance as IDatabaseInstance

However Aurora has cluster but not instance.

So what should I set here?

1 Answers

Add Aurora replicas with the instances prop:

instances: 3; // = 1 writer + 2 replicas

As the Aurora Replicas docs say:

When you create a second, third, and so on DB instance in an Aurora provisioned DB cluster, Aurora automatically sets up replication from the writer DB instance to all the other DB instances. These other DB instances are read-only and are known as Aurora Replicas. We also refer to them as reader instances when discussing the ways that you can combine writer and reader DB instances within a cluster.

Related