I have the following configuration:
//DynamoDB Table
const table = new dynamodb.Table(this, `DataStore`, {
tableName: `${StackConfiguration.appName}-data-${StackConfiguration.environmentKey}`,
partitionKey: { name: 'scope', type: dynamodb.AttributeType.STRING },
sortKey: { name: 'id', type: dynamodb.AttributeType.STRING },
readCapacity: 5,
writeCapacity: 5,
removalPolicy: cdk.RemovalPolicy.DESTROY,
stream: StreamViewType.NEW_IMAGE
});
// Define Call Scaler Lambda
const callScalerLambda = new lambda.Function(this, 'CallLambdaHandler', {
description: 'Call Scaler Document Aggregate Lambda',
functionName: `${StackConfiguration.appName}-call-scaler-${StackConfiguration.environmentKey}`,
runtime: lambda.Runtime.PYTHON_3_8, // execution environment
handler: 'call-scaler/call_lambda.lambda_handler', // file is myLambda, function is lambda_handler
code: lambda.Code.fromAsset('lambda'), // code loaded from the "lambda" directory
role: myIAMRole,
environment: {
ENVIRONMENT: StackConfiguration.environmentKey,
SCALERENV: StackConfiguration.scalerEnvironment,
SYSTEMUSERNAME: StackConfiguration.systemUserName,
SYSTEMUSERPASS: StackConfiguration.systemUserPass
},
});
const DocumentAggregateCallScalerEventTrigger = new lambda.EventSourceMapping(this, 'DocumentEventTrigger', {
target:callScalerLambda,
// the properties below are optional
batchSize: 1,
enabled: true,
eventSourceArn: table.tableStreamArn,
startingPosition: lambda.StartingPosition.LATEST
});
I am trying to simply define an EventSourceTrigger via the DocumentAggregateCallScalerEventTrigger
and define the eventSourceArn
as the table's streamArn. However, I continue to get the following errors:
error 13-Dec-2021 15:23:39 FAIL test/stack.test.ts
error 13-Dec-2021 15:23:39 ● Test suite failed to run
error 13-Dec-2021 15:23:39
error 13-Dec-2021 15:23:39 [96mlib/stack.ts[0m:[93m206[0m:[93m7[0m - [91merror[0m[90m TS2322: [0mType 'string | undefined' is not assignable to type 'string'.
error 13-Dec-2021 15:23:39 Type 'undefined' is not assignable to type 'string'.
error 13-Dec-2021 15:23:39
error 13-Dec-2021 15:23:39 [7m206[0m eventSourceArn: table.tableStreamArn,
error 13-Dec-2021 15:23:39 [7m [0m [91m ~~~~~~~~~~~~~~[0m
error 13-Dec-2021 15:23:39
error 13-Dec-2021 15:23:39 [96mnode_modules/@aws-cdk/aws-lambda/lib/event-source-mapping.d.ts[0m:[93m9[0m:[93m14[0m
error 13-Dec-2021 15:23:39 [7m9[0m readonly eventSourceArn: string;
error 13-Dec-2021 15:23:39 [7m [0m [96m ~~~~~~~~~~~~~~[0m
error 13-Dec-2021 15:23:39 The expected type comes from property 'eventSourceArn' which is declared here on type 'EventSourceMappingProps'
How do I properly reference the table's StreamArn in the CDK?
CodePudding user response:
As far a Typescript is concerned, table.tableStreamArn
is an optional string (string | undefined
), so it's not letting you assign it to a required eventSourceArn
prop. In reality, table.tableStreamArn
will resolve to a string, because you have set the stream
prop on your table. But Typescript has no way of figuring this out on its own.
There are various ways to make Typescript happy, some more elegant than others: You can assert non-null with table.tableStreamArn!
, for instance, or force to a string table?.tableStreamArn ?? 'SHOULD NOT HAPPEN'
. Or
throw an error if it is undefined.
Another option would be add the event source right on the lambda, which avoids the whole issue:
import * as evtsrc from '@aws-cdk/aws-lambda-event-sources';
callScalerLambda.addEventSource(
new evtsrc.DynamoEventSource(table, {
batchSize: 1,
enabled: true,
startingPosition: lambda.StartingPosition.LATEST,
})
);