Search code examples
amazon-web-servicesboto3amazon-rdsamazon-cloudwatchamazon-cloudwatch-metrics

Fetch AWS Cloudwatch metrics for all RDS instance using Wildcard


For some alerting and monitoring purpose, I am trying to fetch RDS cloudwatch metrics. There are around ~50 RDS instances and would like to fetch metrics for all of them in one API call(I am using boto3). This is my code :

response = cloudwatch_client.get_metric_data(
    MetricDataQueries=[
        {
            'Id': 'fetching_data_for_something',
            'MetricStat': {
                'Metric': {
                    'Namespace': 'AWS/RDS',
                    'MetricName': 'FreeStorageSpace',
                    'Dimensions': [
                        {
                            'Name': 'DBInstanceIdentifier',
                            'Value': '*'
                        },
                    ]
                },
                'Period': 300,
                'Stat': 'Average'
            },
            'ReturnData': True
        },
    ],
    StartTime=datetime(2019, 6, 11,13,0,0),
    EndTime=datetime(2019, 6, 11,13,20,00),
    ScanBy='TimestampDescending',
    MaxDatapoints=123
) 

But this is returning an empty result, but when I am searching for a particular DB instance, it is returning the result. This is working :

'Dimensions': [
{
    'Name': 'DBInstanceIdentifier',
    'Value': 'name_of_db'
},
]

But, this isn't :

'Dimensions': [
{
    'Name': 'DBInstanceIdentifier',
    'Value': '*'
},
]

Is there any way to accomplish this? To get metrics for all DBs at once?


Solution

  • You can do it with metric math expressions, using the SEARCH function. See here for more details: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/using-search-expressions.html

    Your expression will look something like this:

    SEARCH('{AWS/RDS,DBInstanceIdentifier} MetricName="FreeStorageSpace"', 'Average', 300)
    

    Full boto3 example:

    response = cloudwatch_client.get_metric_data(
        MetricDataQueries=[
            {
                'Id': 'fetching_data_for_something',
                'Expression': "SEARCH('{AWS/RDS,DBInstanceIdentifier} MetricName=\"FreeStorageSpace\"', 'Average', 300)",
                'ReturnData': True
            },
        ],
        StartTime=datetime(2019, 6, 11,13,0,0),
        EndTime=datetime(2019, 6, 11,13,20,0,0),
        ScanBy='TimestampDescending',
        MaxDatapoints=123
    )