-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathairflow.py
More file actions
253 lines (234 loc) · 8.88 KB
/
airflow.py
File metadata and controls
253 lines (234 loc) · 8.88 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import os
from aws_cdk import (
core,
aws_ecs as ecs,
aws_secretsmanager as sm,
aws_elasticloadbalancingv2 as elbv2,
aws_servicediscovery as sd,
)
from stacks.vpc_stack import VpcStack
from stacks.airflow_cluster_stack import AirflowClusterStack
from stacks.ecr_stack import ECRStack
from stacks.airflow_rds import RDSStack
from stacks.airflow_redis import RedisStack
from types import SimpleNamespace
from typing_extensions import TypedDict
props_type = TypedDict(
"props_type",
{
"airflow_cluster": AirflowClusterStack,
"vpc": VpcStack,
"ecr": ECRStack,
"rds": RDSStack,
"redis": RedisStack,
},
)
class AirflowServices(core.Stack):
def __init__(
self, scope: core.Construct, id: str, props: props_type, **kwargs
) -> None:
super().__init__(scope, id, **kwargs)
ns = SimpleNamespace(**props)
bucket_name = os.environ.get("BUCKET_NAME")
fernet_key_secret = sm.Secret.from_secret_arn(
self, "fernetSecret", os.environ.get("FERNET_SECRET_ARN")
)
webserver_ns = sd.PrivateDnsNamespace(
self,
"webserver-dns-namespace",
vpc=ns.vpc.instance,
name="airflow",
description="Private DNS for Airflow webserver",
)
# Webserver
webserver_task = ecs.FargateTaskDefinition(
self,
"webserver-cdk",
family="webserver-cdk",
cpu=512,
memory_limit_mib=1024,
task_role=ns.airflow_cluster.airflow_task_role,
execution_role=ns.airflow_cluster.task_execution_role,
)
webserver_container = webserver_task.add_container(
"webserver-cdk-container",
image=ecs.ContainerImage.from_ecr_repository(
ns.ecr.airflow_webserver_repo,
os.environ.get("IMAGE_TAG", "latest"),
),
logging=ecs.AwsLogDriver(
stream_prefix="ecs", log_group=ns.airflow_cluster.webserver_log_group
),
environment={
"AIRFLOW_DATABASE_NAME": ns.rds.db_name,
"AIRFLOW_DATABASE_PORT_NUMBER": "5432",
"AIRFLOW_DATABASE_HOST": ns.rds.instance.db_instance_endpoint_address,
"AIRFLOW_EXECUTOR": "CeleryExecutor",
"AIRFLOW_LOAD_EXAMPLES": "no",
"AIRFLOW__SCHEDULER__DAG_DIR_LIST_INTERVAL": "30",
"BUCKET_NAME": bucket_name,
},
secrets={
"AIRFLOW_DATABASE_USERNAME": ecs.Secret.from_secrets_manager(
ns.rds.rds_secret, field="username"
),
"AIRFLOW_DATABASE_PASSWORD": ecs.Secret.from_secrets_manager(
ns.rds.rds_secret, field="password"
),
"AIRFLOW_FERNET_KEY": ecs.Secret.from_secrets_manager(
fernet_key_secret
),
},
)
ws_port_mapping = ecs.PortMapping(
container_port=8080, host_port=8080, protocol=ecs.Protocol.TCP
)
webserver_container.add_port_mappings(ws_port_mapping)
# Webserver service
webserver_service = ecs.FargateService(
self,
"webserverService",
service_name="webserver_cdk",
cluster=ns.airflow_cluster.instance,
task_definition=webserver_task,
desired_count=1,
security_group=ns.vpc.airflow_sg,
assign_public_ip=False,
cloud_map_options=ecs.CloudMapOptions(
cloud_map_namespace=webserver_ns,
name="webserver",
dns_record_type=sd.DnsRecordType.A,
dns_ttl=core.Duration.seconds(30),
),
)
# Scheduler
scheduler_task = ecs.FargateTaskDefinition(
self,
"scheduler-cdk",
family="scheduler-cdk",
cpu=512,
memory_limit_mib=2048,
task_role=ns.airflow_cluster.airflow_task_role,
execution_role=ns.airflow_cluster.task_execution_role,
)
scheduler_task.add_container(
"scheduler-cdk-container",
image=ecs.ContainerImage.from_ecr_repository(
ns.ecr.airflow_scheduler_repo,
os.environ.get("IMAGE_TAG", "latest"),
),
logging=ecs.AwsLogDriver(
stream_prefix="ecs", log_group=ns.airflow_cluster.scheduler_log_group
),
environment={
"AIRFLOW_DATABASE_NAME": ns.rds.db_name,
"AIRFLOW_DATABASE_PORT_NUMBER": "5432",
"AIRFLOW_DATABASE_HOST": ns.rds.instance.db_instance_endpoint_address,
"AIRFLOW_EXECUTOR": "CeleryExecutor",
"AIRFLOW_WEBSERVER_HOST": "webserver.airflow",
"AIRFLOW_LOAD_EXAMPLES": "no",
"AIRFLOW__SCHEDULER__DAG_DIR_LIST_INTERVAL": "30",
"REDIS_HOST": ns.redis.instance.attr_redis_endpoint_address,
"BUCKET_NAME": bucket_name,
},
secrets={
"AIRFLOW_DATABASE_USERNAME": ecs.Secret.from_secrets_manager(
ns.rds.rds_secret, field="username"
),
"AIRFLOW_DATABASE_PASSWORD": ecs.Secret.from_secrets_manager(
ns.rds.rds_secret, field="password"
),
"AIRFLOW_FERNET_KEY": ecs.Secret.from_secrets_manager(
fernet_key_secret
),
},
)
# Scheduler service
ecs.FargateService(
self,
"schedulerService",
service_name="scheduler_cdk",
cluster=ns.airflow_cluster.instance,
task_definition=scheduler_task,
desired_count=1,
security_group=ns.vpc.airflow_sg,
assign_public_ip=False,
)
# Worker
worker_task = ecs.FargateTaskDefinition(
self,
"worker-cdk",
family="worker-cdk",
cpu=1024,
memory_limit_mib=3072,
task_role=ns.airflow_cluster.airflow_task_role,
execution_role=ns.airflow_cluster.task_execution_role,
)
worker_container = worker_task.add_container(
"worker-cdk-container",
image=ecs.ContainerImage.from_ecr_repository(
ns.ecr.airflow_worker_repo,
os.environ.get("IMAGE_TAG", "latest"),
),
logging=ecs.AwsLogDriver(
stream_prefix="ecs", log_group=ns.airflow_cluster.worker_log_group
),
environment={
"AIRFLOW_DATABASE_NAME": ns.rds.db_name,
"AIRFLOW_DATABASE_PORT_NUMBER": "5432",
"AIRFLOW_DATABASE_HOST": ns.rds.instance.db_instance_endpoint_address,
"AIRFLOW_EXECUTOR": "CeleryExecutor",
"AIRFLOW_WEBSERVER_HOST": "webserver.airflow",
"AIRFLOW__SCHEDULER__DAG_DIR_LIST_INTERVAL": "30",
"AIRFLOW_LOAD_EXAMPLES": "no",
"REDIS_HOST": ns.redis.instance.attr_redis_endpoint_address,
"BUCKET_NAME": bucket_name,
},
secrets={
"AIRFLOW_DATABASE_USERNAME": ecs.Secret.from_secrets_manager(
ns.rds.rds_secret, field="username"
),
"AIRFLOW_DATABASE_PASSWORD": ecs.Secret.from_secrets_manager(
ns.rds.rds_secret, field="password"
),
"AIRFLOW_FERNET_KEY": ecs.Secret.from_secrets_manager(
fernet_key_secret
),
},
)
worker_port_mapping = ecs.PortMapping(
container_port=8793, host_port=8793, protocol=ecs.Protocol.TCP
)
worker_container.add_port_mappings(worker_port_mapping)
# Worker service
ecs.FargateService(
self,
"workerService",
service_name="worker_cdk",
cluster=ns.airflow_cluster.instance,
task_definition=worker_task,
desired_count=1,
security_group=ns.vpc.airflow_sg,
assign_public_ip=False,
)
# ALB
lb = elbv2.ApplicationLoadBalancer(
self,
"LB",
vpc=ns.vpc.instance,
internet_facing=True,
security_group=ns.vpc.alb_sg,
)
listener = lb.add_listener("airflow-webserver-cdk-listener", port=80, open=True)
webserver_hc = elbv2.HealthCheck(
interval=core.Duration.seconds(60),
path="/health",
timeout=core.Duration.seconds(5),
)
# Attach ALB to ECS Service
listener.add_targets(
"airflow-webserver-cdk-default",
port=80,
targets=[webserver_service],
health_check=webserver_hc,
)