Creating a table using the AWS SDK for PHP

Now, let's understand how to create a DynamoDB table using the AWS SDK for PHP.

Getting ready…

You can use the IDE of your choice to code these recipes.

How to do it…

Let's start with creating a table called productTablePHP:

  1. Instantiate the DynamoDB client for PHP. Specify the AWS region in which you wish to create the table in:
    $client = DynamoDbClient::factory(array(
        'profile' => 'default',
        'region' => 'us-west-1'  
    ));
  2. Invoke the createTable method by specifying the details, such as the table name, hash and range keys, and provisioned capacity units. Here, we will create a table with the primary key as the composite hash and range keys:
    $result = $client->createTable(array(
        'TableName' => $tableName,
        'AttributeDefinitions' => array(
            array(
                'AttributeName' => 'id',
                'AttributeType' => 'N'
            ),
            array(
                'AttributeName' => 'type',
                'AttributeType' => 'S'
            )
        ),
        'KeySchema' => array(
            array(
                'AttributeName' => 'id',
                'KeyType' => 'HASH'
            ),
            array(
                'AttributeName' => 'name',
                'KeyType' => 'RANGE'
            )
        ),
        'ProvisionedThroughput' => array(
            'ReadCapacityUnits'    => 1,
            'WriteCapacityUnits' => 1
        )
    ));
  3. Invoking this would result in the creation of a table in a specified region of AWS. You can confirm this using the AWS console.

How it works…

Once we invoke these APIs, the AWS SDK creates the table with specified details.