Home > OS >  AWS cloudwatch get-metric-statistics S3 BytesDownloaded returns nothing and I cannot make it fail
AWS cloudwatch get-metric-statistics S3 BytesDownloaded returns nothing and I cannot make it fail

Time:12-25

I know that AWS is collecting data because the link in the S3 Metrics page to the CloudWatch GUI works and the monthly sum of downloaded bytes is correct.

But I cannot get the CLI to return any data. This "should" work to get a monthly view of bytes downloaded:

aws cloudwatch get-metric-statistics --metric-name BytesDownloaded --namespace AWS/S3 --statistics Sum --start-time 2022-12-01T00:00:00Z --end-time 2022-12-31T00:00:00Z --period 2678400 --dimensions '[{"Name":"BucketName","Value":"chess-bgn"}]'

But it yields only this:

{
    "Label": "BytesDownloaded",
    "Datapoints": []
}

Changing the start-time, end-time, and period to any other (valid) values has no effect. No errors, and no data. Furthermore, I can change the metric-name to anything and it still yields empty data:

aws cloudwatch get-metric-statistics --metric-name PopcornShrimp --namespace AWS/S3 --statistics Average --start-time 2022-12-01T00:00:00Z --end-time 2022-12-31T00:00:00Z --period 2678400 --output json 
{
    "Label": "PopcornShrimp",
    "Datapoints": []
}

Any clues? All my other AWS CLI commands to create instances and disks, move content to and from S3, etc. all work fine so it is not a basic authentication or account issue.

CodePudding user response:

While there are exceptions, CloudWatch metrics "treats each unique combination of dimensions as a separate metric, even if the metrics have the same metric name. You can only retrieve statistics using combinations of dimensions that you specifically published"

This means you need to specify all the available dimensions for a given metric. For S3, that often includes both BucketName and FilterId:

$ aws cloudwatch list-metrics --namespace AWS/S3 --metric-name BytesDownloaded
{
    "Metrics": [
        {
            "Namespace": "AWS/S3",
            "MetricName": "BytesDownloaded",
            "Dimensions": [
                {
                    "Name": "BucketName",
                    "Value": "example-bucket"
                },
                {
                    "Name": "FilterId",
                    "Value": "EntireBucket"
                }
            ]
        }
    ]
}

Though, of course if you have custom metrics setup, you will have different options for your FilterId. EntireBucket is a common default, though.

This means you'll need to specify the both dimensions when running the query:

$ aws cloudwatch get-metric-statistics \
    --metric-name BytesDownloaded \
    --namespace AWS/S3 \
    --statistics Sum \
    --start-time 2022-12-01T00:00:00Z \
    --end-time 2022-12-31T00:00:00Z \
    --period 2678400 \
    --dimensions '[{"Name":"BucketName","Value":"example-bucket"},{"Name":"FilterId","Value":"EntireBucket"}]'

{
    "Label": "BytesDownloaded",
    "Datapoints": [
        {
            "Timestamp": "2022-12-01T00:00:00 00:00",
            "Sum": 1234567.0,
            "Unit": "Bytes"
        }
    ]
}

And, I will note I'm showing quoting for Linux and macOS, different shells on other operating systems have different rules.

  • Related