I create AWS Postgres Aurora writer + 2 read replicas with this code (fragment, omitted non-essential for this question code like creation of db subnets)
resource "aws_rds_cluster" "aurora-cluster" {
cluster_identifier = "${var.vpc_name}-aurora"
db_subnet_group_name = "${var.vpc_name}-aurora-subnet"
vpc_security_group_ids = [aws_security_group.vpc.id]
engine = "aurora-postgresql"
engine_version = var.aurora_engine_version
database_name = var.aurora_db_name
master_username = var.aurora_admin
master_password = var.aurora_pass
skip_final_snapshot = true
}
resource "aws_rds_cluster_instance" "aurora-cluster-instance" {
count = 3
identifier = "${var.vpc_name}-aurora-${count.index}"
cluster_identifier = aws_rds_cluster.aurora-cluster.id
instance_class = "db.r5.xlarge"
engine = aws_rds_cluster.default.engine
engine_version = aws_rds_cluster.default.engine_version
}
However I need the flexibility of creating the read replicas of different instance types or even another instance class like db.serverless (Aurora serverless V2)
I can easily create and add alternative read replicas to my rds cluster via AWS UI or AWS CLI but can't figure out how to do it via terraform.
If I'm trying to use another aws_rds_cluster_instance block to add alt replica for the same cluster_identifier:
resource "aws_rds_cluster_instance" "aurora-cluster-ro-instance" {
count = 1
identifier = "${var.vpc_name}-aurora-**ro**-${count.index}"
cluster_identifier = aws_rds_cluster.aurora-cluster.id
instance_class = "db.r6g.xlarge"
engine = aws_rds_cluster.default.engine
engine_version = aws_rds_cluster.default.engine_version
}
however the terraform exits with error:
aws_rds_cluster_instance.aurora-cluster-ro-instances[0]: Creating...
Error: error creating RDS Cluster (test-aurora) Instance: DBInstanceAlreadyExists: DB instance >already exists
status code: 400, request id: 3e33b2c9-1284-4c2d-b8c7-2d8eedd997fe
on db_aurora.tf line 65, in resource "aws_rds_cluster_instance" "aurora-cluster-ro-instances":
65: resource "aws_rds_cluster_instance" "aurora-cluster-ro-instances" {
Any idea how to properly create custom read replicas for Aurora Cluster? Appreciate your help!