Repository navigation
Expand file tree
/
Copy pathecs_wrapper.py
More file actions
550 lines (502 loc) · 19.9 KB
/
Copy pathecs_wrapper.py
File metadata and controls
550 lines (502 loc) · 19.9 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Amazon ECS wrapper class that encapsulates Amazon Elastic Container Service operations.
"""
import logging
from typing import Any, Dict, List, Optional
import boto3
from botocore.client import BaseClient
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)
# snippet-start:[python.example_code.ecs.EcsWrapper.complete]
# snippet-start:[python.example_code.ecs.EcsWrapper.decl]
class EcsWrapper:
"""Encapsulates Amazon ECS operations."""
def __init__(self, ecs_client: BaseClient):
"""
Initializes the EcsWrapper with an ECS client.
:param ecs_client: A Boto3 Amazon ECS client. Boto3 clients are created
by the ``boto3.client`` factory function and are instances of
``botocore.client.BaseClient``, which is the correct type to
annotate here (``boto3.client`` itself is a function, not a type).
"""
self.ecs_client = ecs_client
@classmethod
def from_client(cls) -> "EcsWrapper":
"""Creates an EcsWrapper using a default Boto3 ECS client."""
ecs_client = boto3.client("ecs")
return cls(ecs_client)
# snippet-end:[python.example_code.ecs.EcsWrapper.decl]
# snippet-start:[python.example_code.ecs.CreateCluster]
def create_cluster(self, cluster_name: str) -> Dict[str, Any]:
"""
Creates a new Amazon ECS cluster.
:param cluster_name: The name of the cluster to create.
:return: The cluster details from the response.
:raises ClientError: If the request fails (e.g., InvalidParameterException).
"""
try:
response = self.ecs_client.create_cluster(
clusterName=cluster_name,
settings=[
{"name": "containerInsights", "value": "enabled"},
],
)
cluster = response["cluster"]
logger.info(
"Created cluster '%s' with ARN: %s",
cluster["clusterName"],
cluster["clusterArn"],
)
return cluster
except ClientError as err:
if err.response["Error"]["Code"] == "InvalidParameterException":
logger.error(
"Invalid parameter when creating cluster '%s': %s",
cluster_name,
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.ecs.CreateCluster]
# snippet-start:[python.example_code.ecs.RegisterTaskDefinition]
def register_task_definition(
self,
family: str,
execution_role_arn: str,
container_definitions: Optional[List[Dict[str, Any]]] = None,
cpu: str = "256",
memory: str = "512",
) -> Dict[str, Any]:
"""
Registers a new task definition for Fargate.
:param family: The family name for the task definition.
:param execution_role_arn: The ARN of the task execution IAM role.
:param container_definitions: List of container definitions. If None, a
default httpd container is used.
:param cpu: The task-level CPU units (e.g., '256').
:param memory: The task-level memory in MiB (e.g., '512').
:return: The task definition details from the response.
:raises ClientError: If the request fails (e.g., InvalidParameterException).
"""
if container_definitions is None:
container_definitions = [
{
"name": "sample-app",
"image": "public.ecr.aws/docker/library/httpd:2.4",
"essential": True,
"portMappings": [
{
"containerPort": 80,
"hostPort": 80,
"protocol": "tcp",
}
],
"cpu": 256,
"memory": 512,
}
]
try:
response = self.ecs_client.register_task_definition(
family=family,
networkMode="awsvpc",
requiresCompatibilities=["FARGATE"],
cpu=cpu,
memory=memory,
executionRoleArn=execution_role_arn,
containerDefinitions=container_definitions,
)
task_def = response["taskDefinition"]
logger.info(
"Registered task definition '%s' revision %s, ARN: %s",
task_def["family"],
task_def["revision"],
task_def["taskDefinitionArn"],
)
return task_def
except ClientError as err:
if err.response["Error"]["Code"] == "InvalidParameterException":
logger.error(
"Invalid parameter when registering task definition '%s': %s",
family,
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.ecs.RegisterTaskDefinition]
# snippet-start:[python.example_code.ecs.DescribeClusters]
def describe_clusters(self, cluster_names: List[str]) -> List[Dict[str, Any]]:
"""
Describes one or more ECS clusters.
:param cluster_names: A list of cluster names or ARNs.
:return: A list of cluster details.
:raises ClientError: If the request fails (e.g., ClusterNotFoundException).
"""
try:
response = self.ecs_client.describe_clusters(
clusters=cluster_names,
include=["STATISTICS"],
)
clusters = response.get("clusters", list())
for cluster in clusters:
logger.info(
"Cluster '%s': status=%s, active_services=%s, running_tasks=%s",
cluster["clusterName"],
cluster["status"],
cluster["activeServicesCount"],
cluster["runningTasksCount"],
)
return clusters
except ClientError as err:
if err.response["Error"]["Code"] == "ClusterNotFoundException":
logger.error(
"Cluster(s) not found: %s. %s",
cluster_names,
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.ecs.DescribeClusters]
# snippet-start:[python.example_code.ecs.RunTask]
def run_task(
self,
cluster: str,
task_definition: str,
subnets: List[str],
security_groups: List[str],
count: int = 1,
) -> List[Dict[str, Any]]:
"""
Runs a standalone Fargate task.
:param cluster: The cluster name or ARN.
:param task_definition: The task definition family or family:revision.
:param subnets: List of subnet IDs.
:param security_groups: List of security group IDs.
:param count: Number of tasks to run.
:return: A list of task details.
:raises ClientError: If the request fails (e.g., ClusterNotFoundException).
"""
try:
response = self.ecs_client.run_task(
cluster=cluster,
taskDefinition=task_definition,
launchType="FARGATE",
count=count,
networkConfiguration={
"awsvpcConfiguration": {
"subnets": subnets,
"securityGroups": security_groups,
"assignPublicIp": "ENABLED",
}
},
)
tasks = response.get("tasks", list())
failures = response.get("failures", list())
if failures:
logger.warning("Task run failures: %s", failures)
for task in tasks:
logger.info(
"Started task '%s' with status '%s'",
task["taskArn"],
task["lastStatus"],
)
return tasks
except ClientError as err:
if err.response["Error"]["Code"] == "ClusterNotFoundException":
logger.error(
"Cluster '%s' not found: %s",
cluster,
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.ecs.RunTask]
# snippet-start:[python.example_code.ecs.DescribeTasks]
def describe_tasks(
self, cluster: str, task_arns: List[str]
) -> List[Dict[str, Any]]:
"""
Describes the specified tasks.
:param cluster: The cluster name or ARN.
:param task_arns: A list of task ARNs.
:return: A list of task details.
:raises ClientError: If the request fails (e.g., ClusterNotFoundException).
"""
try:
response = self.ecs_client.describe_tasks(
cluster=cluster,
tasks=task_arns,
)
tasks = response.get("tasks", list())
for task in tasks:
logger.info(
"Task '%s': status=%s, desired_status=%s",
task["taskArn"],
task["lastStatus"],
task["desiredStatus"],
)
return tasks
except ClientError as err:
if err.response["Error"]["Code"] == "ClusterNotFoundException":
logger.error(
"Cluster '%s' not found when describing tasks: %s",
cluster,
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.ecs.DescribeTasks]
# snippet-start:[python.example_code.ecs.CreateService]
def create_service(
self,
cluster: str,
service_name: str,
task_definition: str,
desired_count: int,
subnets: List[str],
security_groups: List[str],
) -> Dict[str, Any]:
"""
Creates a service that maintains a desired count of running tasks.
:param cluster: The cluster name or ARN.
:param service_name: The name for the service.
:param task_definition: The task definition family or family:revision.
:param desired_count: The number of task instances to maintain.
:param subnets: List of subnet IDs.
:param security_groups: List of security group IDs.
:return: The service details from the response.
:raises ClientError: If the request fails (e.g., InvalidParameterException).
"""
try:
response = self.ecs_client.create_service(
cluster=cluster,
serviceName=service_name,
taskDefinition=task_definition,
desiredCount=desired_count,
launchType="FARGATE",
networkConfiguration={
"awsvpcConfiguration": {
"subnets": subnets,
"securityGroups": security_groups,
"assignPublicIp": "ENABLED",
}
},
)
service = response["service"]
logger.info(
"Created service '%s' (ARN: %s) with desired count %s",
service["serviceName"],
service["serviceArn"],
service["desiredCount"],
)
return service
except ClientError as err:
if err.response["Error"]["Code"] == "InvalidParameterException":
logger.error(
"Invalid parameter when creating service '%s': %s",
service_name,
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.ecs.CreateService]
# snippet-start:[python.example_code.ecs.ListTasks]
def list_tasks(
self,
cluster: str,
service_name: Optional[str] = None,
) -> List[str]:
"""
Lists task ARNs for a cluster, optionally filtered by service.
Uses paginator to handle large result sets.
:param cluster: The cluster name or ARN.
:param service_name: The service name to filter by (optional).
:return: A list of task ARN strings.
:raises ClientError: If the request fails (e.g., InvalidParameterException).
"""
try:
task_arns = list()
paginator = self.ecs_client.get_paginator("list_tasks")
params = dict()
params["cluster"] = cluster
if service_name is not None:
params["serviceName"] = service_name
for page in paginator.paginate(**params):
task_arns.extend(page.get("taskArns", list()))
logger.info(
"Listed %d tasks for cluster '%s'%s",
len(task_arns),
cluster,
f" (service='{service_name}')" if service_name else "",
)
return task_arns
except ClientError as err:
if err.response["Error"]["Code"] == "InvalidParameterException":
logger.error(
"Invalid parameter when listing tasks: %s",
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.ecs.ListTasks]
# snippet-start:[python.example_code.ecs.DescribeServices]
def describe_services(
self, cluster: str, service_names: List[str]
) -> List[Dict[str, Any]]:
"""
Describes the specified services running in a cluster.
:param cluster: The cluster name or ARN.
:param service_names: A list of service names or ARNs.
:return: A list of service details.
:raises ClientError: If the request fails (e.g., ClusterNotFoundException).
"""
try:
response = self.ecs_client.describe_services(
cluster=cluster,
services=service_names,
)
services = response.get("services", list())
for svc in services:
logger.info(
"Service '%s': status=%s, desired=%s, running=%s",
svc["serviceName"],
svc["status"],
svc["desiredCount"],
svc["runningCount"],
)
return services
except ClientError as err:
if err.response["Error"]["Code"] == "ClusterNotFoundException":
logger.error(
"Cluster '%s' not found when describing services: %s",
cluster,
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.ecs.DescribeServices]
# snippet-start:[python.example_code.ecs.UpdateService]
def update_service(
self,
cluster: str,
service: str,
desired_count: int,
) -> Dict[str, Any]:
"""
Updates the desired count for a service.
:param cluster: The cluster name or ARN.
:param service: The service name or ARN.
:param desired_count: The new desired task count.
:return: The updated service details.
:raises ClientError: If the request fails (e.g., ServiceNotFoundException).
"""
try:
response = self.ecs_client.update_service(
cluster=cluster,
service=service,
desiredCount=desired_count,
)
svc = response["service"]
logger.info(
"Updated service '%s' desired count to %s",
svc["serviceName"],
svc["desiredCount"],
)
return svc
except ClientError as err:
if err.response["Error"]["Code"] == "ServiceNotFoundException":
logger.error(
"Service '%s' not found in cluster '%s': %s",
service,
cluster,
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.ecs.UpdateService]
# snippet-start:[python.example_code.ecs.DeleteService]
def delete_service(
self,
cluster: str,
service: str,
force: bool = True,
) -> Dict[str, Any]:
"""
Deletes a service from a cluster.
:param cluster: The cluster name or ARN.
:param service: The service name or ARN.
:param force: If True, deletes the service even if it has active tasks.
:return: The deleted service details.
:raises ClientError: If the request fails (e.g., ServiceNotFoundException).
"""
try:
response = self.ecs_client.delete_service(
cluster=cluster,
service=service,
force=force,
)
svc = response["service"]
logger.info(
"Deleted service '%s' from cluster '%s'",
svc["serviceName"],
cluster,
)
return svc
except ClientError as err:
if err.response["Error"]["Code"] == "ServiceNotFoundException":
logger.error(
"Service '%s' not found (may already be deleted): %s",
service,
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.ecs.DeleteService]
# snippet-start:[python.example_code.ecs.DeregisterTaskDefinition]
def deregister_task_definition(self, task_definition: str) -> Dict[str, Any]:
"""
Deregisters a task definition.
:param task_definition: The family:revision of the task definition.
:return: The deregistered task definition details.
:raises ClientError: If the request fails (e.g., InvalidParameterException).
"""
try:
response = self.ecs_client.deregister_task_definition(
taskDefinition=task_definition,
)
task_def = response["taskDefinition"]
logger.info(
"Deregistered task definition '%s' (status: %s)",
task_def["taskDefinitionArn"],
task_def["status"],
)
return task_def
except ClientError as err:
if err.response["Error"]["Code"] == "InvalidParameterException":
logger.error(
"Invalid parameter when deregistering task definition '%s': %s",
task_definition,
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.ecs.DeregisterTaskDefinition]
# snippet-start:[python.example_code.ecs.DeleteCluster]
def delete_cluster(self, cluster: str) -> Dict[str, Any]:
"""
Deletes an ECS cluster.
:param cluster: The cluster name or ARN.
:return: The deleted cluster details.
:raises ClientError: If the request fails (e.g., ClusterContainsServicesException).
"""
try:
response = self.ecs_client.delete_cluster(
cluster=cluster,
)
cluster_info = response["cluster"]
logger.info(
"Deleted cluster '%s' (status: %s)",
cluster_info["clusterName"],
cluster_info["status"],
)
return cluster_info
except ClientError as err:
if err.response["Error"]["Code"] == "ClusterContainsServicesException":
logger.error(
"Cluster '%s' still contains services. Delete services first: %s",
cluster,
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.ecs.DeleteCluster]
# snippet-end:[python.example_code.ecs.EcsWrapper.complete]