11"""Construct App."""
22
33import os
4- from typing import Any , Dict , List , Optional , Union
4+ from typing import Any , Dict , List , Optional
55
66from aws_cdk import App , CfnOutput , Duration , Stack , Tags
77from aws_cdk import aws_apigatewayv2 as apigw
8- from aws_cdk import aws_ec2 as ec2
9- from aws_cdk import aws_ecs as ecs
10- from aws_cdk import aws_ecs_patterns as ecs_patterns
118from aws_cdk import aws_iam as iam
129from aws_cdk import aws_lambda
1310from aws_cdk import aws_logs as logs
@@ -47,6 +44,7 @@ def __init__(
4744 permissions = permissions or []
4845 environment = environment or {}
4946
47+ # COG / STAC / MosaicJSON
5048 lambda_function = aws_lambda .Function (
5149 self ,
5250 f"{ id } -lambda" ,
@@ -79,91 +77,38 @@ def __init__(
7977 )
8078 CfnOutput (self , "Endpoint" , value = api .url )
8179
82-
83- class titilerECSStack (Stack ):
84- """Titiler ECS Fargate Stack."""
85-
86- def __init__ (
87- self ,
88- scope : Construct ,
89- id : str ,
90- cpu : Union [int , float ] = 256 ,
91- memory : Union [int , float ] = 512 ,
92- mincount : int = 1 ,
93- maxcount : int = 50 ,
94- permissions : Optional [List [iam .PolicyStatement ]] = None ,
95- environment : Optional [Dict ] = None ,
96- code_dir : str = "./" ,
97- ** kwargs : Any ,
98- ) -> None :
99- """Define stack."""
100- super ().__init__ (scope , id , * kwargs )
101-
102- permissions = permissions or []
103- environment = environment or {}
104-
105- vpc = ec2 .Vpc (self , f"{ id } -vpc" , max_azs = 2 )
106-
107- cluster = ecs .Cluster (self , f"{ id } -cluster" , vpc = vpc )
108-
109- task_env = environment .copy ()
110- task_env .update ({"LOG_LEVEL" : "error" })
111-
112- # GUNICORN configuration
113- if settings .workers_per_core :
114- task_env .update ({"WORKERS_PER_CORE" : str (settings .workers_per_core )})
115- if settings .max_workers :
116- task_env .update ({"MAX_WORKERS" : str (settings .max_workers )})
117- if settings .web_concurrency :
118- task_env .update ({"WEB_CONCURRENCY" : str (settings .web_concurrency )})
119-
120- fargate_service = ecs_patterns .ApplicationLoadBalancedFargateService (
80+ # Xarray
81+ xarray_lambda_function = aws_lambda .Function (
12182 self ,
122- f"{ id } -service" ,
123- cluster = cluster ,
124- cpu = cpu ,
125- memory_limit_mib = memory ,
126- desired_count = mincount ,
127- public_load_balancer = True ,
128- listener_port = 80 ,
129- task_image_options = ecs_patterns .ApplicationLoadBalancedTaskImageOptions (
130- image = ecs .ContainerImage .from_registry (
131- f"ghcr.io/developmentseed/titiler:{ settings .image_version } " ,
132- ),
133- container_port = 80 ,
134- environment = task_env ,
83+ f"{ id } -xarray-lambda" ,
84+ runtime = runtime ,
85+ code = aws_lambda .Code .from_docker_build (
86+ path = os .path .abspath (code_dir ),
87+ file = "lambda/Dockerfile.xarray" ,
88+ platform = "linux/amd64" ,
89+ build_args = {
90+ "PYTHON_VERSION" : "3.12" ,
91+ },
13592 ),
93+ handler = "handler.handler" ,
94+ memory_size = memory ,
95+ reserved_concurrent_executions = concurrent ,
96+ timeout = Duration .seconds (timeout ),
97+ environment = environment ,
98+ log_retention = logs .RetentionDays .ONE_WEEK ,
13699 )
137- fargate_service .target_group .configure_health_check (path = "/healthz" )
138100
139101 for perm in permissions :
140- fargate_service .task_definition .task_role .add_to_policy (perm )
141-
142- scalable_target = fargate_service .service .auto_scale_task_count (
143- min_capacity = mincount , max_capacity = maxcount
144- )
102+ xarray_lambda_function .add_to_role_policy (perm )
145103
146- # https://github.com/awslabs/aws-rails-provisioner/blob/263782a4250ca1820082bfb059b163a0f2130d02/lib/aws-rails-provisioner/scaling.rb#L343-L387
147- scalable_target .scale_on_request_count (
148- "RequestScaling" ,
149- requests_per_target = 50 ,
150- scale_in_cooldown = Duration .seconds (240 ),
151- scale_out_cooldown = Duration .seconds (30 ),
152- target_group = fargate_service .target_group ,
153- )
154-
155- # scalable_target.scale_on_cpu_utilization(
156- # "CpuScaling", target_utilization_percent=70,
157- # )
158-
159- fargate_service .service .connections .allow_from_any_ipv4 (
160- port_range = ec2 .Port (
161- protocol = ec2 .Protocol .ALL ,
162- string_representation = "All port 80" ,
163- from_port = 80 ,
104+ xarray_api = apigw .HttpApi (
105+ self ,
106+ f"{ id } -xarray-endpoint" ,
107+ default_integration = HttpLambdaIntegration (
108+ f"{ id } -xarray-integration" , handler = xarray_lambda_function
164109 ),
165- description = "Allows traffic on port 80 from ALB" ,
166110 )
111+ CfnOutput (self , "Xarray-Endpoint" , value = xarray_api .url )
167112
168113
169114app = App ()
@@ -179,18 +124,6 @@ def __init__(
179124 )
180125 )
181126
182-
183- ecs_stack = titilerECSStack (
184- app ,
185- f"{ settings .name } -ecs-{ settings .stage } " ,
186- cpu = settings .task_cpu ,
187- memory = settings .task_memory ,
188- mincount = settings .min_ecs_instances ,
189- maxcount = settings .max_ecs_instances ,
190- permissions = perms ,
191- environment = settings .env ,
192- )
193-
194127lambda_stack = titilerLambdaStack (
195128 app ,
196129 f"{ settings .name } -lambda-{ settings .stage } " ,
@@ -209,7 +142,6 @@ def __init__(
209142 "Client" : settings .client ,
210143}.items ():
211144 if value :
212- Tags .of (ecs_stack ).add (key , value )
213145 Tags .of (lambda_stack ).add (key , value )
214146
215147
0 commit comments