Does anyone know if it's possible to access the ne...
# general
r
Does anyone know if it's possible to access the new DynamoDB streams filtering using CDK? I.e. this I can't see reference to it in the docs for latest v1 or v2 but may be missing something.
f
lemme check if CDK has support for it
Hasn’t been supported by CDK yet
Lemme c how to override the CFN resource
r
Thanks Frank, that's what I thought. Maybe it's time for me to learn how to build a custom construct
f
putthing something together.. gimme 1 sec
This could be helpful
f
Thanks @Kujtim Hoxha I’m doing exactly that haha
k
Awesome
f
Aight @Ross Coundon here’s the workaround to update CFN directly
Copy code
import { CfnResource } from "@aws-cdk/core";
import { CfnEventSourceMapping } from "@aws-cdk/aws-lambda";

// Create table
const table = new sst.Table(this, "Table", {
  ...,
  stream: true,
  consumers: {
    consumerA: "src/lambda.main",
  },
});

// The "AWS::Lambda::EventSourceMapping" is created as a child construct of "AWS::Lambda::Function", let's find it
const eventSource = table.getFunction("consumerA")?.node.children.find(
  child => (child.node.defaultChild as CfnResource)?.cfnResourceType === "AWS::Lambda::EventSourceMapping"
);

// Add filter to the CFN resource directly
const cfnEventSource = eventSource?.node.defaultChild as CfnEventSourceMapping;
cfnEventSource?.addPropertyOverride("FilterCriteria", {"Filters": [{"Pattern": "{ \"body\" : { \"age\": [ { \"numeric\": [ \"=\", 27 ] } ]}}"}]})
You should see this block added to the CFN template
Copy code
"FilterCriteria": {
          "Filters": [
            {
              "Pattern": "{ \"body\" : { \"age\": [ { \"numeric\": [ \"=\", 27 ] } ]}}"
            }
          ]
        }
Give it a try!
r
Wow, that was FAST!
Thank you!
k
Huge help for me here! Adding an example for filtering by PK/SK suffixes - useful if you are using overloaded indexes
Copy code
Filters: [
  {
    Pattern: JSON.stringify({
      dynamodb: {
        Keys: {
          PK: { 
            S: [
              { prefix: "PK-prefix" }
            ]
          },
          SK: {
            S: [
              { prefix: "SK-prefix" }
            ]
          },
        },
      },
    }),
  },
]
Docs for Lambda event filtering syntax (linked in the above blog post) https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html#filtering-syntax