Amazon DynamoDB
Developer Guide (API Version 2012-08-10)

Working with Local Secondary Indexes: PHP

You can use the AWS SDK for PHP Low-Level API to create a table with one or more local secondary indexes, describe the indexes on the table, and perform queries using the indexes. These operations map to the corresponding DynamoDB API. For more information, see PHP Code Samples.

The following are the common steps for table operations using the AWS SDK for PHP.

  1. Create an instance of the DynamoDB client.

  2. Provide the parameters for a DynamoDB operation, including any optional parameters.

  3. Load the response from DynamoDB into a local variable for your application.

Create a Table With a Local Secondary Index

Local secondary indexes must be created at the same time you create a table. To do this, use CreateTable and provide your specifications for one or more local secondary indexes.

The following PHP code snippet creates a table to hold information about songs in a music collection. The partition key is Artist and the sort key is SongTitle. A secondary index, AlbumTitleIndex, facilitates queries by album title. The table partition key and sort key, plus the index sort key, are the only attributes projected into the index.

require 'vendor/autoload.php';

$sdk = new Aws\Sdk([
    'region'   => 'us-west-2',
    'version'  => 'latest'

$dynamodb = $sdk->createDynamoDb();

$tableName = 'Music';

$result = $dynamodb->createTable([
    'TableName' => $tableName,
    'AttributeDefinitions' => [
        [ 'AttributeName' => 'Artist', 'AttributeType' => 'S' ],
        [ 'AttributeName' => 'SongTitle', 'AttributeType' => 'S' ],
        [ 'AttributeName' => 'AlbumTitle', 'AttributeType' => 'S'
    'KeySchema' => [
        [ 'AttributeName' => 'Artist', 'KeyType' => 'HASH' ],
        [ 'AttributeName' => 'SongTitle', 'KeyType' => 'RANGE' ]
    'LocalSecondaryIndexes' => [
            'IndexName' => 'AlbumTitleIndex',
            'KeySchema' => [
                [ 'AttributeName' => 'Artist', 'KeyType' => 'HASH' ],
                [ 'AttributeName' => 'AlbumTitle', 'KeyType' => 'RANGE' ]
            'Projection' => [
                'ProjectionType' => 'INCLUDE',
                'NonKeyAttributes' => ['Genre', 'Year']
    'ProvisionedThroughput' => [
        'ReadCapacityUnits' => 5,
        'WriteCapacityUnits' => 5

You must wait until DynamoDB creates the table and sets the table status to ACTIVE. After that, you can begin putting data items into the table.

Query a Local Secondary Index

You can use the Query operation on a local secondary index, in much the same way you Query a table. You need to specify the index name, the query criteria for the index sort key, and the attributes that you want to return. In this example, the index is AlbumTitleIndex and the index sort key is AlbumTitle.

The only attributes returned are those that have been projected into the index. You could modify this query to select non-key attributes too, but this would require table fetch activity that is relatively expensive. For more information about table fetches, see Attribute Projections


$response = $dynamodb->query([
    'TableName' => $tableName,
    'IndexName' => 'AlbumTitleIndex',
    'KeyConditionExpression' => 'Artist = :v_artist and AlbumTitle >= :v_title',
    'ExpressionAttributeValues' =>  [
        ':v_artist' => ['S' => 'Acme Band'],
        ':v_title' => ['S' => 'Songs About Life']
    'Select' => 'ALL_ATTRIBUTES'

echo "Acme Band's Songs About Life:\n";
foreach($response['Items'] as $item) {
    echo "   - " . $item['SongTitle']['S'] . "\n";