Aws-cdk: Cannot use key attribute in one DynamoDB index as non-key attribute in another index

Created on 7 Oct 2019  路  4Comments  路  Source: aws/aws-cdk

I'm using the CDK (in Python) to create global secondary indexes in my DynamoDB table, and one secondary index has a key that's used as a non-key attribute in another index. I have no problems doing this manually in the DynamoDB console, but when I use CDK, I get an error because CDK seems to check the union of key attributes across all indexes against the union of non-key attributes across all indexes. I traced the problem to this commit, specifically these lines:

Was this behavior intentional? This seems to restrict what one can do with CDK vs DynamoDB directly.

Reproduction Steps

In the following snippet, bar is used as a non-key attribute in Index1 and as a key attribute in Index2.

my_table.add_global_secondary_index(
    index_name = "Index1",
    partition_key = aws_dynamodb.Attribute(
        name = "foo",
        type = aws_dynamodb.AttributeType.STRING
    ),
    projection_type = aws_dynamodb.ProjectionType.INCLUDE,
    non_key_attributes = ["bar"] 
)
my_table.add_global_secondary_index(
    index_name = "Index2",
    partition_key = aws_dynamodb.Attribute(
        name = "baz",
        type = aws_dynamodb.AttributeType.STRING
    ),
    sort_key = aws_dynamodb.Attribute(
        name = "bar",
        type = aws_dynamodb.AttributeType.STRING
    ),
    projection_type = aws_dynamodb.ProjectionType.INCLUDE,
    non_key_attributes = ["blah"] 
)

Error Log

Error: a key attribute, bar, is part of a list of non-key attributes, bar,blah, which is not allowed since all key attributes are added automatically and this configuration causes stack creation failure

Environment

  • CLI Version :
  • Framework Version:
  • OS : Linux
  • Language : Python

Other

This could probably be fixed by changing the addGlobalSecondaryIndex method in Table so the key and non-key attributes are only compared per index.


This is :bug: Bug Report

@aws-cdaws-dynamodb bug needs-reproduction p1

Most helpful comment

I have a workaround for Java (Kotlin, actually...), in case it's helpful to others:

table.addGSI {
    indexName("index-1")
    partitionKey(key1)
    sortKey(key2)
    projectionType(ProjectionType.INCLUDE)
    nonKeyAttributes(emptyList()) // we'll add these later to workaround a bug in CDK
}

table.addGSI {
    indexName("index-2")
    partitionKey(key3)
    sortKey(key4)
    projectionType(ProjectionType.INCLUDE)
    nonKeyAttributes(emptyList()) // we'll add these later to workaround a bug in CDK
}

// hack our way around this bug in CDK: https://github.com/aws/aws-cdk/issues/4398
val cfnTable = table.node.defaultChild as CfnTable
cfnTable.addPropertyOverride("GlobalSecondaryIndexes.0.Projection.NonKeyAttributes", listOf(
    "key3", "key4"
))
cfnTable.addPropertyOverride("GlobalSecondaryIndexes.1.Projection.NonKeyAttributes", listOf(
    "key1", "key2"
))

All 4 comments

I'm hitting the same issue in Java

I have a workaround for Java (Kotlin, actually...), in case it's helpful to others:

table.addGSI {
    indexName("index-1")
    partitionKey(key1)
    sortKey(key2)
    projectionType(ProjectionType.INCLUDE)
    nonKeyAttributes(emptyList()) // we'll add these later to workaround a bug in CDK
}

table.addGSI {
    indexName("index-2")
    partitionKey(key3)
    sortKey(key4)
    projectionType(ProjectionType.INCLUDE)
    nonKeyAttributes(emptyList()) // we'll add these later to workaround a bug in CDK
}

// hack our way around this bug in CDK: https://github.com/aws/aws-cdk/issues/4398
val cfnTable = table.node.defaultChild as CfnTable
cfnTable.addPropertyOverride("GlobalSecondaryIndexes.0.Projection.NonKeyAttributes", listOf(
    "key3", "key4"
))
cfnTable.addPropertyOverride("GlobalSecondaryIndexes.1.Projection.NonKeyAttributes", listOf(
    "key1", "key2"
))

I'm also hitting this bug using Typescript.

I'm facing this issue as well. Thankfully, @perihelion1's work-around helps me for now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pepastach picture pepastach  路  3Comments

PaulMaddox picture PaulMaddox  路  3Comments

v-do picture v-do  路  3Comments

mirazmamun picture mirazmamun  路  3Comments

ababra picture ababra  路  3Comments