Using a single module for primary and secondary RDS servers

Hello, I’m new to terraform and I’m trying to update the module that I’m currently using for our RDS servers.

Today we have 2 “aws_db_instance” resoruce entries for our RDS: “primary_resource” and “secondary_resource”.

The “primary_resource” is a really simple configuration with the core information needed by our RDS.

The “secondary_resource” was created with a “for_each” clause so we can create multiple secondary servers.

My team want to simplifly our modules and, one of the things that we want to tackle is the option to use only one “aws_db_instance” for the primary and secondary nodes.

I was able to create a simple logic to read a map for each instance to check for the variables. Example:

resource “aws_db_instance” “this” {
for_each = var.instance_list
engine = lookup(each.value, “engine”, local.engine)
engine_version = lookup(each.value, “engine_version”, var.engine_version)
multi_az = lookup(each.value, “multi_az”, var.multi_az)

This is working great when I try to import a RDS to the new module. The problem is happening when I try to create a new RDS due some conflicts with the variable “replicate_source_db”.

If I try to setup the variables “replicate_source_db” and “username” at the same time, a conflict error is raised like this:

│ “username”: conflicts with replicate_source_db

I tried to run some conditional clauses to avoid entering any value for the variable “username” IF there is something in the variable “replicate_source_db”, but nothing worked.

Could you help me?


terraform {
required_version = “>= 1.3.7”
required_providers {
aws = {
source = “hashicorp/aws”
version = “>= 3.63”