Getting "Error: Invalid index" while performing terraform -plan

Hello Team,

I am trying to deploy my aws infrastructure but terraform plan is failing with the below error.

Error


Error: Invalid index

on xxxxxx\xxxxx\xxxxxxxx.tf line 22, in data “template_file” “init”:
22: rds_host = var.create_rds == 1 ? “${join(”", aws_db_instance.default[0].*.address)}" : “”
|----------------
| aws_db_instance.default is empty tuple

The given key does not identify an element in this collection value.

Releasing state lock. This may take a few moments…


I tried to replace “aws_db_instance.default[count.index]” as mentioned in #22480 but getting the below mentioned error

Error

Error: Reference to “count” in non-counted context

on xxxxxx\xxxxx\xxxxxxxx.tf line 22, in data “template_file” “init”:
22: rds_host = var.create_rds == 1 ? “${join(”", aws_db_instance.default[count.index].*.address)}" : “”

The “count” object can be used only in “resource” and “data” blocks, and only
when the “count” argument is set.

Releasing state lock. This may take a few moments…


Please let me know how i can fix this.

Thank You.

Impossible to say unless you share the resource definition aws_db_instance.default