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.Table.from_table_attributes().table_stream_arn returns null when passing table_name or arn

See original GitHub issue

aws_cdk.aws_dynamodb.Table class has attributes table_arn and table_stream_arn but when we are importing the already created table outside the cdk stack with static method from_table_attributes(self, id='dynamo-table', table_name='_tablename_') it returns ITable object which has value of table_arn already set but table_stream_arn value is not set and returns None.

Use Case

I am trying to create dynamodb trigger on already created table outside the cdk stack. For this I am importing value of table with from_table_attributes(self, id='dynamo-table', table_name='_tablename_') static method it returns ITable object which I am passing to aws-lambda-event-source.DynamoEventSource(Impotedtableobject, starting_position='aws_lambda.StartingPosition.TRIM_HORIZON').

DynamoEventSource class return error below error that streams needs to be created:-

_jsii.errors.JavaScriptError: 
  Error: DynamoDB Streams must be enabled on the table cdk-environment/notificationslkjfs
      at DynamoEventSource.bind (/tmp/jsii-kernel-Un4KOI/node_modules/@aws-cdk/aws-lambda-event-sources/lib/dynamodb.js:18:19)
      at _wrapSandboxCode (/home/ec2-user/environment/cdk-environment/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:7663:51)
      at Kernel._wrapSandboxCode (/home/ec2-user/environment/cdk-environment/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:8299:19)
      at ret._ensureSync (/home/ec2-user/environment/cdk-environment/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:7663:25)
      at Kernel._ensureSync (/home/ec2-user/environment/cdk-environment/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:8272:20)
      at Kernel.invoke (/home/ec2-user/environment/cdk-environment/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:7662:26)
      at KernelHost.processRequest (/home/ec2-user/environment/cdk-environment/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:7371:28)
      at KernelHost.run (/home/ec2-user/environment/cdk-environment/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:7311:14)
      at Immediate.setImmediate [as _onImmediate] (/home/ec2-user/environment/cdk-environment/.env/lib/python3.6/dist-packages/jsii/_embedded/jsii/jsii-runtime.js:7314:37)
      at runCallback (timers.js:705:18)
      at tryOnImmediate (timers.js:676:5)
      at processImmediate (timers.js:658:5)_

While the streams are actually created on table but it is not able to refer that. Now if I propogate the table_stream_arn value in from_table_attributes static method of dynamodb.Table class it works fine but since we are dealing with different environments in different aws account passing the table_stream_arn value hardcoded in code is not a good way, it should be referred through from_table_attributes method

Proposed Solution

Other

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
SchollSimoncommented, May 25, 2022

I think i developed a solution for our application, where we faced the exact same issue due to integration of a multiregion stack architecture, where we only create our global dynamob tables in one main stack.

For our application we created a helper method which creates or returns an existing table, also with stream configuration

export const createDataTable = (
  scope: Construct,
  props: TableProps,
  indexConfigs?: GlobalSecondaryIndexProps[]
): ITable => {

In the method we check the region of our scope, if it is the main region (us-east-1) we only create the database instance

if(Stack.of(scope).region === 'us-east-1'){
  const newTable = new Table(scope, props.tableName, {
    // props contains -> stream: StreamViewType.NEW_AND_OLD_IMAGES,
    ...props,
    replicationRegions: ['eu-central-1'],
  });

  if (ndexConfigs.length > 0) {
    indexConfigs.forEach((indexConfig) =>
      newTable.addGlobalSecondaryIndex(indexConfig)
  );
}

if we create the eu stack or any other regional stack we want to refer the replicated table and not create a new one. But due to the inssufficient support from cdk, we have to do an AWS SDK Call during deployment. We can use dynamoDbs listStreams operation for listing and reading all the available stream configurations for one table.

else {
  const awsSdkCall: AwsSdkCall = {
    service: 'DynamoDBStreams',
    action: 'listStreams',
    region: Stack.of(scope).region,
    physicalResourceId: PhysicalResourceId.of(`${props.tableName}ListStreams` ),
    parameters: {
      TableName: props.tableName,
    },
  };

  const call = new AwsCustomResource(scope,`${props.tableName}GetTableStreams`, {
      onCreate: awsSdkCall,
      onUpdate: awsSdkCall,
      logRetention: RetentionDays.ONE_DAY,
      policy: AwsCustomResourcePolicy.fromStatements([
        new PolicyStatement({
          actions: ['dynamodb:*'],
          resources: ['*'],
        }),
      ]),
    }
  );
  return Table.fromTableAttributes(scope, props.tableName, {
    tableName: props.tableName,
    tableStreamArn: call.getResponseField('Streams.0.StreamArn'),
  });
}

You can have a look at the doc strings how to access the sdk calls output. Obviously you need to know how many stream configurations where created in the first place. But like you see with a CustomResource it is possible to keep this approach dynamic.

Anyway in my opinion cdk should offer this possibility out of the box.

3reactions
RomainMullercommented, Aug 13, 2020

The more I think about it the more I think this would be building a 🦶🏻🔫. I’ll be closing this issue off as I don’t think this is something we should be doing (for the reasons I mentioned in the previous update).

Read more comments on GitHub >

github_iconTop Results From Across the Web

class Table (construct) · AWS CDK - AWS Documentation
The stack in which this resource is defined. tableArn, string, Arn of the dynamodb table. tableName, string, Table name of the dynamodb table....
Read more >
AWS CDK getting DynamoDB Stream ARN returns null
DynamoEventSource on the imported table), you must use the Table.fromTableAttributes method and the tableStreamArn property must be populated.".
Read more >
create a dynamodb table in cdk with java - You.com
Create an instance of the DeleteTableRequest class and provide the table name that you want to delete. Run the deleteTable method by providing...
Read more >
@aws-cdk/aws-dynamodb - npm
fromTableAttributes factory method. This method accepts table name or table ARN which describes the properties of an already existing table:.
Read more >
AWS DynamoDB Streams — Change Data Capture for ...
How to enable the change stream for a DynamoDB table? What is the composition of the change events? How read and process a...
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