简体   繁体   中英

Create AWS Postgres Aurora read replica with instance type different from writer

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!

After some experiments I found out how to work around this issue. It looks like this is a terraform aws provider bug - at least in my version 12.31 with provider 4.21.0

This is the working definition of the read replica that allows to define custom instance type or class and other parameters:

resource "aws_rds_cluster_instance" "aurora-cluster-ro-instance"  {
count               = 1
identifier          = "${var.vpc_name}-aurora-**ro**-${count.index}"
cluster_identifier  = var.string_cluster_id
instance_class      = "db.r6g.xlarge"
engine              = aws_rds_cluster.default.engine
engine_version      = aws_rds_cluster.default.engine_version
}

As you see here the cluster_identifier cannot be the terraform object, which causes the error above, but has to be string variable var.string_cluster_id

This allows proper replica creation under the desired cluster_identifier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM