title: How do I query CloudWatch metrics for ALL (1000+) EC2 instances that ran during a specified timeframe with Boto3?
body: <p>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.</p>
  <p>I've tried using search expression as suggested in <a href="https://stackoverflow.com/questions/56593570/fetch-aws-cloudwatch-metrics-for-all-rds-instance-using-wildcard/56597261#56597261">this StackOverflow answer for querying RDS instances</a>, but this is limited to 500 results with no way to paginate according to the <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/search-expression-syntax.html" rel="nofollow noreferrer">CloudWatch search expression syntax docs</a>:</p>
  <blockquote>
  <p><strong>Search expression limits</strong></p>
  <p>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.</p>
  </blockquote>
  <p>I could do multiple queries for metrics by specifying instance ids using something like this:</p>
  <pre class="lang-py prettyprint-override"><code>for instance_id in instance_ids:
      response = boto3.client(&quot;cloudwatch&quot;).get_metric_data(
          MetricDataQueries=[
              {
                  'Id': &quot;network_out&quot;,
                  'MetricStat': {
                      'Metric': {
                          'Namespace':
                              &quot;AWS/EC2&quot;,
                          'MetricName':
                              'NetworkOut',
                          'Dimensions': [{
                              'Name': 'InstanceId',
                              'Value': instance_id
                          }]
                      },
                      'Period': 300,
                      'Stat': 'Sum',
                      'Unit': 'Bytes'
                  }
                  'ReturnData': True
              }
          ]
      )
  </code></pre>
  <p>but I would still need to have the full list of <code>instance_ids</code> that reported metrics during the time frame, <strong>including those which were terminated by auto-scaling.</strong></p>
  <p>How can I query CloudWatch metrics for <strong>all</strong> EC2 instances which reported during the time frame?</p>
  <p>A way to get a list of all EC2 instances which reported metrics during that time frame would also work.</p>