-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
281 lines (252 loc) · 10.3 KB
/
app.py
File metadata and controls
281 lines (252 loc) · 10.3 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import pathlib
import hashlib
from aws_cdk import (
aws_events as events,
aws_lambda as lambda_,
aws_iam as iam,
aws_s3 as s3,
aws_s3_notifications as s3_notifications,
aws_events_targets as targets,
aws_secretsmanager as secretsmanager,
CfnParameter,
SecretValue,
App,
BundlingOptions,
Duration,
Stack,
RemovalPolicy,
)
class DaskLambdaExampleStack(Stack):
# Bucket to store example data for processing
bucket: s3.Bucket
# Dummy producer of files for processing
lambda_producer: lambda_.Function
# Example connecting to existing cluster from AWS Lambda
# and coordinating some work
lambda_consumer: lambda_.Function
# Function that starts/stops Dask clusters
lambda_start_stop_cluster: lambda_.Function
# Shared file between Dask workers and Lambda client
dask_processing_layer: lambda_.LayerVersion
# Dask dependencies for client, (dask, distributed, coiled)
dask_dependencies_layer: lambda_.LayerVersion
def __init__(self, app: App, id: str) -> None:
super().__init__(app, id)
self.coiled_token = CfnParameter(
self,
"CoiledToken",
type="String",
description="Coiled Token: realistically, already saved and retreive from SecretsManger",
)
self.coiled_account = CfnParameter(
self,
"CoiledAccount",
type="String",
description="Coiled Account: realistically, already saved and retreive from SecretsManger",
)
self.coiled_user = CfnParameter(
self,
"CoiledUser",
type="String",
description="Coiled User: realistically, already saved and retreive from SecretsManger",
)
self.make_bucket()
self.make_secret()
self.make_dask_dependencies_layer()
self.make_dask_processing_layer()
self.make_lambda_producer()
self.make_lambda_consumer()
self.make_lambda_start_stop_cluster()
def make_bucket(self):
self.bucket = s3.Bucket(
self,
"example2-data-bucket",
removal_policy=RemovalPolicy.DESTROY,
block_public_access=s3.BlockPublicAccess.BLOCK_ALL,
)
self.bucket.add_to_resource_policy(
iam.PolicyStatement(
sid="AllowReadingFromThisAccount",
effect=iam.Effect.ALLOW,
principals=[iam.AccountRootPrincipal()],
actions=["s3:List*", "s3:Get*"],
resources=[
self.bucket.bucket_arn,
f"{self.bucket.bucket_arn}/*",
],
)
)
def make_secret(self):
self.secret = secretsmanager.Secret(
self,
"Secret",
description="Connection information to running Dask Cluster",
secret_object_value={
"CLUSTER_NAME": SecretValue.unsafe_plain_text(""),
"SCHEDULER_ADDR": SecretValue.unsafe_plain_text(""),
"DASHBOARD_ADDR": SecretValue.unsafe_plain_text(""),
},
)
def make_lambda_producer(self):
src_file = pathlib.Path(__file__).parent.joinpath("src/lambda_producer.py")
self.lambda_producer = lambda_.Function(
self,
"ExampleProducerFunction",
description="Example producer function. Generating example data for processing",
code=lambda_.InlineCode(src_file.read_text()),
handler="index.producer",
timeout=Duration.seconds(5),
runtime=lambda_.Runtime.PYTHON_3_10,
environment={
"S3_BUCKET": self.bucket.bucket_name,
"SECRET_ARN": self.secret.secret_arn,
},
)
# Allow this function to write to s3
self.lambda_producer.add_to_role_policy(
iam.PolicyStatement(
effect=iam.Effect.ALLOW,
actions=["s3:PutObject"],
resources=[self.bucket.bucket_arn, f"{self.bucket.bucket_arn}/*"],
)
)
# Invoke at regular intervals to produce new files
rule = events.Rule(
self, "S3ProducerRule", schedule=events.Schedule.rate(Duration.minutes(1))
)
rule.add_target(targets.LambdaFunction(self.lambda_producer))
def make_lambda_consumer(self):
src_file = pathlib.Path(__file__).parent.joinpath("src/lambda_consumer.py")
self.lambda_consumer = lambda_.Function(
self,
"ExampleConsumerFunction",
description="Example of Dask client from Lambda, coordinating work on remote cluster",
code=lambda_.InlineCode(src_file.read_text()),
handler="index.consumer",
# time waiting for cluster to process task, can be lower with 'fire-and-forget'
timeout=Duration.minutes(5),
runtime=lambda_.Runtime.PYTHON_3_10,
layers=[self.dask_processing_layer, self.dask_dependencies_layer],
environment={
"SECRET_ARN": self.secret.secret_arn,
"DASK_COILED__TOKEN": self.coiled_token.value_as_string,
"DASK_COILED__ACCOUNT": self.coiled_account.value_as_string,
"DASK_COILED__USER": self.coiled_user.value_as_string,
},
)
# Get cluster connection info permission
self.lambda_consumer.add_to_role_policy(
iam.PolicyStatement(
effect=iam.Effect.ALLOW,
actions=["secretsmanager:GetSecretValue"],
resources=[self.secret.secret_arn],
)
)
self.lambda_consumer.add_to_role_policy(
iam.PolicyStatement(
effect=iam.Effect.ALLOW,
actions=["s3:Get*", "s3:List*"],
resources=[self.bucket.arn_for_objects("*"), self.bucket.bucket_arn],
)
)
# Trigger consumer
notification = s3_notifications.LambdaDestination(self.lambda_consumer)
self.bucket.add_event_notification(s3.EventType.OBJECT_CREATED, notification)
# Add bucket policy that this function, and thus cluster's inherited role
# will have access to read the bucket
self.bucket.add_to_resource_policy(
iam.PolicyStatement(
sid="AllowReadingFromForLambdaConsumerRole",
effect=iam.Effect.ALLOW,
principals=[iam.ArnPrincipal(self.lambda_consumer.role.role_arn)],
actions=["s3:List*", "s3:Get*"],
resources=[
self.bucket.bucket_arn,
f"{self.bucket.bucket_arn}/*",
],
)
)
def make_lambda_start_stop_cluster(self):
src_file = pathlib.Path(__file__).parent.joinpath("src/lambda_consumer.py")
self.lambda_start_stop_cluster = lambda_.Function(
self,
"StartStopClusterFunction",
description="Example of starting and stopping a Dask cluster using coiled",
code=lambda_.InlineCode(src_file.read_text()),
handler="index.start_stop_cluster",
timeout=Duration.minutes(10), # Time for creating software envs / cluster
runtime=lambda_.Runtime.PYTHON_3_10,
memory_size=1024,
layers=[self.dask_processing_layer, self.dask_dependencies_layer],
environment={
"SECRET_ARN": self.secret.secret_arn,
"INSTALLED_PKGS": pathlib.Path("requirements.txt").read_text(),
"DASK_COILED__TOKEN": self.coiled_token.value_as_string,
"DASK_COILED__ACCOUNT": self.coiled_account.value_as_string,
"DASK_COILED__USER": self.coiled_user.value_as_string,
},
)
# Update cluster connection info permission
self.lambda_start_stop_cluster.add_to_role_policy(
iam.PolicyStatement(
effect=iam.Effect.ALLOW,
actions=[
"secretsmanager:PutSecretValue",
"secretsmanager:GetSecretValue",
],
resources=[self.secret.secret_arn],
)
)
# Trigger start/stop cluster
for action, schedule in (
("start", "cron(0 7 ? * 1-6 *)"), # Weekdays 7am
("stop", "cron(0 17 ? * 1-6 *)"), # Weekdays 5pm
):
events.Rule(
self,
f"{action.capitalize()}Cluster",
description=f"Schedule {action} of Dask cluster",
schedule=events.Schedule.expression(schedule),
targets=[
targets.LambdaFunction(
self.lambda_start_stop_cluster,
event=events.RuleTargetInput.from_object({"action": action}),
)
],
)
def make_dask_processing_layer(self):
"""
Layer which bridges Lambda client with code running on workers
"""
self.dask_processing_layer = lambda_.LayerVersion(
self,
"dask_processing_layer",
code=lambda_.Code.from_asset("layer"),
description="Code for client and dask workers",
compatible_runtimes=[lambda_.Runtime.PYTHON_3_10],
removal_policy=RemovalPolicy.DESTROY,
)
def make_dask_dependencies_layer(self):
"""
Layer which bridges Lambda client with code running on workers
"""
command = "pip install --no-cache -r requirements.txt -t /asset-output/python"
asset_key = pathlib.Path("./requirements.txt").read_bytes() + command.encode()
self.dask_dependencies_layer = lambda_.LayerVersion(
self,
"dask_dependencies_layer",
code=lambda_.Code.from_asset(
"./",
asset_hash=hashlib.md5(asset_key).hexdigest(),
bundling=BundlingOptions(
image=lambda_.Runtime.PYTHON_3_10.bundling_image,
command=["bash", "-c", command],
),
),
description="Dask dependencies (coiled, dask, distributed, etc)",
compatible_runtimes=[lambda_.Runtime.PYTHON_3_10],
removal_policy=RemovalPolicy.DESTROY,
)
app = App()
DaskLambdaExampleStack(app, "DaskLambdaExampleStack")
app.synth()