-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathapp.py
202 lines (187 loc) · 7.33 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
from aws_cdk import (
aws_ec2 as ec2,
aws_s3 as s3,
aws_ecs as ecs,
aws_rds as rds,
aws_iam as iam,
aws_secretsmanager as sm,
aws_ecs_patterns as ecs_patterns,
App,
Stack,
CfnParameter,
CfnOutput,
Aws,
RemovalPolicy,
Duration,
)
from constructs import Construct
class MLflowStack(Stack):
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# ==============================
# ======= CFN PARAMETERS =======
# ==============================
project_name_param = CfnParameter(scope=self, id="ProjectName", type="String")
db_name = "mlflowdb"
port = 3306
username = "master"
bucket_name = f"{project_name_param.value_as_string}-artifacts-{Aws.ACCOUNT_ID}"
container_repo_name = "mlflow-containers"
cluster_name = "mlflow"
service_name = "mlflow"
# ==================================================
# ================= IAM ROLE =======================
# ==================================================
role = iam.Role(
scope=self,
id="TASKROLE",
assumed_by=iam.ServicePrincipal(service="ecs-tasks.amazonaws.com"),
)
role.add_managed_policy(
iam.ManagedPolicy.from_aws_managed_policy_name("AmazonS3FullAccess")
)
role.add_managed_policy(
iam.ManagedPolicy.from_aws_managed_policy_name("AmazonECS_FullAccess")
)
# ==================================================
# ================== SECRET ========================
# ==================================================
db_password_secret = sm.Secret(
scope=self,
id="DBSECRET",
secret_name="dbPassword",
generate_secret_string=sm.SecretStringGenerator(
password_length=20, exclude_punctuation=True
),
)
# ==================================================
# ==================== VPC =========================
# ==================================================
public_subnet = ec2.SubnetConfiguration(
name="Public", subnet_type=ec2.SubnetType.PUBLIC, cidr_mask=28
)
private_subnet = ec2.SubnetConfiguration(
name="Private", subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS, cidr_mask=28
)
isolated_subnet = ec2.SubnetConfiguration(
name="DB", subnet_type=ec2.SubnetType.PRIVATE_ISOLATED, cidr_mask=28
)
vpc = ec2.Vpc(
scope=self,
id="VPC",
ip_addresses=ec2.IpAddresses.cidr("10.0.0.0/24"),
max_azs=2,
nat_gateway_provider=ec2.NatProvider.gateway(),
nat_gateways=1,
subnet_configuration=[public_subnet, private_subnet, isolated_subnet],
)
vpc.add_gateway_endpoint(
"S3Endpoint", service=ec2.GatewayVpcEndpointAwsService.S3
)
# ==================================================
# ================= S3 BUCKET ======================
# ==================================================
artifact_bucket = s3.Bucket(
scope=self,
id="ARTIFACTBUCKET",
bucket_name=bucket_name,
public_read_access=False,
)
# # ==================================================
# # ================== DATABASE =====================
# # ==================================================
# Creates a security group for AWS RDS
sg_rds = ec2.SecurityGroup(
scope=self, id="SGRDS", vpc=vpc, security_group_name="sg_rds"
)
# Adds an ingress rule which allows resources in the VPC's CIDR to access the database.
sg_rds.add_ingress_rule(
peer=ec2.Peer.ipv4("10.0.0.0/24"), connection=ec2.Port.tcp(port)
)
database = rds.DatabaseInstance(
scope=self,
id="MYSQL",
database_name=db_name,
port=port,
credentials=rds.Credentials.from_username(
username=username, password=db_password_secret.secret_value
),
engine=rds.DatabaseInstanceEngine.mysql(
version=rds.MysqlEngineVersion.VER_8_0_34
),
instance_type=ec2.InstanceType.of(
ec2.InstanceClass.M5, ec2.InstanceSize.LARGE
),
vpc=vpc,
security_groups=[sg_rds],
vpc_subnets=ec2.SubnetSelection(
subnet_type=ec2.SubnetType.PRIVATE_ISOLATED
),
# multi_az=True,
removal_policy=RemovalPolicy.DESTROY,
deletion_protection=False,
)
# ==================================================
# =============== FARGATE SERVICE ==================
# ==================================================
cluster = ecs.Cluster(
scope=self, id="CLUSTER", cluster_name=cluster_name, vpc=vpc
)
task_definition = ecs.FargateTaskDefinition(
scope=self,
id="MLflow",
task_role=role,
cpu=4 * 1024,
memory_limit_mib=8 * 1024,
)
container = task_definition.add_container(
id="Container",
image=ecs.ContainerImage.from_asset(directory="container"),
environment={
"BUCKET": f"s3://{artifact_bucket.bucket_name}",
"HOST": database.db_instance_endpoint_address,
"PORT": str(port),
"DATABASE": db_name,
"USERNAME": username,
},
secrets={"PASSWORD": ecs.Secret.from_secrets_manager(db_password_secret)},
logging=ecs.LogDriver.aws_logs(stream_prefix="mlflow"),
)
port_mapping = ecs.PortMapping(
container_port=5000, host_port=5000, protocol=ecs.Protocol.TCP
)
container.add_port_mappings(port_mapping)
fargate_service = ecs_patterns.NetworkLoadBalancedFargateService(
scope=self,
id="MLFLOW",
service_name=service_name,
cluster=cluster,
task_definition=task_definition,
)
# Setup security group
fargate_service.service.connections.security_groups[0].add_ingress_rule(
peer=ec2.Peer.ipv4(vpc.vpc_cidr_block),
connection=ec2.Port.tcp(5000),
description="Allow inbound from VPC for mlflow",
)
# Setup autoscaling policy
scaling = fargate_service.service.auto_scale_task_count(max_capacity=2)
scaling.scale_on_cpu_utilization(
id="AUTOSCALING",
target_utilization_percent=70,
scale_in_cooldown=Duration.seconds(60),
scale_out_cooldown=Duration.seconds(60),
)
# ==================================================
# =================== OUTPUTS ======================
# ==================================================
CfnOutput(
scope=self,
id="LoadBalancerDNS",
value=fargate_service.load_balancer.load_balancer_dns_name,
)
app = App()
MLflowStack(app, "MLflowStack")
app.synth()