在 Amazon CDK 中引用 TableStreamArn
Posted
技术标签:
【中文标题】在 Amazon CDK 中引用 TableStreamArn【英文标题】:Referencing TableStreamArn in Amazon CDK 【发布时间】:2022-01-17 21:14:14 【问题描述】:我有以下配置:
//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
定义为表的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 中正确引用表的 StreamArn?
【问题讨论】:
【参考方案1】:就 Typescript 而言,table.tableStreamArn
是一个可选字符串 (string | undefined
),因此它不允许您将其分配给必需的 eventSourceArn
属性。实际上,table.tableStreamArn
将解析为字符串,因为您已在表格上设置了 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,
)
);
【讨论】:
以上是关于在 Amazon CDK 中引用 TableStreamArn的主要内容,如果未能解决你的问题,请参考以下文章
如何创建在 Python CDK 中引用自身的 API Gateway 资源策略?