Skip to content

Commit 18e7a7b

Browse files
authored
fix: adding support for passing target id to EventBridgeRule (#1747)
1 parent abde771 commit 18e7a7b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

samtranslator/model/eventsources/push.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ class CloudWatchEvent(PushEventSource):
150150
"Pattern": PropertyType(False, is_type(dict)),
151151
"Input": PropertyType(False, is_str()),
152152
"InputPath": PropertyType(False, is_str()),
153+
"Target": PropertyType(False, is_type(dict)),
153154
}
154155

155156
def to_cloudformation(self, **kwargs):
@@ -187,7 +188,8 @@ def _construct_target(self, function):
187188
:returns: the Target property
188189
:rtype: dict
189190
"""
190-
target = {"Arn": function.get_runtime_attr("arn"), "Id": self.logical_id + "LambdaTarget"}
191+
target_id = self.Target["Id"] if self.Target and "Id" in self.Target else self.logical_id + "LambdaTarget"
192+
target = {"Arn": function.get_runtime_attr("arn"), "Id": target_id}
191193
if self.Input is not None:
192194
target["Input"] = self.Input
193195

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from mock import Mock, patch
2+
from unittest import TestCase
3+
4+
from samtranslator.model.eventsources.push import CloudWatchEvent
5+
from samtranslator.model.lambda_ import LambdaFunction
6+
7+
8+
class CloudWatchEventSourceTests(TestCase):
9+
def setUp(self):
10+
self.logical_id = "EventLogicalId"
11+
self.func = LambdaFunction("func")
12+
13+
def test_target_id_when_not_provided(self):
14+
cloudwatch_event_source = CloudWatchEvent(self.logical_id)
15+
cfn = cloudwatch_event_source.to_cloudformation(function=self.func)
16+
target_id = cfn[0].Targets[0]["Id"]
17+
self.assertEqual(target_id, "{}{}".format(self.logical_id, "LambdaTarget"))
18+
19+
def test_target_id_when_provided(self):
20+
cloudwatch_event_source = CloudWatchEvent(self.logical_id)
21+
cloudwatch_event_source.Target = {"Id": "MyTargetId"}
22+
cfn = cloudwatch_event_source.to_cloudformation(function=self.func)
23+
target_id = cfn[0].Targets[0]["Id"]
24+
self.assertEqual(target_id, "MyTargetId")

0 commit comments

Comments
 (0)