-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@aws-cdk/aws-lambda-event-sources: Lambda event filtering #17874
Comments
In addition to the classes up above, do we also need to add a field to Additionally, do we need an api designed for filter patterns? I don't really want to inline the json into an event source - I would much rather prefer to be able to express something like "give me all record creation events from on the ddb stream" natively in my chosen cdk laguage; as opposed to "apply this filter json to my ddb stream". |
As long as this issue is open: I wrote a blog post on how to do this for filtering lambda events. @dgoetsch I think so. |
@zehsor Can i ask why the Medium article seems to contain repeated (and non related) code samples? I thought i was because i was not a member - so i joined and no luck? |
@kcliffe I don't really understand what you mean by non related code samples. I just added all the different steps that i did in order to set up a complete cdk sample project. The filtering part takes places in the cdk stack (Building the stack is the headline). |
@zehsor Sorry, i'm not trying to hijack this issue, it's just that the blog article in it's current form is completely useless as a reference to what you did since none of the code samples are visible? Here is a png of what i'm seeing FYI but i take your point - the issue is probably with Medium so i'll try to discuss it with them... |
Hey @zehsor ; I dropped the ball and never responded to you. I think you're point above is fair: an expressive, fluent api is definitely preceded by a more literal integration. In that case, I think the first step is probably just to implement syntax for Filters (i.e. a Filter object with specific fields vs some arbitrary json). |
While it may be nice, I'm not sure a fluent api or similar is really necessary for this feature to be implemented. According to the lambda event filtering docs
There is an EventBridge event pattern interface in the CDK, typed to I propose event filtering for lambda is implemented in the same way, and I am happy to contribute those changes? |
+1 |
@Dilski I am happy to talk through any proposed APIs here. My suggestion is to look at sns subscription filters as prior art for something similar: https://github.com/aws/aws-cdk/tree/master/packages/%40aws-cdk/aws-sns#filter-policy. Regarding fluent APIs, I think as long as we allow users to drop down to basic json, we should be able to try and model a higher level API as well. If we don't want to start with everything at once, we can also try something like a |
@iRoachie i'm responding to your comment here so we consolidate things in one place. i'd be happy to take a community contribution on this and a potential PR should have an API similar to the sns subscription filter linked above. I think that API roughly follows what we're trying to achieve here. I'm not advocating for the eventbridge API typed that's typed So something like this: fn.addEventSource(new sources.DynamoEventSource(table, {
startingPosition: lambda.StartingPosition.TRIM_HORIZON,
batchSize: 5,
filterPatterns: [{
price: lambda.SourceFilter.numericFilter({
between: { start: 100, stop: 200 },
}),
}, {
size: lambda.SourceFilter.stringFilter({
denylist: ['small', 'medium'], // becomes 'anything-but'
}),
}, {
whatever: new lambda.SourceFilter(/* can put in your custom json here */),
}],
}) I think we give @wtho another week to indicate that they intend to contribute this. After that, this feature is fair game :). |
Alright sounds good. I'll check back in a week then. Happened to need this at work so that's why I'm interested 😄 |
@iRoachie feel free to take over. I won't have too much time over the next few weeks to tackle this. |
I can give this a go this week |
@kaizencc @wtho I've been playing around with this as the API. Would love to hear thoughts const patternExample: FilterPattern = {
"arbitrary": {
"depth": {
"of-keys": {
"nullField": FilterExpression.isNull(),
"emptyField": FilterExpression.isEmpty(),
"equalsField": FilterExpression.equals("some value"),
"equalsFieldNullSupported": FilterExpression.equals(null),
"equalsAnyField": FilterExpression.equalsAnyOf("valuea", "valueb"),
"orField": FilterExpression.or(
FilterExpression.equals("valuea"),
FilterExpression.equals("valueb")
),
"notEqualsField": FilterExpression.not(
FilterExpression.equals("my value")
),
"notEqualsAnyOfField": FilterExpression.not(
FilterExpression.equalsAnyOf("not valuea", "not valueb")
),
"numericField": FilterExpression.numeric([{operator: NumericOperator.EQ, number: 4.20}]),
"existsField": FilterExpression.exists(),
"beginsWithField": FilterExpression.beginsWith("begins-"),
"moreComplexOrField": FilterExpression.or(
FilterExpression.equals(170),
FilterExpression.numeric([{operator: NumericOperator.LT, number: 20}])
),
}
}
}
} Which outputs the filter expression: {
"arbitrary": {
"depth": {
"of-keys": {
"nullField": [null],
"emptyField": [""],
"equalsField": ["some value"],
"equalsFieldNullSupported": [null],
"equalsAnyField": ["valuea", "valueb"],
"orField": ["valuea", "valueb"],
"notEqualsField": [{"anything-but": ["my value"]}],
"notEqualsAnyOfField": [{"anything-but": ["not valuea", "not valueb"]}],
"numericField": [{"numeric": ["=", 4.2]}],
"existsField": [{"exists": true}],
"beginsWithField": [{"prefix": "begins-"}],
"moreComplexOrField": [ 170, {"numeric": ["<", 20]}]
}
}
}
} The filter pattern is typed like |
Any updates? |
I'm was hoping to get some feedback on my above comment before doing more |
@Dilski I think it's better to discuss it in a PR in detail, but as far as I remember the keys are not as flexible in jsii. Try to construct these objects in C# or Java and you'll quickly realize the type system there is not so good in building these flexible definitions. But I'm sure @kaizencc can give better feedback on how to work in the limitations jsii. Look at the other aws-cdk APIs to learn more and embrace "idiomatic jsii". |
I no longer have time to contribute this, but it looks like @marciocadev 's pull request introduces a similar API which looks good |
… event sources (#21917) ## Description Adds the ability to create filters for SQS, DynamoDB and Kinesis, enabling filter criteria settings for event sources ## Use Cases With this PR will be possible, for example, to filter events from a DynamoDB Stream allowing only INSERT events to be transmitted as shown in the example below ```typescript const fn = new NodejsFunction(this, 'Fn'); const table = new dynamodb.Table(this, 'T', { partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING, }, stream: dynamodb.StreamViewType.NEW_IMAGE, }); fn.addEventSource(new sources.DynamoEventSource(table, { startingPosition: lambda.StartingPosition.LATEST, filters: [ lambda.FilterCriteria.filter({ eventName: FilterRule.isEqual('INSERT'), }), ], })); ``` Closes #17874 ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
|
… event sources (aws#21917) ## Description Adds the ability to create filters for SQS, DynamoDB and Kinesis, enabling filter criteria settings for event sources ## Use Cases With this PR will be possible, for example, to filter events from a DynamoDB Stream allowing only INSERT events to be transmitted as shown in the example below ```typescript const fn = new NodejsFunction(this, 'Fn'); const table = new dynamodb.Table(this, 'T', { partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING, }, stream: dynamodb.StreamViewType.NEW_IMAGE, }); fn.addEventSource(new sources.DynamoEventSource(table, { startingPosition: lambda.StartingPosition.LATEST, filters: [ lambda.FilterCriteria.filter({ eventName: FilterRule.isEqual('INSERT'), }), ], })); ``` Closes aws#17874 ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Description
Support the newly released event filtering feature for Amazon SQS, Amazon DynamoDB, and Amazon Kinesis.
https://aws.amazon.com/about-aws/whats-new/2021/11/aws-lambda-event-filtering-amazon-sqs-dynamodb-kinesis-sources/
https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html
Use Case
For filtering event source.
Proposed Solution
Add a
filters
toSqsEventSourceProps
,KinesisEventSourceProps
andDynamoEventSourceProps
.Other information
No response
Acknowledge
The text was updated successfully, but these errors were encountered: