aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.5k stars 3.85k forks source link

lambda-eventsources: DynamoDB, unable to filter for bool values #30734

Closed bpauwels closed 2 weeks ago

bpauwels commented 2 months ago

Describe the bug

When I add a lambda-eventsource to a dynamodb stream, I cannot filter for bool values because the .isEqual() function only allows string parameters.

    // filter only items with someValue:true
    fn.addEventSource(new lambda_events.DynamoEventSource(tbl, {
      startingPosition: lambda.StartingPosition.LATEST,
      filters: [
        lambda.FilterCriteria.filter({
          eventName: lambda.FilterRule.isEqual('INSERT'),
          dynamodb: {
            NewImage: {
              'someValue': { BOOL: lambda.FilterRule.isEqual(true) } // this is not possible
            }
          }
        })
      ]
    }));

Expected Behavior

Possibility to filter for all DynamoDB data types.

Current Behavior

.isEqual() only takes string parameters

Reproduction Steps

    const tbl = new ddb.Table(this, 'T', {
      partitionKey: {
        name: 'id',
        type: ddb.AttributeType.STRING
      },
      removalPolicy: cdk.RemovalPolicy.DESTROY,
      stream: ddb.StreamViewType.NEW_IMAGE
    });

    const fn = new lambda.Function(this, 'F', {
      runtime: lambda.Runtime.NODEJS_16_X,
      handler: 'index.handler',
      code: lambda.Code.fromInline(`exports.handler = function(event, context) {
        console.log(event);
        console.log(event.Records[0].dynamodb)
        return;
      }`),
    });

    // filter only items with someValue:true
    fn.addEventSource(new lambda_events.DynamoEventSource(tbl, {
      startingPosition: lambda.StartingPosition.LATEST,
      filters: [
        lambda.FilterCriteria.filter({
          eventName: lambda.FilterRule.isEqual('INSERT'),
          dynamodb: {
            NewImage: {
              'someValue': { BOOL: lambda.FilterRule.isEqual(true) }
            }
          }
        })
      ]
    }));

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.140.0

Framework Version

No response

Node.js Version

22.1.0

OS

MacOS 14.2.1

Language

TypeScript

Language Version

No response

Other information

No response

khushail commented 2 months ago

Hi @bpauwels , thanks for reaching out. I checked the passage of boolean value and you seem to be correct in saying it does not accept Boolean values. As can be seen in the code implementation, this function isEqual() accepts only string or numerical values.

https://github.com/aws/aws-cdk/blob/0875ef9df4396ab5cd6706aca11698987500e124/packages/aws-cdk-lib/aws-lambda/lib/event-source-filter.ts#L22

OperationalFallacy commented 2 months ago

What a luck, just tried to use filter for boolean and there is already a bug. 🍀

github-actions[bot] commented 2 weeks ago

Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.

github-actions[bot] commented 2 weeks ago

Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.