0

Im in a similar boat to the user on this question; Connecting the elastic beanstalk environment with existing RDS instance

I have a Codestar project (Django via Beanstalk) and i cannot get the application to access any rds instances. When attempting to deploy during the testing step (python manage.py test) the following error message is thrown;

django.db.utils.OperationalError: could not connect to server: Connection timed out
    Is the server running on host "172.31.44.126" and accepting
    TCP/IP connections on port 5432?

Things ive already tried

  • Running locally

works fine via django and psql

  • using ipaddress and public hostname (name.key.eu-west-2.rds.amazonaws.com)

both work fine locally and provide the same error message when deploying

  • Connecting to rds via the EC2 instance

This works fine connecting via local ip and name. Queries such as \d and \l return fine indincating the instance can see the database.

  • Changing vpc / subnets

Both EC2 and rds instances are in the same vpc (only have one) and subnet (eu-west, availability group 2a)

  • Spinning up an attached beanstalk rds instance

I have tried dedicated rds instances and a connected instance using the EBS tool and the application has been unable to connect to either.

Settings.py

if 'RDS_DB_NAME' in os.environ:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': os.environ['RDS_DB_NAME'],
            'USER': os.environ['RDS_USERNAME'],
            'PASSWORD': os.environ['RDS_PASSWORD'],
            'HOST': os.environ['RDS_HOSTNAME'],
            'PORT': os.environ['RDS_PORT'],
        }
    }
else:
    DATABASES = {
        'default':
        {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'postgres',
            'USER': 'db_user',
            'PASSWORD': 'db_pwd',
            'HOST': 'db_host',
            'PORT': '5432',
        }
}
Connor Willoughby
  • 86
  • 1
  • 1
  • 10
  • I used the python-decouple package `config` instead of `os.environ` because the `config` option lets me set port type `int` instead of os.environ defaulting this to `str`. e.g. ```from decouple import config ... if whichenv == 'production': DATABASES = {'default': ...'PORT': config('RDS_PORT', default=5432, cast=int) }} ``` – Heartthrob_Rob Jun 21 '23 at 19:48
  • Not sure that accessing the variables is a factor here as the error appears to be related to networking. The inbound rules were configured at the time but ive since given up with this design. – Connor Willoughby Jul 04 '23 at 10:52

1 Answers1

0

Your issue seems to be inbound rules. You need to set up inbound rules for the security group to connect to the database. You can set up one inbound rule for your development environment and another for Elastic Beanstalk. For information about setting up security group inbound rules, see Controlling Access with Security Groups.

smac2020
  • 9,637
  • 4
  • 24
  • 38
  • As it stands i already have the following rules implemented in a security group which both the rds and ec2 instance belong to - Direct granting of the default security group - Cidr notation of the default vpc group - private and public ip of the ec2 instance Im not sure what more i can add to the scg? – Connor Willoughby Aug 11 '21 at 08:59