How to create AWS RDS instance and its replica together

I am trying to create a AWS RDS instance and its replica together in a single call in TS but it is just creating 1st instance and throwing error 404 for the replica one. it can’t find just created RDS instance. how to do this, anyone can help ?
here some basic code attached:

class ExampleStack extends TerraformStack {
  constructor(scope: Construct, name: string) {
    super(scope, name);
    new AwsProvider(this,"test2",{region:"ap-southeast-1"})

    const rdsConfig = {
      identifier: "test-database-1",
      allocatedStorage: 150,
      instanceClass: "db.m5.xlarge"
    };

    const rdsConfig2 = {
      identifier: "test-database-replica",
      allocatedStorage: 150,
      instanceClass: "db.m5.xlarge",
      replicateSourceDb: 'test-database-1'
    };
    new RDS.DbInstance(this,"master",rdsConfig)
    new RDS.DbInstance(this,"slave",rdsConfig2)
  }
}


const main = () => {
  const app = new App();
  new ExampleStack(app, "test-stack-new");
  app.synth();
}

main();

I think the replicateSourceDb value needs to reference an existing database id, see the entry in https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/db_instance#replicate_source_db

I think you’ll need to have the replica depend on the primary either explicitly or implicitly.
Explicit would be assigning a reference the the primary DbInstance in the replica’s dependsOn property.
Implicit would be using the primary DBInstance .arn for the replica source db property.

@DanielMSchmidt , According to replicate_source_db property, identifier of master db instance should be there in replicate_source_db, that is there.

@jsteinich , Tried implicitly, replicateSourceDb: 'test-database-1.arn throwing this error.

How to do explicitly because i am not finding any dependsOn property here:
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/db_instance

const masterDB = new RDS.DbInstance(this,"master",rdsConfig);
new RDS.DbInstance(this,"slave",{
  ...
  dependsOn: [masterDB]
});

or

const masterDB = new RDS.DbInstance(this,"master",rdsConfig);
new RDS.DbInstance(this,"slave",{
  ...
  replicateSourceDb: masterDB.arn
});

By this way i am able to create Db instance and its replica but there are some issues:

  1. We can’t update Db instances because if we pass updated instance to slave Db Instance call it shows this error :

  2. Can we create replica only for already exist Db instance because we need masterDb instance object for that and if it is already there, how will we get it ?

That looks like you are trying to change the replication source of an already created secondary DB. You’ll need to either use the same source or change the identifier of the secondary DB so that a new one is created.

Yep. You can certainly create a replica of an existing DB. You could pass it’s arn as a parameter or use the DataAwsDbInstance data source which would look up the existing instance.