LoginSignup
2
1

More than 5 years have passed since last update.

boto3 で CostandUsageReportService を使うときはリージョンをハードコードすること

Posted at

AWS の Python 用ライブラリである boto3 を使って物作りをしていた際にハマったのでメモ.

CostandUsageReportService とは何者

和名「AWS のコストと使用状況レポート」.

CostandUsageReportService は,ここで作られるコストレポートのエントリをいじくるためのもの

ハマったこと

デフォルトリージョンを ap-northeast-1 などとしている環境でリージョンを指定せずに boto3.client('cur') とすると describe_report_definitions() などの呼び出し時にエンドポイントに繋がらないと怒られる.(エンドポイントが存在しないため)

~/.aws/config
[default]
region = ap-northeast-1
sample.py
bc = boto3.client('cur')
report = bc.describe_report_definitions()

print(report)
実行結果
Traceback (most recent call last):
  (... snip ...)
botocore.exceptions.EndpointConnectionError: Could not connect to the endpoint URL: "https://cur.ap-northeast-1.amazonaws.com/"

直し方

クライアント作成時に us-east-1 リージョンを指定する.

sample.py
bc = boto3.client('cur', 'us-east-1')
report = bc.describe_report_definitions()

print(report)
実行結果

{'ReportDefinitions': [{'ReportName': 'hogehoge', 'TimeUnit': 'DAILY', 'Format': 'textORcsv', 'Compression': 'GZIP', 'AdditionalSchemaElements': [], 'S3Bucket': 'chi9rin-cur', 'S3Prefix': '', 'S3Region': 'us-east-1', 'AdditionalArtifacts': []}], 'ResponseMetadata': {'RequestId': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'content-type': 'application/x-amz-json-1.1', 'content-length': '227', 'date': 'Sat, 04 Nov 2017 10:47:20 GMT'}, 'RetryAttempts': 0}}

原因

Traceback にもあるように,ap-northeast-1 では cur が提供されていないから.

cur については特定のリージョンに基づくものではないので,cur のクライアントを作る部分については us-east-1 でハードコードしておいても問題はなさそう.

あと今回見つかったのは cur だけだったが,他のサービスについても同じことが言えるかもしれない.(他にリージョン無指定の IAM はクライアント作成時にリージョンを指定せずとも問題なかった)

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1