在 Amazon CDK 中引用 TableStreamArn
Referencing TableStreamArn in Amazon CDK
我有以下配置:
//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
});
我试图通过 DocumentAggregateCallScalerEventTrigger
简单地定义一个 EventSourceTrigger,并将 eventSourceArn
定义为 table 的 streamArn。但是,我继续收到以下错误:
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'
如何在 CDK 中正确引用 table 的 StreamArn?
就 Typescript 而言,table.tableStreamArn
是一个可选字符串 (string | undefined
),因此它不允许您将其分配给必需的 eventSourceArn
属性。实际上,table.tableStreamArn
将解析为一个字符串,因为您已经在 table 上设置了 stream
属性。但是 Typescript 无法自行解决这个问题。
有多种方法可以使 Typescript 快乐,有些方法比其他方法更优雅:例如,您可以使用 table.tableStreamArn!
断言非空,或者强制字符串 table?.tableStreamArn ?? 'SHOULD NOT HAPPEN'
。要么
如果未定义则抛出错误。
另一种选择是在 lambda 上添加事件源,这样可以避免整个问题:
import * as evtsrc from '@aws-cdk/aws-lambda-event-sources';
callScalerLambda.addEventSource(
new evtsrc.DynamoEventSource(table, {
batchSize: 1,
enabled: true,
startingPosition: lambda.StartingPosition.LATEST,
})
);
我有以下配置:
//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
});
我试图通过 DocumentAggregateCallScalerEventTrigger
简单地定义一个 EventSourceTrigger,并将 eventSourceArn
定义为 table 的 streamArn。但是,我继续收到以下错误:
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'
如何在 CDK 中正确引用 table 的 StreamArn?
就 Typescript 而言,table.tableStreamArn
是一个可选字符串 (string | undefined
),因此它不允许您将其分配给必需的 eventSourceArn
属性。实际上,table.tableStreamArn
将解析为一个字符串,因为您已经在 table 上设置了 stream
属性。但是 Typescript 无法自行解决这个问题。
有多种方法可以使 Typescript 快乐,有些方法比其他方法更优雅:例如,您可以使用 table.tableStreamArn!
断言非空,或者强制字符串 table?.tableStreamArn ?? 'SHOULD NOT HAPPEN'
。要么
如果未定义则抛出错误。
另一种选择是在 lambda 上添加事件源,这样可以避免整个问题:
import * as evtsrc from '@aws-cdk/aws-lambda-event-sources';
callScalerLambda.addEventSource(
new evtsrc.DynamoEventSource(table, {
batchSize: 1,
enabled: true,
startingPosition: lambda.StartingPosition.LATEST,
})
);