Error creating AWS RDS Cluster Instance with Terraform

Question: Error creating AWS RDS Cluster postgres Instance with Terraform

I’m trying to create an RDS cluster with instances using Terraform, but I encounter the following error:

Error: creating RDS Cluster (test) Instance (test-1): InvalidParameterValue: CreateDBInstance can't be used to create a DB instance in a Multi-AZ DB cluster. Use CreateDBCluster instead.

Here is my Terraform code:

resource "aws_rds_cluster_instance" "test" {
  provider = aws.rds

  count              = 2
  identifier         = "test-${count.index}"
  cluster_identifier = aws_rds_cluster.test.id
  instance_class     = "db.m6gd.large"
  engine             = aws_rds_cluster.test.engine
  engine_version     = aws_rds_cluster.test.engine_version
  monitoring_interval = 60 # 60 seconds interval
}

resource "aws_rds_cluster" "test" {
  provider = aws.rds

  cluster_identifier = "test"

  cluster_members = ["test-instance-1", "test-instance-2", "test-instance-3"]

  engine                      = "postgres"
  engine_version              = "15.7"
  allow_major_version_upgrade = true
  availability_zones          = ["eu-central-1a", "eu-central-1b", "eu-central-1c"]

  db_cluster_instance_class = "db.m6gd.large"
  allocated_storage         = 220
  storage_type              = "gp3"
  iops = 3000
  manage_master_user_password         = true
  master_username                     = "postgres"
  master_user_secret_kms_key_id       = data.aws_kms_key.by_id.arn
  iam_database_authentication_enabled = false
  ca_certificate_identifier           = "rds-ca-rsa2048-g1"

  db_subnet_group_name = aws_db_subnet_group.database_2.name
  vpc_security_group_ids = [aws_default_security_group.default.id]

  deletion_protection = false
}

Issues:

  • I get an error message that suggests using CreateDBCluster instead of CreateDBInstance.

How can I properly configure the aws_rds_cluster and aws_rds_cluster_instance resources for a Multi-AZ DB cluster postgres in Terraform? Any suggestions for managing these configurations?

The aws_rds_cluster_instance resource applies only to Aurora, not regular RDS, clusters. AFAIK a regular RDS multi-AZ DB cluster has a pretty static topology consisting of one writer DB instance and two reader DB instances in different AZs.

It seems possible to create more read replicas but it’s likely not treated like a member of the cluster (not 100% sure). You you can read about it in Creating a DB instance read replica from a Multi-AZ DB cluster. To create one in Terraform, it looks like you would use the aws_db_instance resource with the source replication DB set to the multi-AZ DB cluster. I don’t have direct experience but feel free to give if a try if you have this need.