Django on Elastic Beanstalk not detecting RDS environment variables (falls back to SQLite)
I am deploying a Django app to AWS Elastic Beanstalk and connecting it to an RDS PostgreSQL database.
My Django settings are configured to use PostgreSQL only if RDS_DB_NAME exists in os.environ, otherwise it falls back to SQLite.
However, even though RDS environment variables are defined in Elastic Beanstalk, Django behaves as if they are not present and falls back to SQLite.
settings.py
if 'RDS_DB_NAME' in os.environ:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'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.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
Elastic Beanstalk Environment Variables (from console)
Under Configuration → Environment properties:
RDS_DB_NAME=ebdb
RDS_USERNAME=vividvaultdb
RDS_PASSWORD=********
RDS_HOSTNAME=awseb-xxxxx.ap-south-1.rds.amazonaws.com
RDS_PORT=5432
All variables are clearly visible in the EB console.
django.config
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: vividvault.wsgi:application
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: static
db-migrate.config
container_commands:
01_migrate:
command: "source /var/app/venv/*/bin/activate && python3 manage.py migrate"
leader_only: true
option_settings:
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: vividvault.settings
.elasticbeanstalk/config.yml
branch-defaults:
main:
environment: vividvault-env
global:
application_name: vividvault-project
default_platform: Python 3.11 running on 64bit Amazon Linux 2023
default_region: ap-south-1
profile: eb-cli
sc: git
workspace_type: Application
The Problem
When I SSH into the instance and open Django shell:
eb ssh
cd /var/app/current
source /var/app/venv/*/bin/activate
python3 manage.py shell
Then check:
import os
print("RDS_DB_NAME in env:", 'RDS_DB_NAME' in os.environ)
print(os.environ.get("RDS_DB_NAME"))
Output:
False
None
So Django does not see RDS_DB_NAME in os.environ, even though it is defined in Elastic Beanstalk configuration.
Because of this, Django enters the else block and uses SQLite instead of PostgreSQL.
Question
Why would Elastic Beanstalk show RDS environment variables in the console, but they are not available inside the running Django process (os.environ)?
Is this:
A container environment issue?
Related to
.ebextensions?Something specific to Amazon Linux 2023?
Requiring a full
eb rebuild?
What is the correct way to ensure RDS environment variables are properly injected into the Django app running on Elastic Beanstalk?
If more logs are needed, I can provide them.