Amazon RDS examples using SDK for PHP - AWS SDK for PHP

Amazon RDS examples using SDK for PHP

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for PHP with Amazon RDS.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Scenarios are code examples that show you how to accomplish specific tasks by calling multiple functions within a service or combined with other AWS services.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Actions

The following code example shows how to use CreateDBInstance.

SDK for PHP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

require __DIR__ . '/vendor/autoload.php'; use Aws\Exception\AwsException; $rdsClient = new Aws\Rds\RdsClient([ 'region' => 'us-east-2' ]); $dbIdentifier = '<<{{db-identifier}}>>'; $dbClass = 'db.t2.micro'; $storage = 5; $engine = 'MySQL'; $username = 'MyUser'; $password = 'MyPassword'; try { $result = $rdsClient->createDBInstance([ 'DBInstanceIdentifier' => $dbIdentifier, 'DBInstanceClass' => $dbClass, 'AllocatedStorage' => $storage, 'Engine' => $engine, 'MasterUsername' => $username, 'MasterUserPassword' => $password, ]); var_dump($result); } catch (AwsException $e) { echo $e->getMessage(); echo "\n"; }

The following code example shows how to use CreateDBSnapshot.

SDK for PHP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

require __DIR__ . '/vendor/autoload.php'; use Aws\Exception\AwsException; $rdsClient = new Aws\Rds\RdsClient([ 'region' => 'us-east-2' ]); $dbIdentifier = '<<{{db-identifier}}>>'; $snapshotName = '<<{{backup_2018_12_25}}>>'; try { $result = $rdsClient->createDBSnapshot([ 'DBInstanceIdentifier' => $dbIdentifier, 'DBSnapshotIdentifier' => $snapshotName, ]); var_dump($result); } catch (AwsException $e) { echo $e->getMessage(); echo "\n"; }

The following code example shows how to use DeleteDBInstance.

SDK for PHP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

require __DIR__ . '/vendor/autoload.php'; use Aws\Exception\AwsException; //Create an RDSClient $rdsClient = new Aws\Rds\RdsClient([ 'region' => 'us-east-1' ]); $dbIdentifier = '<<{{db-identifier}}>>'; try { $result = $rdsClient->deleteDBInstance([ 'DBInstanceIdentifier' => $dbIdentifier, ]); var_dump($result); } catch (AwsException $e) { echo $e->getMessage(); echo "\n"; }

The following code example shows how to use DescribeDBInstances.

SDK for PHP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

require __DIR__ . '/vendor/autoload.php'; use Aws\Exception\AwsException; //Create an RDSClient $rdsClient = new Aws\Rds\RdsClient([ 'region' => 'us-east-2' ]); try { $result = $rdsClient->describeDBInstances(); foreach ($result['DBInstances'] as $instance) { print('<p>DB Identifier: ' . $instance['DBInstanceIdentifier']); print('<br />Endpoint: ' . $instance['Endpoint']["Address"] . ':' . $instance['Endpoint']["Port"]); print('<br />Current Status: ' . $instance["DBInstanceStatus"]); print('</p>'); } print(" Raw Result "); var_dump($result); } catch (AwsException $e) { echo $e->getMessage(); echo "\n"; }

Scenarios

The following code example shows how to create a web application that tracks work items in an Amazon Aurora Serverless database and uses Amazon Simple Email Service (Amazon SES) to send reports.

SDK for PHP

Shows how to use the AWS SDK for PHP to create a web application that tracks work items in an Amazon RDS database and emails reports by using Amazon Simple Email Service (Amazon SES). This example uses a front end built with React.js to interact with a RESTful PHP backend.

  • Integrate a React.js web application with AWS services.

  • List, add, update, and delete items in an Amazon RDS table.

  • Send an email report of filtered work items using Amazon SES.

  • Deploy and manage example resources with the included AWS CloudFormation script.

For complete source code and instructions on how to set up and run, see the full example on GitHub.

Services used in this example
  • Aurora

  • Amazon RDS

  • Amazon RDS Data Service

  • Amazon SES

Serverless examples

The following code example shows how to implement a Lambda function that connects to an RDS database. The function makes a simple database request and returns the result.

SDK for PHP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples repository.

Connecting to an Amazon RDS database in a Lambda function using PHP.

<?php # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 # using bref/bref and bref/logger for simplicity use Bref\Context\Context; use Bref\Event\Handler as StdHandler; use Bref\Logger\StderrLogger; use Aws\Rds\AuthTokenGenerator; use Aws\Credentials\CredentialProvider; require __DIR__ . '/vendor/autoload.php'; class Handler implements StdHandler { private StderrLogger $logger; public function __construct(StderrLogger $logger) { $this->logger = $logger; } private function getAuthToken(): string { // Define connection authentication parameters $dbConnection = [ 'hostname' => getenv('DB_HOSTNAME'), 'port' => getenv('DB_PORT'), 'username' => getenv('DB_USERNAME'), 'region' => getenv('AWS_REGION'), ]; // Create RDS AuthTokenGenerator object $generator = new AuthTokenGenerator(CredentialProvider::defaultProvider()); // Request authorization token from RDS, specifying the username return $generator->createToken( $dbConnection['hostname'] . ':' . $dbConnection['port'], $dbConnection['region'], $dbConnection['username'] ); } private function getQueryResults() { // Obtain auth token $token = $this->getAuthToken(); // Define connection configuration $connectionConfig = [ 'host' => getenv('DB_HOSTNAME'), 'user' => getenv('DB_USERNAME'), 'password' => $token, 'database' => getenv('DB_NAME'), ]; // Create the connection to the DB $conn = new PDO( "mysql:host={$connectionConfig['host']};dbname={$connectionConfig['database']}", $connectionConfig['user'], $connectionConfig['password'], [ PDO::MYSQL_ATTR_SSL_CA => '/path/to/rds-ca-2019-root.pem', PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => true, ] ); // Obtain the result of the query $stmt = $conn->prepare('SELECT ?+? AS sum'); $stmt->execute([3, 2]); return $stmt->fetch(PDO::FETCH_ASSOC); } /** * @param mixed $event * @param Context $context * @return array */ public function handle(mixed $event, Context $context): array { $this->logger->info("Processing query"); // Execute database flow $result = $this->getQueryResults(); return [ 'sum' => $result['sum'] ]; } } $logger = new StderrLogger(); return new Handler($logger);