Unable to Dynamically Retrieve RDS Cluster Endpoint in Terraform Provider Configuration

49 Views Asked by At

I'm encountering an issue with dynamically retrieving the endpoint of an RDS cluster in my Terraform provider configuration when there is change which needs a DB destroy and recreate with updates like enabling encryption or renaming the DB.

Here's the relevant part of my Terraform configuration:

provider "postgresql" {
  host             = module.rds_cluster.endpoint
  port             = var.db_port
  database         = var.db_name
  username         = var.admin_user
  password         = var.admin_password
  superuser        = false
  expected_version = var.engine_version
}

Despite configuring module.rds_cluster.endpoint to fetch the RDS cluster endpoint dynamically, Terraform is substituting it with the localhost IP address instead. However, when I hardcode the endpoint, it works fine.

Error:

Error: Error connecting to PostgreSQL server  (scheme: postgres): dial tcp 127.0.0.1:5432: connect: connection refused

with module.rds_cluster_db.postgresql_role.iam_user,
   on .terraform/modules/rds_cluster_db/main.tf line 103, in resource "postgresql_role" "iam_user":
  103: resource "postgresql_role" "iam_user" {

Error is coming from the below resource,

resource "postgresql_role" "iam_user" {
  name            = var.db_user
  login           = true
  roles           = ["rds_iam"]
  create_database = true
  create_role     = true
  inherit         = true

  depends_on = [
    module.rds_cluster
  ]
}

Details:

I am using, rds_cluster==1.3.1

Terraform and Providers info:

Terraform v1.5.5
on darwin_arm64
+ provider registry.terraform.io/cyrilgdn/postgresql v1.22.0
+ provider registry.terraform.io/hashicorp/archive v2.4.2
+ provider registry.terraform.io/hashicorp/aws v5.41.0
+ provider registry.terraform.io/hashicorp/kubernetes v2.27.0
+ provider registry.terraform.io/hashicorp/null v3.2.2
+ provider registry.terraform.io/hashicorp/random v3.6.0
+ provider registry.terraform.io/hashicorp/tls v4.0.5

Any insights in troubleshooting or fix the issue is helpful and appreciated!

Update:

Module code:

module "rds_cluster_db" {
  source = "github.com/cloudposse/terraform-aws-rds-cluster?ref=1.3.1"

  name      = "my-user-db" # can't update name, fails with the provided error

  db_user       = "db_user"
  db_name       = "db_name"
  instance_type = "db.t4g.small"

  # storage_encrypted = true # can't enable, fails with the provided error
}
0

There are 0 best solutions below