Matt
Matt

Reputation: 123

Can't add secondary index for dynamodb in cdk using python

I am trying to create a dynamoDB table, with a secondary index with partition and sort key. I can create the table without the secondary index, but haven't been able to find a way yet to add the secondary index

I've looked at both of these resources, but haven't found anything that actually shows me what code i need in my cdk python script to create the resource with a secondary index https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-dynamodb.Table.html https://docs.aws.amazon.com/cdk/api/latest/docs/aws-dynamodb-readme.html

This is the code that will create the table

table_name = 'event-table-name'
    event_table = dynamoDB.Table(self, 'EventsTable',
         table_name=table_name,
         partition_key=Attribute(
             name='composite',
             type=AttributeType.STRING
         ),
         sort_key=Attribute(
             name='switch_ref',
             type=AttributeType.STRING
         ),
         removal_policy=core.RemovalPolicy.DESTROY,
         billing_mode=BillingMode.PAY_PER_REQUEST,
         stream=StreamViewType.NEW_IMAGE,
                                 )

and this is the secondary index I need to attach to it

secondaryIndex = dynamoDB.GlobalSecondaryIndexProps(
        index_name='mpan-status-index',
        partition_key=Attribute(
            name='field1',
            type=AttributeType.STRING
        ),
        sort_key=Attribute(
            name='field2',
            type=AttributeType.STRING
        ),
    )

I've tried adding the block inside the table creation and tried calling the addSecondaryindex method on the table. But both fail either saying unexpected keyword or object has no attribute addGlobalSecondaryIndex

Upvotes: 4

Views: 7915

Answers (4)

Isaac Kleinman
Isaac Kleinman

Reputation: 4392

aws_dynamodb.Table returns an ITable. To use the addGlobalSecondaryIndex, first cast to Table like so:

table = aws_dynamodb.Table(self, "Table",
    partition_key=dynamodb.Attribute(name="id", type=dynamodb.AttributeType.STRING)

aws_dynamodb.Table(table).add_global_secondary_index(...)

Upvotes: 0

lynkfox
lynkfox

Reputation: 2400

For anyone looking for this and stumbling on it through google search:

create your table with the usual:

from aws_cdk import aws_dynamodb as dynamodb
from aws_cdk.aws_dynamodb import Attribute, AttributeType, ProjectionType

table = dynamodb.Table(self, 'tableID',
                    partition_key=Attribute(name='partition_key', type = AttributeType.STRING))

then add your global secondary indexes in much the same way:

table.add_global_secondary_index( 
           partition_key=Attribute(name='index_hash_key', type=AttributeType.NUMBER),
           sort_key=Attribute(name='range_key', type=AttributeType.STRING),
           index_name='some_index')

you can add projection attributes with they kwarg arguments:

projection_type = ProjectionType.INCLUDE,
non_key_attributes= ['list', 'of', 'attribute','names']

and projection_type defaults to All if you don't include it.

I know the docs are incomplete in lots of areas, but this is found here:

https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_dynamodb/Table.html?highlight=add_global#aws_cdk.aws_dynamodb.Table.add_global_secondary_index

Upvotes: 3

piljoong
piljoong

Reputation: 381

addGlobalSecondaryIndex should be called on the Table class.

The code below (in typescript) works perfectly for me:

const table = new ddb.Table(this, "EventsTable", {
  tableName: "event-table-name",
  partitionKey: { name: 'composite', type: ddb.AttributeType.STRING },
  sortKey: { name: 'switch_ref', type: ddb.AttributeType.STRING },
  removalPolicy: cdk.RemovalPolicy.DESTROY,
  billingMode: BillingMode.PAY_PER_REQUEST,
  stream: StreamViewType.NEW_IMAGE
});
table.addGlobalSecondaryIndex({
  indexName: 'mpan-status-idex',
  partitionKey: { name: 'field1', type: ddb.AttributeType.STRING },
  sortKey:  { name: 'field2', type: ddb.AttributeType.STRING }
});

Upvotes: 8

Ashaman Kingpin
Ashaman Kingpin

Reputation: 1577

Have you tried using the addGlobalSecondaryIndex method as in

event_table.addGlobalSecondaryIndex({indexName: "...", partitionKey: "...", ...})

Take a look at the documentation for the method.

Upvotes: 2

Related Questions