Skip to content

Commit 2bd6135

Browse files
hawflauvalerena
andauthored
feat: Event filtering for MQ, MSK, SMK (#134) (#265)
Support event filtering for Amazon MQ, MSK and SelfManagedKafka event sources Co-authored-by: Renato Valenzuela <[email protected]>
1 parent 848299d commit 2bd6135

File tree

7 files changed

+472
-25
lines changed

7 files changed

+472
-25
lines changed

samtranslator/model/eventsources/pull.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class PullEventSource(ResourceMacro):
2121
"""
2222

2323
# Event types that support `FilterCriteria`, stored as a list to keep the alphabetical order
24-
RESOURCE_TYPES_WITH_EVENT_FILTERING = ["DynamoDB", "Kinesis", "SQS"]
24+
RESOURCE_TYPES_WITH_EVENT_FILTERING = ["DynamoDB", "Kinesis", "MQ", "MSK", "SelfManagedKafka", "SQS"]
2525

2626
# Note(xinhol): `PullEventSource` should have been an abstract class. Disabling the type check for the next
2727
# line to avoid any potential behavior change.

tests/translator/input/error_event_filtering.yaml

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,6 @@ Resources:
1515
FiltersToUse:
1616
- Pattern: '{"name": "value"}'
1717

18-
NotSupportedPullEvent:
19-
Type: AWS::Serverless::Function
20-
Properties:
21-
CodeUri: s3://sam-demo-bucket/filtered_events.zip
22-
Handler: index.handler
23-
Runtime: nodejs16.x
24-
Events:
25-
KafkaEvent:
26-
Type: MSK
27-
Properties:
28-
StartingPosition: LATEST
29-
Stream: arn:aws:kafka:us-east-1:012345678012:cluster/clusterName/abcdefab-1234-abcd-5678-cdef0123ab01-2
30-
Topics:
31-
- MyTopic
32-
FilterCriteria:
33-
Filters:
34-
- Pattern: '{"name": "value"}'
35-
36-
3718
NotSupportedPushEvent:
3819
Type: AWS::Serverless::Function
3920
Properties:

tests/translator/input/function_with_event_filtering.yaml

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,53 @@ Resources:
2929
}
3030
}
3131
}'
32-
MySqsQueue:
32+
MySqsEvent:
3333
Type: SQS
3434
Properties:
3535
Queue: !GetAtt MySqsQueue.Arn
3636
FilterCriteria:
3737
Filters:
3838
- Pattern: '{"name": "value"}'
39+
MSKEvent:
40+
Type: MSK
41+
Properties:
42+
StartingPosition: LATEST
43+
Stream: arn:aws:kafka:us-west-2:012345678901:cluster/mycluster/6cc0432b-8618-4f44-bccc-e1fbd8fb7c4d-2
44+
Topics:
45+
- "MyDummyTestTopic"
46+
FilterCriteria:
47+
Filters:
48+
- Pattern: '{"name": "value"}'
49+
MyKafkaEvent:
50+
Type: SelfManagedKafka
51+
Properties:
52+
KafkaBootstrapServers:
53+
- "abc.xyz.com:9092"
54+
Topics:
55+
- "Topic1"
56+
SourceAccessConfigurations:
57+
- Type: SASL_SCRAM_512_AUTH
58+
URI: arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c
59+
- Type: VPC_SUBNET
60+
URI: subnet:subnet-12345
61+
- Type: VPC_SECURITY_GROUP
62+
URI: security_group:sg-67890
63+
FilterCriteria:
64+
Filters:
65+
- Pattern: '{"name": "value"}'
66+
MyMQQueue:
67+
Type: MQ
68+
Properties:
69+
Broker: arn:aws:mq:us-east-2:123456789012:broker:MyBroker:b-1234a5b6-78cd-901e-2fgh-3i45j6k178l9
70+
Queues:
71+
- "Queue1"
72+
SourceAccessConfigurations:
73+
- Type: BASIC_AUTH
74+
URI: arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c
75+
SecretsManagerKmsKeyId: 1abc23d4-567f-8ab9-cde0-1fab234c5d67
76+
FilterCriteria:
77+
Filters:
78+
- Pattern: '{"name": "value"}'
3979

4080
KinesisStream:
4181
Type: AWS::Kinesis::Stream

tests/translator/output/aws-cn/function_with_event_filtering.json

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,66 @@
5858
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
5959
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaDynamoDBExecutionRole",
6060
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaKinesisExecutionRole",
61+
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaMSKExecutionRole",
6162
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaSQSQueueExecutionRole"
6263
],
64+
"Policies": [
65+
{
66+
"PolicyDocument": {
67+
"Statement": [
68+
{
69+
"Action": [
70+
"secretsmanager:GetSecretValue"
71+
],
72+
"Effect": "Allow",
73+
"Resource": "arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c"
74+
},
75+
{
76+
"Action": [
77+
"ec2:CreateNetworkInterface",
78+
"ec2:DescribeNetworkInterfaces",
79+
"ec2:DeleteNetworkInterface",
80+
"ec2:DescribeVpcs",
81+
"ec2:DescribeSubnets",
82+
"ec2:DescribeSecurityGroups"
83+
],
84+
"Effect": "Allow",
85+
"Resource": "*"
86+
}
87+
],
88+
"Version": "2012-10-17"
89+
},
90+
"PolicyName": "SelfManagedKafkaExecutionRolePolicy"
91+
},
92+
{
93+
"PolicyName": "SamAutoGeneratedAMQPolicy",
94+
"PolicyDocument": {
95+
"Statement": [
96+
{
97+
"Action": [
98+
"secretsmanager:GetSecretValue"
99+
],
100+
"Effect": "Allow",
101+
"Resource": "arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c"
102+
},
103+
{
104+
"Action": [
105+
"mq:DescribeBroker"
106+
],
107+
"Effect": "Allow",
108+
"Resource": "arn:aws:mq:us-east-2:123456789012:broker:MyBroker:b-1234a5b6-78cd-901e-2fgh-3i45j6k178l9"
109+
},
110+
{
111+
"Action": "kms:Decrypt",
112+
"Effect": "Allow",
113+
"Resource": {
114+
"Fn::Sub": "arn:${AWS::Partition}:kms:${AWS::Region}:${AWS::AccountId}:key/1abc23d4-567f-8ab9-cde0-1fab234c5d67"
115+
}
116+
}
117+
]
118+
}
119+
}
120+
],
63121
"Tags": [
64122
{
65123
"Key": "lambda:createdBy",
@@ -115,7 +173,7 @@
115173
}
116174
}
117175
},
118-
"FilteredEventsFunctionMySqsQueue": {
176+
"FilteredEventsFunctionMySqsEvent": {
119177
"Type": "AWS::Lambda::EventSourceMapping",
120178
"Properties": {
121179
"EventSourceArn": {
@@ -135,6 +193,90 @@
135193
]
136194
}
137195
}
196+
},
197+
"FilteredEventsFunctionMSKEvent": {
198+
"Type": "AWS::Lambda::EventSourceMapping",
199+
"Properties": {
200+
"EventSourceArn": "arn:aws:kafka:us-west-2:012345678901:cluster/mycluster/6cc0432b-8618-4f44-bccc-e1fbd8fb7c4d-2",
201+
"FunctionName": {
202+
"Ref": "FilteredEventsFunction"
203+
},
204+
"StartingPosition": "LATEST",
205+
"Topics": [
206+
"MyDummyTestTopic"
207+
],
208+
"FilterCriteria": {
209+
"Filters": [
210+
{
211+
"Pattern": "{\"name\": \"value\"}"
212+
}
213+
]
214+
}
215+
}
216+
},
217+
"FilteredEventsFunctionMyKafkaEvent": {
218+
"Type": "AWS::Lambda::EventSourceMapping",
219+
"Properties": {
220+
"FunctionName": {
221+
"Ref": "FilteredEventsFunction"
222+
},
223+
"Topics": [
224+
"Topic1"
225+
],
226+
"SourceAccessConfigurations": [
227+
{
228+
"Type": "SASL_SCRAM_512_AUTH",
229+
"URI": "arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c"
230+
},
231+
{
232+
"Type": "VPC_SUBNET",
233+
"URI": "subnet:subnet-12345"
234+
},
235+
{
236+
"Type": "VPC_SECURITY_GROUP",
237+
"URI": "security_group:sg-67890"
238+
}
239+
],
240+
"SelfManagedEventSource": {
241+
"Endpoints": {
242+
"KafkaBootstrapServers": [
243+
"abc.xyz.com:9092"
244+
]
245+
}
246+
},
247+
"FilterCriteria": {
248+
"Filters": [
249+
{
250+
"Pattern": "{\"name\": \"value\"}"
251+
}
252+
]
253+
}
254+
}
255+
},
256+
"FilteredEventsFunctionMyMQQueue": {
257+
"Type": "AWS::Lambda::EventSourceMapping",
258+
"Properties": {
259+
"EventSourceArn": "arn:aws:mq:us-east-2:123456789012:broker:MyBroker:b-1234a5b6-78cd-901e-2fgh-3i45j6k178l9",
260+
"FunctionName": {
261+
"Ref": "FilteredEventsFunction"
262+
},
263+
"Queues": [
264+
"Queue1"
265+
],
266+
"SourceAccessConfigurations": [
267+
{
268+
"Type": "BASIC_AUTH",
269+
"URI": "arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c"
270+
}
271+
],
272+
"FilterCriteria": {
273+
"Filters": [
274+
{
275+
"Pattern": "{\"name\": \"value\"}"
276+
}
277+
]
278+
}
279+
}
138280
}
139281
}
140282
}

0 commit comments

Comments
 (0)