Search code examples
postgresqlamazon-rdsterraform-provider-aws

AWS RDS for PostgreSQL Connection attempt timed out error


I created postgresql rds in aws with terraform. I'm checking from the aws console, everything seems normal. But I'm trying to connect to database with DBeaver but I can't connect. Likewise, I can't make the ssh connection for the ec2 I created, maybe there is a connection. The terraform codes I wrote:

# postgres-db/main.tf
resource "aws_db_instance" "default" {
  allocated_storage      = 20
  storage_type           = "gp2"
  engine                 = var.engine
  engine_version         = var.engine-version
  instance_class         = var.instance-class
  db_name                = var.db-name
  identifier             = var.identifier
  username               = var.username
  password               = var.password
  port                   = var.port
  publicly_accessible    = var.publicly-accessible
  db_subnet_group_name   = var.db-subnet-group-name
  parameter_group_name   = var.parameter-group-name
  vpc_security_group_ids = var.vpc-security-group-ids
  apply_immediately      = var.apply-immediately
  skip_final_snapshot    = true
}

module "service-db" {
  source = "./postgres-db"

  apply-immediately      = true
  db-name                = var.service-db-name
  db-subnet-group-name   = data.terraform_remote_state.server.outputs.db_subnet_group
  identifier             = "${var.app-name}-db"
  password               = var.service-db-password
  publicly-accessible    = true # TODO: True for now, but should be false
  username               = var.service-db-username
  vpc-security-group-ids = [data.terraform_remote_state.server.outputs.security_group_allow_internal_postgres]
}
resource "aws_security_group" "allow_internal_postgres" {
  name        = "allow-internal-postgres"
  description = "Allow internal Postgres traffic"
  vpc_id      = aws_vpc.vpc.id

  ingress {
    from_port   = 5432
    to_port     = 5432
    protocol    = "tcp"
    cidr_blocks = [aws_vpc.vpc.cidr_block, "0.0.0.0/0"] # TODO: Remove public IP
  }
}

In the research I did, it was written things like edit the security rules or set it to public, it seems like that anyway.

Security group inbound rules

Public accessible

How can I solve this problem can you please help?


Solution

  • I solved my problem by setting the subnet group to public.

    module "service-db" {
      source = "./postgres-db"
    
      apply-immediately      = true
      db-name                = var.service-db-name
      db-subnet-group-name   = data.terraform_remote_state.server.outputs.db_subnet_group_public
      identifier             = "${var.app-name}-db"
      password               = var.service-db-password
      publicly-accessible    = true # TODO: True for now, but should be false
      username               = var.service-db-username
      vpc-security-group-ids = [data.terraform_remote_state.server.outputs.security_group_allow_internal_postgres]
    }
    
    resource "aws_db_subnet_group" "private" {
      name       = "${var.server_name}-db-subnet-group-private"
      subnet_ids = aws_subnet.private.*.id
    
      tags = {
        Name = "${var.server_name} DB Subnet Group Private"
      }
    }
    
    resource "aws_db_subnet_group" "public" {
      name       = "${var.server_name}-db-subnet-group-public"
      subnet_ids = aws_subnet.public.*.id
    
      tags = {
        Name = "${var.server_name} DB Subnet Group Public"
      }
    }