4

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

4 Answers 4

8

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 }
});
Sign up to request clarification or add additional context in comments.

3 Comments

I was looking for a python solution. But thinking python simply isn't supported properly and i may need to simply use the typescript version
@Matt - Simply saying that Python uses underscore naming convention, so you have to use add_global_secondary_index(), not addGlobalSecondaryIndex(). It works well.
Way necro comment here - but it is worth noting that there is no language that CDK is in that 'isnt supported properly'. All sub languages outside of Typescript use a custom AWS interpreter to compile into TypesScript - which is why you can get Type errors from node in Python CDK and a custom construct can be written in any language and distributed for any language user of CDK to use. of course there can be bugs in that process, but regardless - the python version of CDK is derived directly from the Typescript version.
3

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

Comments

2

Have you tried using the addGlobalSecondaryIndex method as in

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

Take a look at the documentation for the method.

1 Comment

This simply returns AttributeError: 'Table' object has no attribute 'addGlobalSecondaryIndex
0

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(...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.