Skip to content

Commit 8485376

Browse files
authored
Allowing appProtocol to be set for port mappings (#752)
1 parent cf991eb commit 8485376

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

ecs_composex/compose/compose_services/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,9 @@ def define_port_mappings(self) -> list:
904904
service_port,
905905
f"{protocol.lower()}_{target_port}",
906906
),
907+
AppProtocol=set_else_none(
908+
"app_protocol", target_port, NoValue
909+
),
907910
)
908911
)
909912
else:

ecs_composex/ecs/service_networking/ingress_helpers.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from ecs_composex.compose.compose_services import ComposeService
1313
from ecs_composex.common.settings import ComposeXSettings
1414
from ecs_composex.common.stacks import ComposeXStack
15+
from troposphere.ecs import PortMapping
1516
from ecs_composex.ecs_ingress.ecs_ingress_stack import XStack as EcsIngressStack
1617

1718
from json import dumps
@@ -318,6 +319,21 @@ def find_namespace(
318319
return namespace.get_resource_attribute_value(parameter, family)[0]
319320

320321

322+
def find_port_mapping(port_name, family: ComposeFamily) -> PortMapping | None:
323+
"""
324+
Goes over all the services/containers of the task definition and over each PortMapping of the container.
325+
If the port name matches, we have found the PortMapping
326+
"""
327+
for service in family.ordered_services:
328+
port_mappings = getattr(service.container_definition, "PortMappings", [])
329+
if not port_mappings:
330+
continue
331+
for _port_mapping in port_mappings:
332+
if _port_mapping.Name == port_name:
333+
return _port_mapping
334+
return
335+
336+
321337
def set_ecs_connect_from_macro(
322338
family: ComposeFamily,
323339
service: ComposeService,
@@ -347,8 +363,19 @@ def set_ecs_connect_from_macro(
347363
f"No port called {port_name} in family {family.name}",
348364
[_port["name"] for _port in family.service_networking.ports],
349365
)
350-
351366
dns_name = set_else_none("DnsName", connect_config, None)
367+
app_protocol = set_else_none("appProtocol", connect_config, NoValue)
368+
valid_protocols: list[str] = ["http", "http2", "grpc"]
369+
if (
370+
not isinstance(app_protocol, str) and app_protocol not in valid_protocols
371+
) and app_protocol is not NoValue:
372+
raise ValueError(
373+
"appProtocol must be one of", valid_protocols, "got", app_protocol
374+
)
375+
if "appProtocol" not in the_port:
376+
the_port["appProtocol"] = app_protocol
377+
port_mapping: PortMapping = find_port_mapping(the_port["name"], family)
378+
setattr(port_mapping, "AppProtocol", app_protocol)
352379
client_aliases = NoValue
353380
if dns_name:
354381
client_aliases = [

ecs_composex/specs/services.x-network.spec.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@
111111
"type": "string",
112112
"maxLength": 64,
113113
"description": "Optional - Set the name of the service as it appears in the CloudMap namespace"
114+
},
115+
"appProtocol": {
116+
"description": "https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_PortMapping.html",
117+
"type": "string",
118+
"pattern": "http|http2|grpc"
114119
}
115120
},
116121
"patternProperties": {

use-cases/yelb.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ services:
1717
- 80
1818
environment:
1919
- UI_ENV=prod
20+
- APP_SERVER=yelb-appserver
2021
networks:
2122
- public
2223
x-network:
@@ -39,6 +40,8 @@ services:
3940
environment:
4041
redishost: redis-server
4142
yelbdbhost: yelb-db
43+
YELB_DB_SERVER: yelb-db
44+
YELB_REDIS_SERVER: redis-server
4245
x-network:
4346
Ingress:
4447
Services:
@@ -48,7 +51,7 @@ services:
4851
service_ports:
4952
tcp_4567:
5053
DnsName: yelb-appserver
51-
CloudMapServiceName: yelb-appserver
54+
CloudMapServiceName: yelb-appservera
5255
x-cloudmap: PrivateNamespace
5356

5457

0 commit comments

Comments
 (0)