Search code examples
boto3aws-sdkamazon-cloudwatchboto

boto equivalent of aws client command


I have this command that works as expected. But I will like to use boto instead.

aws cloudwatch get-metric-statistics \
    --region ap-south-1 \
    --namespace AWS/RDS \
    --metric-name DBLoad  \
    --period 60 \
    --statistics Average \
    --start-time 2023-06-12T21:00:00Z \
    --end-time 2023-06-12T23:59:00Z \
    --dimensions Name=DBInstanceIdentifier,Value=sql-rds

Does boto support all the parameters?


Solution

  • The AWS CloudWatch functionality, including the get_metric_statistics operation, is available in Boto3

    See here: Boto3 documentation - CloudWatch Client - get_metric_statistics

    Using Boto3 - the code would be :

    import boto3
    from datetime import datetime, timedelta
    
    client = boto3.client('cloudwatch', region_name='ap-south-1')
    
    response = client.get_metric_statistics(
        Namespace='AWS/RDS',
        MetricName='DBLoad',
        Dimensions=[
            {
                'Name': 'DBInstanceIdentifier',
                'Value': 'sql-rds'
            },
        ],
        StartTime=datetime(2023, 6, 12, 21, 0, 0),
        EndTime=datetime(2023, 6, 12, 23, 59, 0),
        Period=60,
        Statistics=['Average']
    )
    
    print(response)
    

    N.B.
    Boto is no longer officially maintained by AWS
    Boto documentation might not be up-to-date
    It does not provide the same level of support for AWS services as Boto3.

    However, if you need to use Boto the documentation is here: boto.ec2.cloudwatch

    Using Boto - the code would be :

    import boto
    
    conn = boto.connect_cloudwatch()
    region = 'ap-south-1'
    
    namespace = 'AWS/RDS'
    metric_name = 'DBLoad'
    dimensions = {'DBInstanceIdentifier': 'sql-rds'}
    
    start_time = '2023-06-12T21:00:00Z'
    end_time = '2023-06-12T23:59:00Z'
    
    period = 60
    statistics = ['Average']
    
    response = conn.get_metric_statistics(
        period=period,
        start_time=start_time,
        end_time=end_time,
        metric_name=metric_name,
        namespace=namespace,
        statistics=statistics,
        dimensions=dimensions,
        region=region
    )
    
    print(response)