Elastic Beanstalk Add an Amazon RDS DB instance to your Python application environment
If you are using RDS for the first time, use the EB Management Console to create a test environment with DB Instances (https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/create-deploy-python-rds. Add html # python-rds-create) and make sure the application can connect to that instance
To connect to the DB, the appropriate [driver] for your application (https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/create-deploy-python-rds.html#python-rds-drivers ), Load the driver into your code, and then use the environment properties provided in EB to create a connection object (https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/create- deploy-python-rds.html # python-rds-connect) Settings and connection codes depend on the DB engine and framework used
It takes about 10 minutes to add an EB instance Once the environment update is complete, the DB Instant host name and other connection information will be available to the application through the following environment properties:
Property name | Description | Property value |
---|---|---|
RDS_HOSTNAME |
DB instance host name | Amazon RDS console[Connection and security]tab:[end point] |
RDS_PORT |
Ports that the DB instance allows to connect The default value depends on the DB engine | Amazon RDS console[Connection and security]tab:[port] |
RDS_DB_NAME |
EB nameebdb |
Amazon RDS console[Setting]tab:[port] |
RDS_USETNAME |
Username set for the database | Amazon RDS console[Setting]tab:[Master user name] |
RDS_PASSWORD |
Password set for the database | Not available on Amazon RDS console |
For details on setting the internal DB instance, see Add DB to EB environment. reference
[Requirement file] of the project (https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/python-configuration-requirements.html) Add the DB driver Example) requirements.txt
mysqlclient==1.3.12
Python common driver package
MySL-python
(Python 2)、mysqlclient
(Python 3)psycopg2
cx_Oracle
adodbapi
EB provides connection information for the DB instance attached in the environment properties ʻOs.environ [‘VARIABLE’] to read properties and set up DB connection
Example) Django configuration file --DATABASE 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’],
}
}
Recommended Posts