AWS Elastic Beanstalk
Developer Guide

Adding an Amazon RDS DB Instance to Your Python Application Environment

You can use an Amazon Relational Database Service (Amazon RDS) DB instance to store data gathered and modified by your application. The database can be attached to your environment and managed by Elastic Beanstalk, or created and managed externally.

If you are using Amazon RDS for the first time, add a DB instance to a test environment with the Elastic Beanstalk Management Console and verify that your application can connect to it.

To connect to a database, add the driver to your application, load the driver in your code, and create a connection object with the environment properties provided by Elastic Beanstalk. The configuration and connection code vary depending on the database engine and framework that you use.


For learning purposes or test environments, you can use Elastic Beanstalk to add a DB instance.

For production environments, you can create a DB instance outside of your Elastic Beanstalk environment to decouple your environment resources from your database resources. This way, when you terminate your environment, the DB instance isn’t deleted. An external DB instance also lets you connect to the same database from multiple environments and perform blue-green deployments. For instructions, see Using Elastic Beanstalk with Amazon Relational Database Service.

Adding a DB Instance to Your Environment

To add a DB instance to your environment

  1. Open the Elastic Beanstalk console.

  2. Navigate to the management page for your environment.

  3. Choose Configuration.

  4. In the Database configuration category, choose Modify.

  5. Choose a DB engine, and enter a user name and password.

  6. Choose Apply.

Adding a DB instance takes about 10 minutes. When the environment update is complete, the DB instance's hostname and other connection information are available to your application through the following environment properties:

  • RDS_HOSTNAME – The hostname of the DB instance.

    Amazon RDS console label – Endpoint (this is the hostname)

  • RDS_PORT – The port on which the DB instance accepts connections. The default value varies among DB engines.

    Amazon RDS console label – Port

  • RDS_DB_NAME – The database name, ebdb.

    Amazon RDS console label – DB Name

  • RDS_USERNAME – The user name that you configured for your database.

    Amazon RDS console label – Username

  • RDS_PASSWORD – The password that you configured for your database.

For more information about configuring an internal DB instance, see Adding a Database to Your Elastic Beanstalk Environment.

Downloading a Driver

Add the database driver to your project's requirements file.

Example requirements.txt – Django with MySQL

Django==2.0.3 mysqlclient==1.3.12

Common Driver Packages for Python

  • MySQLMySQL-python (Python 2) , mysqlclient (Python 3)

  • PostgreSQLpsycopg2

  • Oraclecx_Oracle

  • SQL Serveradodbapi

Connecting to a Database

Elastic Beanstalk provides connection information for attached DB instances in environment properties. Use os.environ['VARIABLE'] to read the properties and configure a database connection.

Example Django Settings File – DATABASES Dictionary

import os if 'RDS_HOSTNAME' in os.environ: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', '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'], } }