AWS and boto3 recipes for Python
To list metrics in CloudWatch, first create a CloudWatch client. Next, use the get_paginator() function to get the paginator object. This object allows you to query CloudWatch using the pagination interface. Finally, use the paginate() function to iterate through the results of the query. Note that the query uses the Dimensions and MetricName parameters to specify the query parameters. The Namespace parameter specifies the namespace under which the results should be retrieved.
Library: boto3
import boto3
# Create CloudWatch client
cloudwatch = boto3.client('cloudwatch')
# List metrics through the pagination interface
paginator = cloudwatch.get_paginator('list_metrics')
for response in paginator.paginate(Dimensions=[{'Name': 'loggroupname'}],
MetricName='IncomingLogEvents',
Namespace='AWS/Logs'):
print(response['Metrics'])