[aws-dynamodb] Creating a new Table using fromTableName returns a Table object that doesn't include secondary indexes
See original GitHub issueHello!
I’ve noticed some discrepancies between the behavior of a dynamodb.Table
created as new Table()
and ones that are created by using Table.fromTableName()
Using new Table(...).grantReadWriteData(myLambdaFunction)
delegates permissions to the primary table index, alongside all available GSIs.
However, using new Table.fromTableName(…).grantReadWriteData(myLambdaFunction)
seems to delegate permissions to the primary table index only.
Reproduction Steps
const table = new Table.fromTableName(this, TABLE_NAME, TABLE_NAME).grantReadWriteData(
myLambdaFunction
);
table.grantReadWrite(lambdaFn);
What did you expect to happen?
Lambda function should be granted permissions to ReadWrite
to all of the table’s indexes, both primary and LSIs/GSIs.
What actually happened?
Lambda function was granted permissions to ReadWrite
to the primary index only.
Environment
- CDK CLI Version : 1.69.0
- Framework Version: 1.69.0
- Node.js Version: 12.9.0
- OS : Linux
- Language (Version): TypeScript 3.6.4
Other
To workaround this issue one could explicitly define all partitions and then grant the necessary permissions to each, as such:
const primaryIndex = Table.fromTableName(this, "MyTable", "MyTable");
const secondaryIndex = Table.fromTableName(
this,
`MyTableGsi`,
`MyTable/index/Gsi-index`
);
primaryIndex.grantReadWriteData(lambdaFn);
secondaryIndex.grantReadWriteData(lambdaFn);
This is 🐛 Bug Report
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:10 (6 by maintainers)
Top GitHub Comments
Hello @alonjupiter ,
thanks for opening the issue.
Table.fromTableName()
does not, in fact, create a new Table, but allows you to refer to an already existing one. Because of that, you have to specify all details of the Table yourself. If the Table has indexes, you need to useTable.fromTableAttributes
method, and provide the indexes yourself.Thanks, Adam
Great stuff, thank you @skinny85!