1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

cloudwatch.get_metric_dataを利用してECSで実行しているタスク数を取得する

Last updated at Posted at 2022-03-03

事前準備:https://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/monitoring/deploy-container-insights-ECS-cluster.html

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

参考:

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?