Sho*_*ora 3 amazon-dynamodb aws-lambda amazon-dynamodb-streams aws-cdk
我想使用 AWS CDK 在我的 lambda 上启用 DynamoDB 流,我可以这样做,但我也想在 lambda 上启用过滤条件
但我收到此错误:
过滤器模式定义无效。(服务:AWSLambda;状态代码:400;错误代码:InvalidParameterValueException
这是我从 DynamoDB 流中收到的事件:
{
"input": {
"Records": [
{
"eventID": "e92e0072a661a06df0e62e411f",
"eventName": "INSERT",
"eventVersion": "1.1",
"eventSource": "aws:dynamodb",
"awsRegion": "<region>",
"dynamodb": {
"ApproximateCreationDateTime": 1639500357,
"Keys": {
"service": {
"S": "service"
},
"key": {
"S": "key"
}
},
"NewImage": {
"service": {
"S": "service"
},
"channel": {
"S": "email"
},
"key": {
"S": "key"
}
},
"SequenceNumber": "711500000000015864417",
"SizeBytes": 168,
"StreamViewType": "NEW_IMAGE"
},
"eventSourceARN": "arn:aws:dynamodb:<region>:<account>:table/table-name/stream/2021-12-14T13:00:29.888"
}
]
},
"env": {
"lambdaContext": {
"callbackWaitsForEmptyEventLoop": true,
"functionVersion": "$LATEST",
"functionName": "functionName",
"memoryLimitInMB": "128",
"logGroupName": "/aws/lambda/functionName",
"logStreamName": "2021/12/14/[$LATEST]028531c7b489b8ec69bace700acc0",
"invokedFunctionArn": "arn:aws:lambda:<region>:<account>:function:functionName",
"awsRequestId": "c72e80252-4722-b9f0-a03b7f8b820e"
},
"region": "<region-name>"
}
}
Run Code Online (Sandbox Code Playgroud)
事件源映射代码为:
const mapping = new lambda.CfnEventSourceMapping(this, 'event', {
functionName: "functionName,
batchSize: 1,
bisectBatchOnFunctionError: true,
startingPosition: lambda.StartingPosition.TRIM_HORIZON,
eventSourceArn: <stream-arn>,
filterCriteria: filter,
});
Run Code Online (Sandbox Code Playgroud)
我想在此处获取事件名称INSERT和通道电子邮件。过滤条件的值应该是多少?它不适合我
fed*_*nev 13
不再需要原来的解决方法。CDK 现在具有适用于 Lambda、Kinesis 和 SQS 的事件源过滤器。将过滤器传递给 L2EventSourceMapping构造:
const source: EventSourceMapping = new lambda.EventSourceMapping(this, "EventSourceMapping",{
target: func,
eventSourceArn: table.tableStreamArn,
startingPosition: lambda.StartingPosition.TRIM_HORIZON,
filters: [
lambda.FilterCriteria.filter({
eventName: lambda.FilterRule.isEqual("INSERT"),
dynamodb: { NewImage: { channel: { S: lambda.FilterRule.isEqual("email") } },},
}),
],
}
);
Run Code Online (Sandbox Code Playgroud)
以下是Pattern带有channelof的新记录的 DynamoDB 流过滤器语法email:
`{ \"eventName\": [\"INSERT\"], \"dynamodb\": { \"NewImage\": {\"channel\": { \"S\" : [\"email\"]}} } }`
Run Code Online (Sandbox Code Playgroud)
换句话说,这Pattern是一个带有转义引号的字符串化 JSON过滤规则。该模式应用于每个流记录。
这是完整的 CDK 语法。代码以通常的 L2 开头EventSourceMapping。然后,它使用逃生舱口语法来设置FilterCriteria底层L1 CfnEventSourceMapping:
// start with the L2 type - Note: the OP code starts with a L1 `CfnEventSourceMapping`
const source: EventSourceMapping = new lambda.EventSourceMapping(this, 'EventSourceMapping', {
target: func,
eventSourceArn: table.tableStreamArn,
startingPosition: lambda.StartingPosition.TRIM_HORIZON,
});
// escape hatch - get a L1 reference
const cfnSouce = source.node.defaultChild as lambda.CfnEventSourceMapping;
cfnSouce.addPropertyOverride('FilterCriteria', {
Filters: [
{
Pattern: `{ \"eventName\": [\"INSERT\"], \"dynamodb\": { \"NewImage\": {\"channel\": { \"S\" : [\"email\"]}} } }`,
},
],
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5945 次 |
| 最近记录: |