This guide focuses on the AWS SDK for PHP client for Amazon Redshift. This guide assumes that you have already downloaded and installed the AWS SDK for PHP. See Installation for more information on getting started.
First you need to create a client object using one of the following techniques.
The easiest way to get up and running quickly is to use the Aws\Redshift\RedshiftClient::factory() method and provide your credential profile (via the profile option), which identifies the set of credentials you want to use from your ~/.aws/credentials file (see Using the AWS credentials file and credential profiles).
A region parameter is also required and must be set to one of the following values: us-east-1, ap-northeast-1, ap-southeast-1, ap-southeast-2, us-west-2, eu-west-1
use Aws\Redshift\RedshiftClient;
$client = RedshiftClient::factory(array(
'profile' => '<profile in your aws credentials file>',
'region' => '<region name>'
));
You can provide your credential profile like in the preceding example, specify your access keys directly (via key and secret), or you can choose to omit any credential information if you are using AWS Identity and Access Management (IAM) roles for EC2 instances or credentials sourced from the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.
Note
The profile option and AWS credential file support is only available for version 2.6.1 of the SDK and higher. We recommend that all users update their copies of the SDK to take advantage of this feature, which is a safer way to specify credentials than explicitly providing key and secret.
A more robust way to connect to Amazon Redshift is through the service builder. This allows you to specify credentials and other configuration settings in a configuration file. These settings can then be shared across all clients so that you only have to specify your settings once.
use Aws\Common\Aws;
// Create a service builder using a configuration file
$aws = Aws::factory('/path/to/my_config.json');
// Get the client from the builder by namespace
$client = $aws->get('Redshift');
The primary resource in Amazon Redshift is the cluster. To create a cluster you will use the CreateCluster operation. There are several parameters you can send when creating a cluster, so please refer to the API docs to determine which parameters to use. The following is basic example.
$client->createCluster(array(
'ClusterIdentifier' => 'your-unique-cluster-id',
'ClusterType' => 'multi-node',
'MasterUsername' => 'yourusername',
'MasterUserPassword' => 'Y0urP@$$w0rd',
'NodeType' => 'dw.hs1.xlarge',
'NumberOfNodes' => 2,
));
After the CreateCluster operation is complete, the record for your cluster will exist, but it will still take some time before the cluster is actually available for use. You can describe your cluster to check it's status.
$result = $client->describeClusters(array(
'ClusterIdentifier' => 'your-unique-cluster-id',
));
$clusters = $result->get('Clusters');
$status = $clusters['ClusterStatus'];
If you would like your code to wait until the cluster is available, you can use the the ClusterAvailable waiter.
$client->waitUntil('ClusterAvailable', array(
'ClusterIdentifier' => 'your-unique-cluster-id',
));
Warning
It can take over 20 minutes for a cluster to become available.
You can also take snapshots of your cluster with the CreateClusterSnapshot operation. Snapshots can take a little time before they become available as well, so there is a corresponding SnapshotAvailable waiter.
$client->createClusterSnapshot(array(
'ClusterIdentifier' => 'your-unique-cluster-id',
'SnapshotIdentifier' => 'your-unique-snapshot-id',
));
$client->waitUntil('SnapshotAvailable', array(
'SnapshotIdentifier' => 'your-unique-snapshot-id',
));
Amazon Redshift records events that take place with your clusters and account. These events are available for up to 14 days and can be retrieved via the DescribeEvents operation. Only 100 events can be returned at a time, so using the SDK's iterators feature allows you to easily fetch and iterate over all the events in your query without having to manually send repeated requests. The StartTime and EndTime parameters can take any PHP date string or DateTime object.
$events = $client->getIterator('DescribeEvents', array(
'StartTime' => strtotime('-3 days'),
'EndTime' => strtotime('now'),
));
foreach ($events as $event) {
echo "{$event['Date']}: {$event['Message']}\n";
}
Please see the Amazon Redshift Client API reference for a details about all of the available methods, including descriptions of the inputs and outputs.