Skip to content

Commit

Permalink
feat: Add support to specify a SNS topic that belongs to a different …
Browse files Browse the repository at this point in the history
…region (#989)
  • Loading branch information
53ningen authored and jlhood committed Jul 2, 2019
1 parent 6004308 commit 7361002
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 5 deletions.
7 changes: 5 additions & 2 deletions samtranslator/model/eventsources/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ class SNS(PushEventSource):
principal = 'sns.amazonaws.com'
property_types = {
'Topic': PropertyType(True, is_str()),
'Region': PropertyType(False, is_str()),
'FilterPolicy': PropertyType(False, dict_of(is_str(), list_of(one_of(is_str(), is_type(dict)))))
}

Expand All @@ -360,13 +361,15 @@ def to_cloudformation(self, **kwargs):
raise TypeError("Missing required keyword argument: function")

return [self._construct_permission(function, source_arn=self.Topic),
self._inject_subscription(function, self.Topic, self.FilterPolicy)]
self._inject_subscription(function, self.Topic, self.Region, self.FilterPolicy)]

def _inject_subscription(self, function, topic, filterPolicy):
def _inject_subscription(self, function, topic, region, filterPolicy):
subscription = SNSSubscription(self.logical_id)
subscription.Protocol = 'lambda'
subscription.Endpoint = function.get_runtime_attr("arn")
subscription.TopicArn = topic
if region is not None:
subscription.Region = region
if CONDITION in function.resource_attributes:
subscription.set_resource_attribute(CONDITION, function.resource_attributes[CONDITION])

Expand Down
1 change: 1 addition & 0 deletions samtranslator/model/sns.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ class SNSSubscription(Resource):
'Endpoint': PropertyType(True, is_str()),
'Protocol': PropertyType(True, is_str()),
'TopicArn': PropertyType(True, is_str()),
'Region': PropertyType(False, is_str()),
'FilterPolicy': PropertyType(False, is_type(dict))
}
3 changes: 3 additions & 0 deletions samtranslator/validator/sam_schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,9 @@
"Topic": {
"type": "string"
},
"Region": {
"type": "string"
},
"FilterPolicy": {
"type": "object"
}
Expand Down
13 changes: 13 additions & 0 deletions tests/model/eventsources/test_sns_event_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,21 @@ def test_to_cloudformation_returns_permission_and_subscription_resources(self):
self.assertEqual(subscription.TopicArn, 'arn:aws:sns:MyTopic')
self.assertEqual(subscription.Protocol, 'lambda')
self.assertEqual(subscription.Endpoint, 'arn:aws:lambda:mock')
self.assertIsNone(subscription.Region)
self.assertIsNone(subscription.FilterPolicy)

def test_to_cloudformation_passes_the_region(self):
region = 'us-west-2'
self.sns_event_source.Region = region

resources = self.sns_event_source.to_cloudformation(
function=self.function)
self.assertEqual(len(resources), 2)
self.assertEqual(resources[1].resource_type,
'AWS::SNS::Subscription')
subscription = resources[1]
self.assertEqual(subscription.Region, region)

def test_to_cloudformation_passes_the_filter_policy(self):
filterPolicy = {
'attribute1': ['value1'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Resources:
Type: SNS
Properties:
Topic: topicArn
Region: region
FilterPolicy:
store:
- example_corp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
]
},
"Protocol": "lambda",
"TopicArn": "topicArn"
"TopicArn": "topicArn",
"Region": "region"
}
},
"MyAwesomeFunctionNotificationTopicPermission": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
]
},
"Protocol": "lambda",
"TopicArn": "topicArn"
"TopicArn": "topicArn",
"Region": "region"
}
},
"MyAwesomeFunctionNotificationTopicPermission": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
]
},
"Protocol": "lambda",
"TopicArn": "topicArn"
"TopicArn": "topicArn",
"Region": "region"
}
},
"MyAwesomeFunctionNotificationTopicPermission": {
Expand Down
1 change: 1 addition & 0 deletions versions/2016-10-31.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ The object describing an event source with type `SNS`.
Property Name | Type | Description
---|:---:|---
Topic | `string` | **Required.** Topic ARN.
Region | `string` | Region.
FilterPolicy | [Amazon SNS filter policy](https://docs.aws.amazon.com/sns/latest/dg/message-filtering.html) | Policy assigned to the topic subscription in order to receive only a subset of the messages.

##### Example: SNS event source object
Expand Down

0 comments on commit 7361002

Please sign in to comment.