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] tableStreamArn is undefined

See original GitHub issue

We’re creating a DynamoDB table with a stream. We’re trying to create a lambda.EventSourceMapping, passing in table.tableStreamArn as the eventSourceArn prop like so:

    new lambda.EventSourceMapping(this, props.serviceName + 'eventSourceMapping', {
      eventSourceArn: ddbTable.tableStreamArn,
      target: ddbStreamProcessor,
      startingPosition: lambda.StartingPosition.TRIM_HORIZON
    });

When we try to build (transpiling from TypeScript to JavaScript), we get the following error:

npm run build

> cdk@0.1.0 build /home/ec2-user/scm/app/cdk
> tsc

lib/cdk-stack.ts:63:7 - error TS2322: Type 'string | undefined' is not assignable to type 'string'.
  Type 'undefined' is not assignable to type 'string'.

63       eventSourceArn: ddbTable.tableStreamArn,
         ~~~~~~~~~~~~~~

  node_modules/@aws-cdk/aws-lambda/lib/event-source-mapping.d.ts:9:14
    9     readonly eventSourceArn: string;
                   ~~~~~~~~~~~~~~
    The expected type comes from property 'eventSourceArn' which is declared here on type 'EventSourceMappingProps'


Found 1 error.

Environment

  • CDK CLI Version: 1.56.0 (build c1c174d)
  • Module Version: 1.56.0
  • Node.js Version: v10.16.0
  • OS: all
  • Language (Version): TypeScript 3.7.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
RomainMullercommented, Aug 12, 2020

The tableStreamArn property is optional (meaning it can be undefined) because no stream will exist if the table was created without a stream property. This is perfectly normal and expected.

There are two things you can do to fix your code:

  1. If you’re certain the table was created with a stream property, use a ! to inform the typescript compiler that you know the value will not be undefined:
    eventSourceArn: ddbTable.tableStreamArn!,
    
  2. If you are obtaining an ITable from outside of your control and cannot guarantee it was created with a stream property, add guard for the case where no stream was created:
    if (ddbTable.tableStreamArn == null) {
       throw new Exception("No table stream is available on this DynamoDB table. It is required."); // Or whatever
    }
    // From here on, the typescript compiler **knows** `ddbTable.tableStreamArn` cannot be `undefined`
    // since an exception would have been thrown if that was the case. It won't complain about the incompatible
    // types in assignment anymore.
    
0reactions
skinny85commented, Feb 16, 2021

Hello!

When I say “directly”, I mean the same way my blog post that you mentioned is passing an S3 Bucket from the ProducingStack to the ConsumingStack.

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
class Table (construct) · AWS CDK
CUSTOMER_MANAGED and this property is undefined, a new KMS key will be created and associated with this table.) External KMS key to use...
Read more >
Aws Cdk Getting Dynamodb Stream Arn Returns Null - ADocLib
The tableStreamArn property is optional meaning it can be undefined because no stream will exist if the table was created without a stream....
Read more >
@aws-cdk/aws-dynamodb | Yarn - Package Manager
Amazon DynamoDB Construct Library. cfn-resources: Stable. cdk-constructs: Stable. Here is a minimal deployable DynamoDB table definition:
Read more >
awsdynamodb - Go Packages
Returns: The construct as a stack element or undefined if it is not a stack element ... such as `arn:aws:dynamodb:us-east-1:123456789012:table/testddbstack- ...
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