Menu
AWS Elastic Beanstalk
Developer Guide (API Version 2010-12-01)

Using Amazon RDS with PHP

With Amazon Relational Database Service (Amazon RDS), you can quickly and easily provision and maintain a MySQL, Oracle, PostgreSQL, or Microsoft SQL Server instance in the cloud. This topic explains how you can use Amazon RDS and PHP with your Elastic Beanstalk application. For more information about Amazon RDS, go to http://aws.amazon.com/rds/.

To use Amazon RDS from your Elastic Beanstalk application, you need to do the following:

  1. Create an Amazon RDS DB instance.

  2. If you plan to use PDO, install the PDO drivers. For more information, go to http://www.php.net/manual/pdo.installation.php.

  3. Establish a database connection in your code by using the connectivity information for your Amazon RDS DB instance.

  4. Deploy your application to 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

Using a New Amazon RDS DB Instance with PHP

This topic walks you through creating a new Amazon RDS DB Instance and using it with your PHP application.

To use a new Amazon RDS DB Instance and PHP from your Elastic Beanstalk application

  1. Create an Amazon RDS DB Instance. You can create an RDS DB Instance in one of the following ways:

    • Create an Amazon RDS DB instance when you create a new application version. For instructions using the Elastic Beanstalk console, see Create an Application. For a sample walkthrough of a CakePHP application deployment with Amazon RDS, see Deploying a CakePHP Application to Elastic Beanstalk.

    • Create an Amazon RDS DB instance when you launch a new environment with an existing application version. For instructions using the AWS Elastic Beanstalk console, see Creating an AWS Elastic Beanstalk Environment.

    • If you already deployed an application to Elastic Beanstalk, you can create an Amazon RDS DB instance and attach it to an existing environment. For instructions using the AWS Elastic Beanstalk console, see Configuring Databases with Elastic Beanstalk. If you use the EB CLI, use the --database option when you run eb create.

  2. If you plan to use PDO, install the PDO drivers. For more information, go to http://www.php.net/manual/pdo.installation.php.

  3. 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.

    Example 1. Example using PDO to connect to an RDS database

    <?php
    $dbhost = $_SERVER['RDS_HOSTNAME'];
    $dbport = $_SERVER['RDS_PORT'];
    $dbname = $_SERVER['RDS_DB_NAME'];
    
    $dsn = "mysql:host={$dbhost};port={$dbport};dbname={$dbname}";
    $username = $_SERVER['RDS_USERNAME'];
    $password = $_SERVER['RDS_PASSWORD'];
    
    $dbh = new PDO($dsn, $username, $password);
    ?>

    For more information about constructing a connection string using PDO, go to http://us2.php.net/manual/en/pdo.construct.php.


    Example 2. Example using mysqli_connect() to connect to an RDS database

    $link = mysqli_connect($_SERVER['RDS_HOSTNAME'], $_SERVER['RDS_USERNAME'], $_SERVER['RDS_PASSWORD'], $_SERVER['RDS_DB_NAME'], $_SERVER['RDS_PORT']);						

    For more information on using mysqli_connect(), go to http://www.phpbuilder.com/manual/function.mysqli-connect.php.


  4. Deploy the updated application to your Elastic Beanstalk environment.

Using an Existing Amazon RDS DB Instance with PHP

You can update your application to use an Amazon RDS DB Instance that you have previously created. This topic walks you through how to update your PHP application using an existing Amazon RDS DB Instance and deploy your application to Elastic Beanstalk.

To use an existing Amazon RDS DB Instance and PHP from your Elastic Beanstalk application

  1. Create an 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 Create an Application. For a sample walkthrough of a CakePHP application deployment with Amazon RDS, see Deploying a CakePHP Application to 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 Creating an AWS Elastic Beanstalk Environment. You do not need to create an Amazon RDS DB instance with this environment because you already have an existing Amazon RDS DB instance.

  2. Configure your Amazon RDS DB security group to allow access from the Amazon EC2 security group used by your Elastic Beanstalk application. For instructions on how to find the name of your EC2 security group using the AWS Management Console, see 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.

  3. If you plan to use PDO, install the PDO drivers. For more information, go to http://www.php.net/manual/pdo.installation.php.

  4. 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-west-2.rds.amazonaws.com using port 3306, with the user name "sa" and the password "mypassword".

    Example 1. Example using PDO to connect to an RDS database

    <?php
    $dsn = 'mysql:host=mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com;port=3306;dbname=mydb';
    $username = 'sa';
    $password = 'mypassword';
    
    $dbh = new PDO($dsn, $username, $password);
    ?>

    For more information about constructing a connection string using PDO, go to http://us2.php.net/manual/en/pdo.construct.php.


    Example 2. Example using mysqli_connect() to connect to an RDS database

    $link = mysqli_connect('mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com', 'sa', 'mypassword', 'mydb', 3306);						

    For more information on using mysqli_connect(), go to http://www.phpbuilder.com/manual/function.mysqli-connect.php.


  5. Deploy the updated application to your Elastic Beanstalk environment.