[Python] Added DB instance of RDS with Elastic Beanstalk

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

Add a DB instance to your environment

  1. Select an AWS Region from the EB console
  2. Select the environment to use in the navigation pane
  3. Select a setting in the navigation pane
  4. Select Edit in Database Settings category
  5. Select the DB engine and enter your username and password
  6. Select Apply

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

Driver download

[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

Connection to DB

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

[Python] Added DB instance of RDS with Elastic Beanstalk
Getting Started with Python Basics of Python
The story of making a standard driver for db with python.
Life game with Python! (Conway's Game of Life)
10 functions of "language with battery" python
Implementation of Dijkstra's algorithm with python
Coexistence of Python2 and 3 with CircleCI (1.0)
Basic study of OpenCV with Python
Basics of binarized image processing with Python
Execute Python script with cron of TS-220
Passwordless authentication with RDS and IAM (Python)
Check the existence of the file with python
Algorithm learned with Python 8th: Evaluation of algorithm
Clogged with python update of GCP console ①
Easy introduction of speech recognition with Python
Save / load in-memory DB with python sqlite3
[python] Get a list of instance variables
UnicodeEncodeError struggle with standard output of python3
1. Statistics learned with Python 1-3. Calculation of various statistics (statistics)
Drawing with Matrix-Reinventor of Python Image Processing-
Recommendation of Altair! Data visualization with Python
Comparison of matrix transpose speeds with Python
Note: How to get the last day of the month with python (added the first day of the month)
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Prepare the execution environment of Python3 with Docker
Automatic operation of Chrome with Python + Selenium + pandas
Performance comparison of face detector with Python + OpenCV
[Python] limit axis of 3D graph with Matplotlib
2016 The University of Tokyo Mathematics Solved with Python
Algorithm learned with Python 13th: Tower of Hanoi
Color page judgment of scanned image with python
[Note] Export the html of the site with python.
Clogged with python update of GCP console ② (Solution)
Use multiple versions of python environment with pyenv
Check the date of the flag duty with Python
Solve A ~ D of yuki coder 247 with python
1. Statistics learned with Python 1-2. Calculation of various statistics (Numpy)
[Python] Get rid of dating with regular expressions
How to specify attributes with Mock of python
Poetry-virtualenv environment construction with python of centos-sclo-rh ~ Notes
Automating simple tasks with Python Table of contents
Implementation of TRIE tree with Python and LOUDS
Get CPU information of Raspberry Pi with Python
[Python] Summary of S3 file operations with boto3
Dump the contents of redis db with lua
Export RDS snapshot to S3 with Lambda (Python)
Convert the character code of the file with Python3
Static analysis of Python code with GitLab CI
One-liner that outputs 10000 digits of pi with Python
Continuation of multi-platform development with Electron and Python
[Python] Determine the type of iris with SVM
[Python] How to compare datetime with timezone added
[Blender x Python] Think of code with symbols
Two-dimensional elastic skeleton geometric nonlinear analysis with Python
Example of reading and writing CSV with Python
"Principle of dependency reversal" learned slowly with Python
Measure CPU temperature of Raspberry Pi with Python
[AWS] Development environment version that tried to build a Python environment with eb [Elastic Beanstalk]