如何查询所有(1000+)EC2实例在用BOTO3的指定时间范围内运行的EC2实例?

发布于 2025-01-26 10:19:35 字数 1507 浏览 1 评论 0原文

我想查询我帐户(1000+)中所有EC2实例的CloudWatch指标,该实例在使用BOTO3的特定1小时时间范围(负载测试)中报告它们。预计在测试期间会通过自动缩放来提高/向下提出实例。

我尝试使用查询RDS实例的此堆叠式答案,但这仅限于500个结果,无法根据

搜索表达限制

最大搜索表达查询大小为1024个字符。您可以在一个图上具有多达100个搜索表达式。图可以显示多达500个时间序列。

我可以通过使用类似的内容来指定实例ID来对指标进行多个查询:

for instance_id in instance_ids:
    response = boto3.client("cloudwatch").get_metric_data(
        MetricDataQueries=[
            {
                'Id': "network_out",
                'MetricStat': {
                    'Metric': {
                        'Namespace':
                            "AWS/EC2",
                        'MetricName':
                            'NetworkOut',
                        'Dimensions': [{
                            'Name': 'InstanceId',
                            'Value': instance_id
                        }]
                    },
                    'Period': 300,
                    'Stat': 'Sum',
                    'Unit': 'Bytes'
                }
                'ReturnData': True
            }
        ]
    )

但是我仍然需要拥有在时间范围内报告指标的instance_ids的完整列表,包括那些

如何查询所有 ec2实例,如何查询CloudWatch指标?

获取所有在此时间范围内报告指标的EC2实例列表的方法也将起作用。

I want to query CloudWatch metrics for all EC2 instances in my account (1000+) that reported them during a specific 1 hour time frame (load test) using Boto3. It is expected that instances would have been brought up/down by auto-scaling during the test.

I've tried using search expression as suggested in this StackOverflow answer for querying RDS instances, but this is limited to 500 results with no way to paginate according to the CloudWatch search expression syntax docs:

Search expression limits

The maximum search expression query size is 1024 characters. You can have as many as 100 search expressions on one graph. A graph can display as many as 500 time series.

I could do multiple queries for metrics by specifying instance ids using something like this:

for instance_id in instance_ids:
    response = boto3.client("cloudwatch").get_metric_data(
        MetricDataQueries=[
            {
                'Id': "network_out",
                'MetricStat': {
                    'Metric': {
                        'Namespace':
                            "AWS/EC2",
                        'MetricName':
                            'NetworkOut',
                        'Dimensions': [{
                            'Name': 'InstanceId',
                            'Value': instance_id
                        }]
                    },
                    'Period': 300,
                    'Stat': 'Sum',
                    'Unit': 'Bytes'
                }
                'ReturnData': True
            }
        ]
    )

but I would still need to have the full list of instance_ids that reported metrics during the time frame, including those which were terminated by auto-scaling.

How can I query CloudWatch metrics for all EC2 instances which reported during the time frame?

A way to get a list of all EC2 instances which reported metrics during that time frame would also work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文