def get_task_cnt():
"""
ECSタスク数の取得を行う
Returns:ECSタスク数(int)
"""
cloudwatch = boto3.client('cloudwatch')
response = cloudwatch.get_metric_data(
MetricDataQueries=[
{
'Id': 'm1',
'MetricStat': {
'Metric': {
'Namespace': 'ECS/ContainerInsights',
'MetricName': 'TaskCount',
'Dimensions': [
{
'Name': 'ClusterName',
'Value': ECS_CLUSTER_NAME
},
]
},
'Period': 300,
'Stat': 'Average',
'Unit': 'Count'
},
},
],
StartTime=datetime.datetime.now() - datetime.timedelta(minutes=1),
EndTime=datetime.datetime.now()
)
values = response['MetricDataResults'][0]['Values']
if len(values) != 0:
value = int(response['MetricDataResults'][0]['Values'][0])
return value
参考: