Cost Explorerを使ってCloudFront→ユーザーの転送量をaws cliで出力する

CloudFrontのデータ転送量を調べます。それの料金を出すこともできます。

この方法はCloudFrontからユーザーへの転送量です。

EC2やS3間の転送量はこちら => Cost Explorerを使ってEC2⇔CloudFrontの転送量をaws cliで出力する

ドキュメント

https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ce/index.html

こちらのget-cost-and-usageを行っていきます

ce実行

下記を実行すると、2020年10月の一ヶ月分のデータ転送量を出力することが出来ます。
今回はAWS Organizationsでリンクされているアカウントで出力します

time-period:期間指定
granularity:毎月(MONTHLY)、毎日(DAILY)、毎時(HOURLY)から選べます
metrics:コストを指定するか、使用量を指定するか
group-by:表示するものを何でグループ分けするか
filter:フィルター(jsonで指定)

メトリクスに何をいれるといいかは、AWS Cost Explorerに渡す、Metricsの値の意味がとてもわかりやすいです。感謝

aws ce  get-cost-and-usage \
      --time-period Start=2020-10-01,End=2020-11-01 \
      --granularity MONTHLY  \
      --metrics UsageQuantity  \
      --group-by Type=DIMENSION,Key=LINKED_ACCOUNT \
      --filter file://filter.json

fileter.jsonは下記を記述します。
CloudFront かつ 全リージョンのデータ転送量という意味です

{
    "And":[
    {
        "Dimensions": {
            "Key": "SERVICE",
            "Values": ["Amazon CloudFront"]
        }
    },
    {
        "Dimensions": {
            "Key":"USAGE_TYPE",
            "Values": [
                    "AP-DataTransfer-Out-Bytes",
                    "AP-DataTransfer-Out-OBytes",
                    "APN1-DataTransfer-In-Bytes",
                    "APN1-DataTransfer-Out-Bytes",
                    "APN1-DataTransfer-Regional-Bytes",
                    "APN2-DataTransfer-In-Bytes",
                    "APN2-DataTransfer-Out-Bytes",
                    "APN2-DataTransfer-Regional-Bytes",
                    "APS1-DataTransfer-In-Bytes",
                    "APS1-DataTransfer-Out-Bytes",
                    "APS2-DataTransfer-In-Bytes",
                    "APS2-DataTransfer-Out-Bytes",
                    "APS3-DataTransfer-In-Bytes",
                    "APS3-DataTransfer-Out-Bytes",
                    "AU-DataTransfer-Out-Bytes",
                    "AU-DataTransfer-Out-OBytes",
                    "CA-DataTransfer-Out-Bytes",
                    "CA-DataTransfer-Out-OBytes",
                    "CAN1-DataTransfer-In-Bytes",
                    "CAN1-DataTransfer-Out-Bytes",
                    "DataTransfer-In-Bytes",
                    "DataTransfer-Out-Bytes",
                    "DataTransfer-Regional-Bytes",
                    "EU-DataTransfer-In-Bytes",
                    "EU-DataTransfer-Out-Bytes",
                    "EU-DataTransfer-Out-OBytes",
                    "EUC1-DataTransfer-In-Bytes",
                    "EUC1-DataTransfer-Out-Bytes",
                    "EUN1-DataTransfer-In-Bytes",
                    "EUN1-DataTransfer-Out-Bytes",
                    "EUW2-DataTransfer-In-Bytes",
                    "EUW2-DataTransfer-Out-Bytes",
                    "EUW3-DataTransfer-In-Bytes",
                    "EUW3-DataTransfer-Out-Bytes",
                    "IN-DataTransfer-Out-Bytes",
                    "IN-DataTransfer-Out-OBytes",
                    "JP-DataTransfer-Out-Bytes",
                    "JP-DataTransfer-Out-OBytes",
                    "ME-DataTransfer-Out-Bytes",
                    "ME-DataTransfer-Out-OBytes",
                    "SA-DataTransfer-Out-Bytes",
                    "SA-DataTransfer-Out-OBytes",
                    "SAE1-DataTransfer-In-Bytes",
                    "SAE1-DataTransfer-Out-Bytes",
                    "US-DataTransfer-Out-Bytes",
                    "US-DataTransfer-Out-OBytes",
                    "USE2-DataTransfer-In-Bytes",
                    "USE2-DataTransfer-Out-Bytes",
                    "USW1-DataTransfer-Out-Bytes",
                    "USW2-DataTransfer-Out-Bytes",
                    "ZA-DataTransfer-Out-Bytes",
                    "ZA-DataTransfer-Out-OBytes"
                ]
        }
    }
    ]
}

実行結果

{
    "GroupDefinitions": [
        {
            "Type": "DIMENSION",
            "Key": "LINKED_ACCOUNT"
        }
    ],
    "ResultsByTime": [
        {
            "TimePeriod": {
                "Start": "2020-10-01",
                "End": "2020-11-01"
            },
            "Total": {},
            "Groups": [
                {
                    "Keys": [
                        "111111111111"
                    ],
                    "Metrics": {
                        "UsageQuantity": {
                            "Amount": "1.111111",
                            "Unit": "GB"
                        }
                    }
                },
                {
                    "Keys": [
                        "222222222222"
                    ],
                    "Metrics": {
                        "UsageQuantity": {
                            "Amount": "2.2222222",
                            "Unit": "GB"
                        }
                    }
                },
                {
                    "Keys": [
                        "333333333333"
                    ],
                    "Metrics": {
                        "UsageQuantity": {
                            "Amount": "3.33333333",
                            "Unit": "GB"
                        }
                    }
                },
                {
                    "Keys": [
                        "444444444444"
                    ],
                    "Metrics": {
                        "UsageQuantity": {
                            "Amount": "4.44444444",
                            "Unit": "GB"
                        }
                    }
                },
                {
                    "Keys": [
                        "555555555555"
                    ],
                    "Metrics": {
                        "UsageQuantity": {
                            "Amount": "5.555555555",
                            "Unit": "GB"
                        }
                    }
                },
                {
                    "Keys": [
                        "666666666666"
                    ],
                    "Metrics": {
                        "UsageQuantity": {
                            "Amount": "66666.666",
                            "Unit": "GB"
                        }
                    }
                },
                {
                    "Keys": [
                        "777777777777"
                    ],
                    "Metrics": {
                        "UsageQuantity": {
                            "Amount": "777.7777777",
                            "Unit": "GB"
                        }
                    }
                },
                {
                    "Keys": [
                        "888888888888"
                    ],
                    "Metrics": {
                        "UsageQuantity": {
                            "Amount": "88.8888888888",
                            "Unit": "GB"
                        }
                    }
                }
            ],
            "Estimated": false
        }
    ]
}