Skip to content

Commit 7f43e23

Browse files
authored
Merge pull request #231 from pierretr/master
Fix backward incompatible change of VPC endpoint names
2 parents 2f44960 + 12a4d8b commit 7f43e23

File tree

6 files changed

+395
-11
lines changed

6 files changed

+395
-11
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def __init__(
102102
cidr_block: str,
103103
vpc: ec2.vpc,
104104
interface_endpoints: list[tuple[str, PolicyDocument | None]] | None = None,
105+
vpc_prefixed_endpoints: bool | None = None,
105106
) -> None:
106107
"""Initialize VPCEndpointsSubnet Construct.
107108
@@ -111,11 +112,15 @@ def __init__(
111112
:param vpc: attach the subnet to this vpc
112113
:param interface_endpoint: list of (<service_name>, <endpoint_policy_document>)
113114
tuples for each interface endpoint to create in the vpc endpoints subnet.
115+
:param vpc_prefixed_endpoints: If True prefix endpoint names by VPC name.
116+
This is to avoid conflicts in stacks with multiple VPCs with endpoints for
117+
the same services. This is not the default for backward compatibility.
114118
"""
115119
self.name = name
116120
self.region = region
117121
self.cidr_block = cidr_block
118122
self.vpc = vpc
123+
self.vpc_prefixed_endpoints = vpc_prefixed_endpoints
119124
self.has_ses_endpoint = False
120125

121126
if interface_endpoints:
@@ -220,9 +225,14 @@ def interface_vpc_endpoints(self) -> list[ec2.VPCEndpoint]:
220225
else:
221226
security_group_id = Ref(self.security_group)
222227

228+
if self.vpc_prefixed_endpoints:
229+
endpoint_name = f"{self.vpc.name}-{service_name}Endpoint"
230+
else:
231+
endpoint_name = f"{service_name}Endpoint"
232+
223233
endpoints.append(
224234
ec2.VPCEndpoint(
225-
name_to_id(f"{self.vpc.name}-{service_name}Endpoint"),
235+
name_to_id(endpoint_name),
226236
PrivateDnsEnabled="true",
227237
SecurityGroupIds=[security_group_id],
228238
ServiceName=f"com.amazonaws.{self.region}.{service_name}",
@@ -404,6 +414,7 @@ def __init__(
404414
s3_endpoint_policy_document: PolicyDocument | None = None,
405415
interface_endpoints: list[tuple[str, PolicyDocument | None]] | None = None,
406416
tags: dict[str, str] | None = None,
417+
vpc_prefixed_endpoints: bool | None = None,
407418
) -> None:
408419
"""Initialize VPC Construct.
409420
@@ -424,6 +435,9 @@ def __init__(
424435
:param interface_endpoint: list of (<service_name>, <endpoint_policy_document>)
425436
tuples for each interface endpoint to create in the vpc endpoints subnet.
426437
:param tags: tags for the VPC
438+
:param vpc_prefixed_endpoints: It should be set to True if multiple VPCs in
439+
the same stack have endpoints for the same services to avoid name
440+
conflicts. It is None by default for backward compatibility.
427441
"""
428442
self.name = name
429443
self.region = region
@@ -496,6 +510,7 @@ def __init__(
496510
cidr_block=vpc_endpoints_subnet_cidr_block,
497511
vpc=self.vpc,
498512
interface_endpoints=interface_endpoints,
513+
vpc_prefixed_endpoints=vpc_prefixed_endpoints,
499514
)
500515

501516
@cached_property

tests/tests_e3_aws/troposphere/ec2/ec2_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,26 @@ def test_vpc_with_ses_and_other_endpoints(stack: Stack) -> None:
130130
expected_template = json.load(fd)
131131

132132
assert stack.export()["Resources"] == expected_template
133+
134+
135+
def test_vpc_with_vpc_prefixed_endpoints(stack: Stack) -> None:
136+
"""Test creation of a VPC with endpoints prefixed by vpc name."""
137+
vpc = VPC(
138+
name="TestVPC",
139+
region="eu-west-1",
140+
nat_gateway=False,
141+
interface_endpoints=[
142+
("email-smtp", None),
143+
("logs", None),
144+
("sts", None),
145+
],
146+
vpc_prefixed_endpoints=True,
147+
)
148+
stack.add(vpc)
149+
150+
with open(
151+
os.path.join(TEST_DIR, "vpc_ses_and_other_endpoints_prefixed.json")
152+
) as fd:
153+
expected_template = json.load(fd)
154+
155+
assert stack.export()["Resources"] == expected_template

tests/tests_e3_aws/troposphere/ec2/vpc.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@
242242
},
243243
"Type": "AWS::EC2::SecurityGroupIngress"
244244
},
245-
"TestVPCLogsEndpoint": {
245+
"LogsEndpoint": {
246246
"Properties": {
247247
"PrivateDnsEnabled": true,
248248
"SecurityGroupIds": [
@@ -278,7 +278,7 @@
278278
},
279279
"Type": "AWS::EC2::VPCEndpoint"
280280
},
281-
"TestVPCEcrapiEndpoint": {
281+
"EcrapiEndpoint": {
282282
"Properties": {
283283
"PrivateDnsEnabled": true,
284284
"SecurityGroupIds": [
@@ -314,7 +314,7 @@
314314
},
315315
"Type": "AWS::EC2::VPCEndpoint"
316316
},
317-
"TestVPCEcrdkrEndpoint": {
317+
"EcrdkrEndpoint": {
318318
"Properties": {
319319
"PrivateDnsEnabled": true,
320320
"SecurityGroupIds": [
@@ -350,7 +350,7 @@
350350
},
351351
"Type": "AWS::EC2::VPCEndpoint"
352352
},
353-
"TestVPCStsEndpoint": {
353+
"StsEndpoint": {
354354
"Properties": {
355355
"PrivateDnsEnabled": true,
356356
"SecurityGroupIds": [
@@ -371,7 +371,7 @@
371371
},
372372
"Type": "AWS::EC2::VPCEndpoint"
373373
},
374-
"TestVPCSecretsmanagerEndpoint": {
374+
"SecretsmanagerEndpoint": {
375375
"Properties": {
376376
"PrivateDnsEnabled": true,
377377
"SecurityGroupIds": [
@@ -501,4 +501,4 @@
501501
},
502502
"Type": "AWS::EC2::SecurityGroup"
503503
}
504-
}
504+
}

tests/tests_e3_aws/troposphere/ec2/vpc_ses_and_other_endpoints.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@
242242
},
243243
"Type": "AWS::EC2::SecurityGroupIngress"
244244
},
245-
"TestVPCEmailSmtpEndpoint": {
245+
"EmailSmtpEndpoint": {
246246
"Properties": {
247247
"PrivateDnsEnabled": true,
248248
"SecurityGroupIds": [
@@ -263,7 +263,7 @@
263263
},
264264
"Type": "AWS::EC2::VPCEndpoint"
265265
},
266-
"TestVPCLogsEndpoint": {
266+
"LogsEndpoint": {
267267
"Properties": {
268268
"PrivateDnsEnabled": true,
269269
"SecurityGroupIds": [
@@ -284,7 +284,7 @@
284284
},
285285
"Type": "AWS::EC2::VPCEndpoint"
286286
},
287-
"TestVPCStsEndpoint": {
287+
"StsEndpoint": {
288288
"Properties": {
289289
"PrivateDnsEnabled": true,
290290
"SecurityGroupIds": [

0 commit comments

Comments
 (0)