Skip to content

Commit d986e14

Browse files
authored
feat(sns): Add FilterPolicyScope support (#23108)
---- closes #23079 ### Description This PR adds support for FilterPolicyScope for SNS subscriptions. This is in response to AWS introducing payload-based message filtering: https://aws.amazon.com/blogs/compute/introducing-payload-based-message-filtering-for-amazon-sns/. ### 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*
1 parent bee883c commit d986e14

30 files changed

+2905
-40
lines changed

packages/@aws-cdk/aws-sns-subscriptions/lib/email.ts

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export class EmailSubscription implements sns.ITopicSubscription {
3232
endpoint: this.emailAddress,
3333
protocol: this.props.json ? sns.SubscriptionProtocol.EMAIL_JSON : sns.SubscriptionProtocol.EMAIL,
3434
filterPolicy: this.props.filterPolicy,
35+
filterPolicyWithMessageBody: this.props.filterPolicyWithMessageBody,
3536
deadLetterQueue: this.props.deadLetterQueue,
3637
};
3738
}

packages/@aws-cdk/aws-sns-subscriptions/lib/lambda.ts

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export class LambdaSubscription implements sns.ITopicSubscription {
4545
endpoint: this.fn.functionArn,
4646
protocol: sns.SubscriptionProtocol.LAMBDA,
4747
filterPolicy: this.props.filterPolicy,
48+
filterPolicyWithMessageBody: this.props.filterPolicyWithMessageBody,
4849
region: this.regionFromArn(topic),
4950
deadLetterQueue: this.props.deadLetterQueue,
5051
};

packages/@aws-cdk/aws-sns-subscriptions/lib/sms.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class SmsSubscription implements sns.ITopicSubscription {
2020
endpoint: this.phoneNumber,
2121
protocol: sns.SubscriptionProtocol.SMS,
2222
filterPolicy: this.props.filterPolicy,
23+
filterPolicyWithMessageBody: this.props.filterPolicyWithMessageBody,
2324
};
2425
}
2526
}

packages/@aws-cdk/aws-sns-subscriptions/lib/sqs.ts

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export class SqsSubscription implements sns.ITopicSubscription {
7575
protocol: sns.SubscriptionProtocol.SQS,
7676
rawMessageDelivery: this.props.rawMessageDelivery,
7777
filterPolicy: this.props.filterPolicy,
78+
filterPolicyWithMessageBody: this.props.filterPolicyWithMessageBody,
7879
region: this.regionFromArn(topic),
7980
deadLetterQueue: this.props.deadLetterQueue,
8081
subscriptionDependency: queuePolicyDependable,

packages/@aws-cdk/aws-sns-subscriptions/lib/subscription.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ export interface SubscriptionProps {
1111
* @default - all messages are delivered
1212
*/
1313
readonly filterPolicy?: { [attribute: string]: sns.SubscriptionFilter };
14-
14+
/**
15+
* The filter policy that is applied on the message body.
16+
* To apply a filter policy to the message attributes, use `filterPolicy`. A maximum of one of `filterPolicyWithMessageBody` and `filterPolicy` may be used.
17+
*
18+
* @default - all messages are delivered
19+
*/
20+
readonly filterPolicyWithMessageBody?: { [attribute: string]: sns.FilterOrPolicy };
1521
/**
1622
* Queue to be used as dead letter queue.
1723
* If not passed no dead letter queue is enabled.

packages/@aws-cdk/aws-sns-subscriptions/lib/url.ts

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export class UrlSubscription implements sns.ITopicSubscription {
6161
protocol: this.protocol,
6262
rawMessageDelivery: this.props.rawMessageDelivery,
6363
filterPolicy: this.props.filterPolicy,
64+
filterPolicyWithMessageBody: this.props.filterPolicyWithMessageBody,
6465
deadLetterQueue: this.props.deadLetterQueue,
6566
};
6667
}

packages/@aws-cdk/aws-sns-subscriptions/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"@aws-cdk/assertions": "0.0.0",
7676
"@aws-cdk/cdk-build-tools": "0.0.0",
7777
"@aws-cdk/integ-runner": "0.0.0",
78+
"@aws-cdk/integ-tests": "0.0.0",
7879
"@aws-cdk/cfn2ts": "0.0.0",
7980
"@aws-cdk/pkglint": "0.0.0",
8081
"@types/jest": "^27.5.2",

packages/@aws-cdk/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/aws-cdk-sns-lambda.assets.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"version": "20.0.0",
2+
"version": "22.0.0",
33
"files": {
4-
"451c86b70ff9eaa09b0558745f5aa97d2af847bd82e8960d44d5c91c88fa855a": {
4+
"b4d5d97a3217325cd1f06b3e725bdfbb9c4fd3c5c4a6adba15b286efa9146e23": {
55
"source": {
66
"path": "aws-cdk-sns-lambda.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "451c86b70ff9eaa09b0558745f5aa97d2af847bd82e8960d44d5c91c88fa855a.json",
12+
"objectKey": "b4d5d97a3217325cd1f06b3e725bdfbb9c4fd3c5c4a6adba15b286efa9146e23.json",
1313
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
1414
}
1515
}

packages/@aws-cdk/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/aws-cdk-sns-lambda.template.json

+95
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,101 @@
241241
]
242242
}
243243
}
244+
},
245+
"FilteredMessageBodyServiceRoleB2EB82B3": {
246+
"Type": "AWS::IAM::Role",
247+
"Properties": {
248+
"AssumeRolePolicyDocument": {
249+
"Statement": [
250+
{
251+
"Action": "sts:AssumeRole",
252+
"Effect": "Allow",
253+
"Principal": {
254+
"Service": "lambda.amazonaws.com"
255+
}
256+
}
257+
],
258+
"Version": "2012-10-17"
259+
},
260+
"ManagedPolicyArns": [
261+
{
262+
"Fn::Join": [
263+
"",
264+
[
265+
"arn:",
266+
{
267+
"Ref": "AWS::Partition"
268+
},
269+
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
270+
]
271+
]
272+
}
273+
]
274+
}
275+
},
276+
"FilteredMessageBody222AE8F1": {
277+
"Type": "AWS::Lambda::Function",
278+
"Properties": {
279+
"Code": {
280+
"ZipFile": "exports.handler = function handler(event, _context, callback) {\n /* eslint-disable no-console */\n console.log('====================================================');\n console.log(JSON.stringify(event, undefined, 2));\n console.log('====================================================');\n return callback(undefined, event);\n}"
281+
},
282+
"Role": {
283+
"Fn::GetAtt": [
284+
"FilteredMessageBodyServiceRoleB2EB82B3",
285+
"Arn"
286+
]
287+
},
288+
"Handler": "index.handler",
289+
"Runtime": "nodejs14.x"
290+
},
291+
"DependsOn": [
292+
"FilteredMessageBodyServiceRoleB2EB82B3"
293+
]
294+
},
295+
"FilteredMessageBodyAllowInvokeawscdksnslambdaMyTopic6C62AB90FB54CEA4": {
296+
"Type": "AWS::Lambda::Permission",
297+
"Properties": {
298+
"Action": "lambda:InvokeFunction",
299+
"FunctionName": {
300+
"Fn::GetAtt": [
301+
"FilteredMessageBody222AE8F1",
302+
"Arn"
303+
]
304+
},
305+
"Principal": "sns.amazonaws.com",
306+
"SourceArn": {
307+
"Ref": "MyTopic86869434"
308+
}
309+
}
310+
},
311+
"FilteredMessageBodyMyTopicAD1F55C4": {
312+
"Type": "AWS::SNS::Subscription",
313+
"Properties": {
314+
"Protocol": "lambda",
315+
"TopicArn": {
316+
"Ref": "MyTopic86869434"
317+
},
318+
"Endpoint": {
319+
"Fn::GetAtt": [
320+
"FilteredMessageBody222AE8F1",
321+
"Arn"
322+
]
323+
},
324+
"FilterPolicy": {
325+
"background": {
326+
"color": [
327+
"red",
328+
{
329+
"prefix": "bl"
330+
},
331+
{
332+
"prefix": "ye"
333+
}
334+
]
335+
}
336+
},
337+
"FilterPolicyScope": "MessageBody"
338+
}
244339
}
245340
},
246341
"Parameters": {
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":"20.0.0"}
1+
{"version":"22.0.0"}

packages/@aws-cdk/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/integ.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "20.0.0",
2+
"version": "22.0.0",
33
"testCases": {
44
"integ.sns-lambda": {
55
"stacks": [

packages/@aws-cdk/aws-sns-subscriptions/test/integ.sns-lambda.js.snapshot/manifest.json

+32-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
{
2-
"version": "20.0.0",
2+
"version": "22.0.0",
33
"artifacts": {
4-
"Tree": {
5-
"type": "cdk:tree",
6-
"properties": {
7-
"file": "tree.json"
8-
}
9-
},
104
"aws-cdk-sns-lambda.assets": {
115
"type": "cdk:asset-manifest",
126
"properties": {
@@ -23,7 +17,7 @@
2317
"validateOnSynth": false,
2418
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
2519
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
26-
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/451c86b70ff9eaa09b0558745f5aa97d2af847bd82e8960d44d5c91c88fa855a.json",
20+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/b4d5d97a3217325cd1f06b3e725bdfbb9c4fd3c5c4a6adba15b286efa9146e23.json",
2721
"requiresBootstrapStackVersion": 6,
2822
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
2923
"additionalDependencies": [
@@ -105,6 +99,30 @@
10599
"data": "FilteredMyTopicC8395C27"
106100
}
107101
],
102+
"/aws-cdk-sns-lambda/FilteredMessageBody/ServiceRole/Resource": [
103+
{
104+
"type": "aws:cdk:logicalId",
105+
"data": "FilteredMessageBodyServiceRoleB2EB82B3"
106+
}
107+
],
108+
"/aws-cdk-sns-lambda/FilteredMessageBody/Resource": [
109+
{
110+
"type": "aws:cdk:logicalId",
111+
"data": "FilteredMessageBody222AE8F1"
112+
}
113+
],
114+
"/aws-cdk-sns-lambda/FilteredMessageBody/AllowInvoke:awscdksnslambdaMyTopic6C62AB90": [
115+
{
116+
"type": "aws:cdk:logicalId",
117+
"data": "FilteredMessageBodyAllowInvokeawscdksnslambdaMyTopic6C62AB90FB54CEA4"
118+
}
119+
],
120+
"/aws-cdk-sns-lambda/FilteredMessageBody/MyTopic/Resource": [
121+
{
122+
"type": "aws:cdk:logicalId",
123+
"data": "FilteredMessageBodyMyTopicAD1F55C4"
124+
}
125+
],
108126
"/aws-cdk-sns-lambda/BootstrapVersion": [
109127
{
110128
"type": "aws:cdk:logicalId",
@@ -119,6 +137,12 @@
119137
]
120138
},
121139
"displayName": "aws-cdk-sns-lambda"
140+
},
141+
"Tree": {
142+
"type": "cdk:tree",
143+
"properties": {
144+
"file": "tree.json"
145+
}
122146
}
123147
}
124148
}

0 commit comments

Comments
 (0)