Skip to content

Commit 4f424f6

Browse files
committed
Merge branch 'morosi-fix' into 'master'
Allow specify version or alias in add_lambda_subscription See merge request it/e3-aws!88
2 parents 76bb114 + 18679d3 commit 4f424f6

File tree

2 files changed

+105
-6
lines changed

2 files changed

+105
-6
lines changed

src/e3/aws/troposphere/sns/__init__.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from typing import Any
1212
from troposphere import AWSObject
1313
from e3.aws.troposphere import Stack
14-
from e3.aws.troposphere.awslambda import Function
14+
from e3.aws.troposphere.awslambda import Function, Version, Alias
1515
from e3.aws.troposphere.iam.policy_statement import ConditionType
1616

1717

@@ -36,15 +36,19 @@ def _get_topic_policy_name(self) -> str:
3636
return name_to_id(f"{self.name}Policy")
3737

3838
def add_lambda_subscription(
39-
self, function: Function, delivery_policy: dict | None = None
39+
self,
40+
function: Function,
41+
delivery_policy: dict | None = None,
42+
version: Version | Alias | None = None,
4043
) -> None:
4144
"""Add a lambda subscription endpoint to topic.
4245
4346
:param function: lambda function that will be added as endpoint
4447
:param delivery_policy: The delivery policy to assign to the subscription
48+
:param version: specific version or alias to subscribe
4549
"""
4650
sub_params = {
47-
"Endpoint": function.arn,
51+
"Endpoint": version.ref if version is not None else function.arn,
4852
"Protocol": "lambda",
4953
"TopicArn": self.arn,
5054
}
@@ -55,10 +59,18 @@ def add_lambda_subscription(
5559
self.optional_resources.extend(
5660
[
5761
sns.SubscriptionResource(
58-
name_to_id(f"{function.name}Sub"), **sub_params
62+
name_to_id(
63+
"{}Sub".format(
64+
version.name if version is not None else function.name
65+
)
66+
),
67+
**sub_params,
5968
),
6069
function.invoke_permission(
61-
name_suffix=self.name, service="sns", source_arn=self.arn
70+
name_suffix=self.name,
71+
service="sns",
72+
source_arn=self.arn,
73+
version=version,
6274
),
6375
]
6476
)

tests/tests_e3_aws/troposphere/sns/sns_test.py

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
from __future__ import annotations
2-
2+
from typing import TYPE_CHECKING
33
import pytest
44

55
from e3.aws.troposphere import Stack
66
from e3.aws.troposphere.sns import Topic
7+
from e3.aws.troposphere.awslambda import PyFunction, Version, Alias
8+
9+
if TYPE_CHECKING:
10+
from typing import Any
711

812

913
EXPECTED_TOPIC_DEFAULT_TEMPLATE = {
@@ -61,3 +65,86 @@ def test_allow_service_to_publish_not_unique_sid(stack: Stack) -> None:
6165
stack.add(topic)
6266

6367
assert str(ex.value) == "Unique Sid is required for TopicPolicy statements"
68+
69+
70+
@pytest.mark.parametrize(
71+
"version, expected_endpoint, expected_function_name_ref",
72+
[
73+
# Add the subscription to the function itself
74+
(
75+
None,
76+
{"Fn::GetAtt": ["Mypylambda", "Arn"]},
77+
"Mypylambda",
78+
),
79+
# Add the subscription to a version of the function
80+
(
81+
Version(
82+
name="myversion", description="this is some version", lambda_arn=""
83+
),
84+
{"Ref": "Myversion"},
85+
"Myversion",
86+
),
87+
# Add the subscription to an alias of the function
88+
(
89+
Alias(
90+
name="myalias",
91+
description="this is some alias",
92+
lambda_arn="",
93+
lambda_version="",
94+
),
95+
{"Ref": "Myalias"},
96+
"Myalias",
97+
),
98+
],
99+
)
100+
def test_topic_lambda_subscription(
101+
version: Version | Alias | None,
102+
expected_endpoint: dict[str, Any],
103+
expected_function_name_ref: str,
104+
stack: Stack,
105+
) -> None:
106+
"""Test topic creation with lambda subscription.
107+
108+
:param version: a version or alias of the function
109+
:param expected_endpoint: value that should be set for Endpoint
110+
:param expected_function_name_ref: name that should be referenced in FunctionName
111+
:param stack: the stack
112+
"""
113+
topic = Topic("mytopic")
114+
topic.add_lambda_subscription(
115+
function=PyFunction(
116+
name="mypylambda",
117+
description="this is a test",
118+
role="somearn",
119+
runtime="python3.9",
120+
code_dir="my_code_dir",
121+
handler="app.main",
122+
),
123+
version=version,
124+
)
125+
stack.add(topic)
126+
assert stack.export()["Resources"] == EXPECTED_TOPIC_DEFAULT_TEMPLATE | {
127+
f"{expected_function_name_ref}Sub": {
128+
"Properties": {
129+
"Endpoint": expected_endpoint,
130+
"Protocol": "lambda",
131+
"TopicArn": {
132+
"Ref": "Mytopic",
133+
},
134+
},
135+
"Type": "AWS::SNS::Subscription",
136+
},
137+
f"{expected_function_name_ref}mytopic": {
138+
"Properties": {
139+
"Action": "lambda:InvokeFunction",
140+
"FunctionName": {
141+
"Ref": expected_function_name_ref,
142+
},
143+
"Principal": "sns.amazonaws.com",
144+
"SourceArn": {
145+
"Ref": "Mytopic",
146+
},
147+
},
148+
"Type": "AWS::Lambda::Permission",
149+
},
150+
}

0 commit comments

Comments
 (0)