サイトアイコン 知的好奇心

PythonでAWSの料金情報を取得する方法

PythonでAWSの料金情報を取得する方法をご紹介します。

条件

事前準備

Cost Explorer の有効化

AWS マネジメントコンソール にサインインします。

Billing and Cost Management コンソールを開きます。(以下のリンク)
https://console.aws.amazon.com/billing/home#/

左のナビゲーションで [Cost Explorer] を選択します。

[Enable Cost Explorer] を選択します。

認証資格情報

awsのセキュリティ資格情報を取得する必要があります。
取得手順は、以下の記事の「セキュリティ資格情報」をご参照ください。

CyberduckでAmazon S3の操作を行う方法

認証/構成ファイルに記述

取得したセキュリティ資格情報を、認証/構成ファイルに記述します。

以下の2つのファイルを指定のディレクトリに配置します。

手順は、以下の記事の「認証/構成ファイルに記述する方法」をご参照ください。

PythonでAmazon S3の操作を行う方法(接続編)

パッケージのインストール

インストールされているパッケージを確認します。

pip list

「boto3」が存在しない場合、「boto3」をインストールします。

pip install boto3

サンプルプログラム

使用するAPI

以下のAPIを用いて、アカウントのコストと使用状況の指標を取得します。

サンプルソース

from logging import getLogger, StreamHandler, Formatter, DEBUG, FileHandler
from botocore.exceptions import ClientError
import boto3
import pprint


## ログ出力設定
logger = getLogger("CostExplore")
logger.setLevel(DEBUG)

# コンソール出力設定
stream_handler = StreamHandler()
formatter = Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)

# レポートパラメータ
START_DAY = "2020-04-01"
END_DAY = "2020-06-01"


def get_client():
    """
    CostExploreのclient取得
    """
    costExplore_client = boto3.client(
        'ce'
    )
    return costExplore_client


def get_report(client):
    """
    レポートの取得
    """

    #logger.info('レポート取得結果:')
    response_service = client.get_cost_and_usage(
        TimePeriod = {"Start": START_DAY, "End": END_DAY}, 
        Granularity='DAILY',
        Metrics = ["BlendedCost"],
    )
    pprint.pprint(response_service)


def main():
    """
    実処理(main)
    """
    client = get_client()  # client取得
    
    get_report(client)  # レポートの取得


if __name__ == '__main__':
    """
    main関数を実行
    """
    
    main()

実行結果

以下のように情報が取得されます。

以下のパラメータを変更することで、様々な形式の情報を取得することが出来ます。

また、以下のパラメータを指定することでフィルタやグループ化を行うことが出来ます。

{'ResponseMetadata': {'HTTPHeaders': {'cache-control': 'no-cache',
                                      'connection': 'keep-alive',
                                      'content-length': '8437',
                                      'content-type': 'application/x-amz-json-1.1',
                                      'date': 'Sun, 28 Jun 2020 07:28:27 GMT',
                                      'x-amzn-requestid': '24b48d0d-15b1-4a2c-8249-aae72a7a8c20'},
                      'HTTPStatusCode': 200,
                      'RequestId': '24b48d0d-15b1-4a2c-8249-aae72a7a8c20',
                      'RetryAttempts': 0},
 'ResultsByTime': [{'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-02', 'Start': '2020-04-01'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-03', 'Start': '2020-04-02'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-04', 'Start': '2020-04-03'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-05', 'Start': '2020-04-04'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-06', 'Start': '2020-04-05'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-07', 'Start': '2020-04-06'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-08', 'Start': '2020-04-07'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-09', 'Start': '2020-04-08'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-10', 'Start': '2020-04-09'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-11', 'Start': '2020-04-10'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-12', 'Start': '2020-04-11'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-13', 'Start': '2020-04-12'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-14', 'Start': '2020-04-13'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-15', 'Start': '2020-04-14'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-16', 'Start': '2020-04-15'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-17', 'Start': '2020-04-16'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-18', 'Start': '2020-04-17'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-19', 'Start': '2020-04-18'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-20', 'Start': '2020-04-19'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-21', 'Start': '2020-04-20'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-22', 'Start': '2020-04-21'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-23', 'Start': '2020-04-22'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-24', 'Start': '2020-04-23'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-25', 'Start': '2020-04-24'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-26', 'Start': '2020-04-25'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-27', 'Start': '2020-04-26'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-28', 'Start': '2020-04-27'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-29', 'Start': '2020-04-28'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-04-30', 'Start': '2020-04-29'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-01', 'Start': '2020-04-30'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-02', 'Start': '2020-05-01'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-03', 'Start': '2020-05-02'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-04', 'Start': '2020-05-03'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-05', 'Start': '2020-05-04'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-06', 'Start': '2020-05-05'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-07', 'Start': '2020-05-06'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-08', 'Start': '2020-05-07'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-09', 'Start': '2020-05-08'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-10', 'Start': '2020-05-09'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-11', 'Start': '2020-05-10'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-12', 'Start': '2020-05-11'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-13', 'Start': '2020-05-12'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-14', 'Start': '2020-05-13'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-15', 'Start': '2020-05-14'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-16', 'Start': '2020-05-15'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-17', 'Start': '2020-05-16'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-18', 'Start': '2020-05-17'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-19', 'Start': '2020-05-18'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-20', 'Start': '2020-05-19'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-21', 'Start': '2020-05-20'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-22', 'Start': '2020-05-21'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-23', 'Start': '2020-05-22'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-24', 'Start': '2020-05-23'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-25', 'Start': '2020-05-24'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-26', 'Start': '2020-05-25'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-27', 'Start': '2020-05-26'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-28', 'Start': '2020-05-27'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-29', 'Start': '2020-05-28'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-30', 'Start': '2020-05-29'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-05-31', 'Start': '2020-05-30'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2020-06-01', 'Start': '2020-05-31'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'}}}]}

参考

AWS:Cost Explorer の有効化

https://docs.aws.amazon.com/ja_jp/awsaccountbilling/latest/aboutv2/ce-enable.html

Boto3 Docs:CostExplore

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ce.html

Qiita:AWS の請求金額をモニタリングする

https://qiita.com/aokad/items/16e45a8201022dd3f335

モバイルバージョンを終了