-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
565faa3
commit 89d8d64
Showing
23 changed files
with
1,034 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,4 +98,6 @@ ENV/ | |
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.mypy_cache/ | ||
|
||
cdk.out/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7 | ||
FROM laurents/uvicorn-gunicorn-fastapi:python3.7-slim | ||
# Ref https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker/issues/15 | ||
# Cuts image size by 50% | ||
# FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7 | ||
|
||
ENV CURL_CA_BUNDLE /etc/ssl/certs/ca-certificates.crt | ||
|
||
COPY README.md /app/README.md | ||
COPY titiler/ /app/titiler/ | ||
COPY setup.py /app/setup.py | ||
|
||
RUN pip install /app/. | ||
RUN pip install -e /app/. --no-cache-dir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"app": "python3 stack/app.py" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""AWS App.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
"""Construct App.""" | ||
|
||
from typing import Any, Union | ||
|
||
import os | ||
|
||
from aws_cdk import ( | ||
core, | ||
aws_ec2 as ec2, | ||
aws_ecs as ecs, | ||
aws_ecs_patterns as ecs_patterns, | ||
) | ||
|
||
import config | ||
|
||
|
||
class titilerStack(core.Stack): | ||
"""Titiler ECS Fargate Stack.""" | ||
|
||
def __init__( | ||
self, | ||
scope: core.Construct, | ||
id: str, | ||
cpu: Union[int, float] = 256, | ||
memory: Union[int, float] = 512, | ||
mincount: int = 1, | ||
maxcount: int = 50, | ||
code_dir: str = "./", | ||
**kwargs: Any, | ||
) -> None: | ||
"""Define stack.""" | ||
super().__init__(scope, id, *kwargs) | ||
|
||
vpc = ec2.Vpc(self, f"{id}-vpc", max_azs=2) | ||
|
||
cluster = ecs.Cluster(self, f"{id}-cluster", vpc=vpc) | ||
|
||
fargate_service = ecs_patterns.ApplicationLoadBalancedFargateService( | ||
self, | ||
f"{id}-service", | ||
cluster=cluster, | ||
cpu=cpu, | ||
memory_limit_mib=memory, | ||
desired_count=mincount, | ||
public_load_balancer=True, | ||
listener_port=80, | ||
task_image_options=dict( | ||
image=ecs.ContainerImage.from_asset( | ||
code_dir, exclude=["cdk.out", ".git"] | ||
), | ||
container_port=80, | ||
environment=dict( | ||
CPL_TMPDIR="/tmp", | ||
GDAL_CACHEMAX="25%", | ||
GDAL_DISABLE_READDIR_ON_OPEN="EMPTY_DIR", | ||
GDAL_HTTP_MERGE_CONSECUTIVE_RANGES="YES", | ||
GDAL_HTTP_MULTIPLEX="YES", | ||
GDAL_HTTP_VERSION="2", | ||
MODULE_NAME="titiler.main", | ||
PYTHONWARNINGS="ignore", | ||
VARIABLE_NAME="app", | ||
VSI_CACHE="TRUE", | ||
VSI_CACHE_SIZE="1000000", | ||
WORKERS_PER_CORE="5", | ||
LOG_LEVEL="error", | ||
), | ||
), | ||
) | ||
|
||
scalable_target = fargate_service.service.auto_scale_task_count( | ||
min_capacity=mincount, max_capacity=maxcount | ||
) | ||
|
||
# https://github.com/awslabs/aws-rails-provisioner/blob/263782a4250ca1820082bfb059b163a0f2130d02/lib/aws-rails-provisioner/scaling.rb#L343-L387 | ||
scalable_target.scale_on_request_count( | ||
"RequestScaling", | ||
requests_per_target=50, | ||
scale_in_cooldown=core.Duration.seconds(240), | ||
scale_out_cooldown=core.Duration.seconds(30), | ||
target_group=fargate_service.target_group, | ||
) | ||
|
||
# scalable_target.scale_on_cpu_utilization( | ||
# "CpuScaling", target_utilization_percent=70, | ||
# ) | ||
|
||
fargate_service.service.connections.allow_from_any_ipv4( | ||
port_range=ec2.Port( | ||
protocol=ec2.Protocol.ALL, | ||
string_representation="All port 80", | ||
from_port=80, | ||
), | ||
description="Allows traffic on port 80 from NLB", | ||
) | ||
|
||
|
||
app = core.App() | ||
|
||
# Tag infrastructure | ||
for key, value in { | ||
"Project": config.PROJECT_NAME, | ||
"Stack": config.STAGE, | ||
"Owner": os.environ.get("OWNER"), | ||
"Client": os.environ.get("CLIENT"), | ||
}.items(): | ||
if value: | ||
core.Tag.add(app, key, value) | ||
|
||
stackname = f"{config.PROJECT_NAME}-{config.STAGE}" | ||
titilerStack( | ||
app, | ||
stackname, | ||
cpu=config.TASK_CPU, | ||
memory=config.TASK_MEMORY, | ||
mincount=config.MIN_ECS_INSTANCES, | ||
maxcount=config.MAX_ECS_INSTANCES, | ||
) | ||
app.synth() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"""STACK Configs.""" | ||
|
||
import os | ||
|
||
PROJECT_NAME = "titiler" | ||
STAGE = os.environ.get("STAGE", "dev") | ||
|
||
# // Service config | ||
# Min/Max Number of ECS images | ||
MIN_ECS_INSTANCES = 2 | ||
MAX_ECS_INSTANCES = 50 | ||
|
||
# CPU value | Memory value | ||
# 256 (.25 vCPU) | 0.5 GB, 1 GB, 2 GB | ||
# 512 (.5 vCPU) | 1 GB, 2 GB, 3 GB, 4 GB | ||
# 1024 (1 vCPU) | 2 GB, 3 GB, 4 GB, 5 GB, 6 GB, 7 GB, 8 GB | ||
# 2048 (2 vCPU) | Between 4 GB and 16 GB in 1-GB increments | ||
# 4096 (4 vCPU) | Between 8 GB and 30 GB in 1-GB increments | ||
TASK_CPU = 1024 | ||
TASK_MEMORY = 2048 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
"""titiler.""" | ||
"""titiler.api""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
"""titiler.""" | ||
"""titiler.api.api_v1""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
"""titiler.""" | ||
"""titiler.api.api_v1.endpoints""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.