question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[aws-dynamodb] Creating a new Table using fromTableName returns a Table object that doesn't include secondary indexes

See original GitHub issue

Hello!

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:open
  • Created 3 years ago
  • Reactions:3
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
skinny85commented, Nov 15, 2020

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 use Table.fromTableAttributes method, and provide the indexes yourself.

Thanks, Adam

2reactions
alonjupitercommented, Nov 18, 2020

Great stuff, thank you @skinny85!

Read more comments on GitHub >

github_iconTop Results From Across the Web

class Table (construct) · AWS CDK
The read capacity for the table. Careful if you add Global Secondary Indexes, as those will share the table's provisioned throughput.
Read more >
@aws-cdk/aws-dynamodb - npm
Start using @aws-cdk/aws-dynamodb in your project by running `npm i ... mode where the table and global secondary indexes have configured ...
Read more >
create-table — AWS CLI 2.9.9 Command Reference
If you want to create multiple tables with secondary indexes on them, you must create ... The partition key of an item is...
Read more >
Exploring AWS DynamoDB - Andrew Jarombek
In addition to primary keys, DynamoDB tables can have secondary indexes. ... A global secondary index creates an index with a partition key ......
Read more >
Local Secondary Indexes - DynamoDB, explained.
which will return a description of your table. Then, let's recreate the table: $ aws dynamodb create-table \ --table-name UserOrdersTable \ --attribute- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found