aws-lambda - Lambda 函数中的过滤条件

标签 aws-lambda amazon-dynamodb aws-cdk amazon-dynamodb-streams

我想使用 AWS CDK 在我的 lambda 上启用 DynamoDB 流,我可以这样做,但我也想启用 filter criteria在 lambda 上

但是我收到了这个错误:

Invalid filter pattern definition. (Service: AWSLambda; Status Code: 400; Error Code: 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>"
    }
}

事件源映射代码为:

const mapping = new lambda.CfnEventSourceMapping(this, 'event', {
  functionName: "functionName,
  batchSize: 1,
  bisectBatchOnFunctionError: true,
  startingPosition: lambda.StartingPosition.TRIM_HORIZON,
  eventSourceArn: <stream-arn>,
  filterCriteria: filter,
});

我想让 eventName 为 INSERT, channel 为 email here。过滤条件的值应该是多少?它对我不起作用

最佳答案

CDK filter helpers 添加到 v2.42.0

不再需要原来的解决方法。 CDK 现在有事件源 filters用于 Lambda、Kinesis 和 SQS。将过滤器传递给 L2 EventSourceMapping构造:

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") } },},
      }),
    ],
  }
);


这是 DynamoDB 流过滤器 Pattern带有 channel 的新记录的语法的 email :

`{ \"eventName\": [\"INSERT\"], \"dynamodb\": { \"NewImage\": {\"channel\": { \"S\" : [\"email\"]}} } }`

换句话说,Pattern是一个字符串化的 JSON filter rule带有转义引号。该模式应用于每个流记录。

这是完整的 CDK 语法。代码以通常的 L2 EventSourceMapping 开头.然后它使用 escape hatch设置 FilterCriteria 的语法在 underlying L1CfnEventSourceMapping :

// 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\"]}} } }`,
    },
  ],
});

关于aws-lambda - Lambda 函数中的过滤条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70363945/

相关文章:

amazon-web-services - 为多个 lambda 生成一个 API 网关 - 以自动化方式

amazon-web-services - 提供的路由键格式不适合 HTTP 协议(protocol)

java - 通过对排序键执行 IN 操作 (begins_with (condition-expression )) 来查询 dynamoDB

python - Asyncpg 和 AWS Lambda

c# - AWS LAMBDA 使用 SqlConnection 连接到 RDS SQL 数据库

node.js - dynamodb 从 Nodejs 循环中更新插入

amazon-web-services - 由于命名约定,使用 DynamoDB AWS SDK [No Mapping for Hash Key] 时出错

amazon-web-services - AWS Cloudformation - Stack 与 NestedStack

amazon-web-services - CDK 部署在本地需要几秒钟,在 CI 中需要 >5 分钟

Typescript - AWS CDK 启用 CORS