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.
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: 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
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
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.
Most helpful comment
I have a workaround for Java (Kotlin, actually...), in case it's helpful to others: