| « PreviousNext » | |
![]() ![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
With Amazon Relational Database Service (Amazon RDS), you can quickly and easily provision and maintain a MySQL, Oracle, or Microsoft SQL Server instance in the cloud. This topic explains how you can use Amazon RDS and Python with your AWS Elastic Beanstalk application. For more information about Amazon RDS, go to http://aws.amazon.com/rds/.
To use Amazon RDS from your AWS Elastic Beanstalk application, you need to do the following:
Create an Amazon RDS DB Instance.
Establish a database connection in your code by using the connectivity information for your Amazon RDS DB Instance.
Update your requirements.txt file.
Deploy your application to AWS Elastic Beanstalk.
This topic walks you through the following:
Using a new Amazon RDS DB Instance with your application
Using an existing Amazon RDS DB Instance with your application
This topic walks you through creating a new Amazon RDS DB Instance and using it with your Python application.
To use a new Amazon RDS DB Instance and Python from your AWS Elastic Beanstalk application
Create an Amazon RDS DB Instance. You can create an RDS DB Instance in one of the following ways:
Create an RDS DB Instance when you create a new application version. For instructions using the AWS Elastic Beanstalk console, see Creating New Applications. For a sample walkthrough of a Django application deployment with Amazon RDS using eb, see Deploying a Django Application to AWS Elastic Beanstalk.
Create an RDS DB Instance when you launch a new environment with an existing application version. For instructions using the AWS Elastic Beanstalk console, see Launching New Environments.
If you already deployed an application to AWS Elastic Beanstalk, you can create an RDS DB Instance and
attach it to an existing environment. For instructions using the AWS Elastic Beanstalk console, see Configuring Databases with AWS Elastic Beanstalk. If you use eb, use
the eb init command to specify RDS configuration
settings, and then use the eb update command to update
your environment.
Establish a database connection in your code using your Amazon RDS DB Instance's connectivity information. You can access your connectivity information using environment variables. The following shows how you would connect to the database on an RDS instance.
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'],
}
}Create a requirements.txt file and place it in the top-level directory of your source bundle. A typical python application will have dependencies on other third-party Python packages. In Python, pip is the standard way of installing packages. Pip has a feature that allows you to specify all the packages you need (as well as their versions) in a single requirements file. For more information about the requirements file, go to Requirements files. The following is an example requirements.txt file for Django.
Django==1.4.1 MySQL-python==1.2.3
Deploy your updated application to your existing AWS Elastic Beanstalk environment. For information on how to deploy a new application version to an existing environment using the Elastic Beanstalk Console, see Step 4: Deploy New Version. If you are using eb, commit your changes and deploy your application. For an example, see Step 5: Update the Application.
You can update your Python application to use an Amazon RDS DB Instance that you have previously created. This topic walks you through how to update your Python application using an existing Amazon RDS DB Instance and deploy your application to AWS Elastic Beanstalk.
To use an existing Amazon RDS DB Instance and Python from your AWS Elastic Beanstalk application
Create an AWS Elastic Beanstalk environment in one of the following ways:
Create a new application with a new environment. For instructions using the Elastic Beanstalk console, see Creating New Applications. For a sample walkthrough of an Django application deployment with Amazon RDS using eb, see Deploying a Django Application to AWS Elastic Beanstalk. You do not need to create an RDS DB Instance with this environment because you already have an existing RDS DB Instance.
Launch a new environment with an existing application version. For instructions using the Elastic Beanstalk console, see Launching New Environments. You do not need to create an RDS DB Instance with this environment because you already have an existing RDS DB Instance.
Configure your Amazon RDS DB Security Group to allow access from the Amazon EC2 security group used by your AWS Elastic Beanstalk application. For instructions on how to find the name of your EC2 security group using the AWS Management Console, see Amazon EC2 Security Groups. For more information, go to the "Authorizing Network Access to an Amazon EC2 Security Group" section of Working with DB Security Groups in the Amazon Relational Database Service User Guide.
Establish a database connection in your code using your Amazon RDS DB Instance's connectivity information. The following examples show how you could connect to the database on an RDS instance at mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com using port 3306, with the user name "sa" and the password "mypassword".
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb',
'USER': 'sa',
'PASSWORD': 'mypwd',
'HOST': 'mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com',
'PORT': '3306',
}
}Create a requirements.txt file, add the package you need to communicate to the database, and place it in the top-level directory of your source bundle. For more information about the requirements file, go to Requirements files. The following is an example requirements.txt file.
MySQL-python==1.2.3
Deploy your updated application to your existing AWS Elastic Beanstalk environment. For information on how to deploy a new application version to an existing environment using the Elastic Beanstalk Console, see Step 4: Deploy New Version. If you are using eb, commit your changes and deploy your application. For an example, see Step 5: Update the Application.