[小ネタ]EC2のRIの値段がパッと知りたい時に役に立つかもしれないワンライナー

EC2のRIの値段をパッと調べる方法が無いかなと思って作ったワンライナーを備忘も兼ねて公開してみます。
2019.06.30

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

西澤です。EC2のRIの値段を調べるのが手間なことがあると思います。これをパッと取る方法が無いかなと思って作ったメモを備忘も兼ねて公開してみようと思います。

ちなみに、私も下記記事で紹介されている Amazon EC2 Instance Comparison が大のお気に入りなので、EC2の料金やスペックを確認するほとんどのケースはこちらで済ませています。

EC2インスタンスの比較検討が超効率的にできるサイト[EC2Instances.info]の紹介

環境

環境は、通常利用しているMBPのbash環境です。が、先にお詫びと言い訳させていただきますと、始めは取得できるjsonから良い感じに情報を抽出できるような仕組みを検討したのですが、構造が複雑過ぎて断念。なるべく環境に依存しないようにしたいと当初は考えたのですが、いろいろ試した結果、ダブルクォーテーション内のコンマを含むCSVを取り扱う為に、gawkのFPATに頼ってしまいました。申し訳ありませんが、今回ご紹介のワンライナーは、FPATに対応しているv4.0以降のgawkをご利用いただける前提となります。

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.14.5
BuildVersion:   18F132

$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.

$ gawk --version
GNU Awk 5.0.0, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2)
Copyright (C) 1989, 1991-2019 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.

EC2のRI料金を取得するワンライナー

取得したい情報については、様々な需要があるかと思いますので、カラムは多少多めに残したつもりです。フィルター条件については、必要に応じて編集いただければと思いますが、結局元となるCSVのデータとにらめっこする必要はあるので、詳細な説明は割愛させていただきます。

curl -s https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/index.csv | \
    gawk -v FPAT='([^,]*)|("[^"]+")' \
        '$1 == "\"SKU\"" || \
        ( \
            $4 == "\"Reserved\"" && \
            $9 == "\"Quantity\"" && \
            $12 == "\"1yr\"" && \
            $13 == "\"All Upfront\"" && \
            $14 == "\"standard\"" && \
            $17 == "\"Asia Pacific (Tokyo)\"" && \
            $19 ~ "\"m5\\." && \
            $36 == "\"Shared\"" && \
            $76 == "\"NA\"" \
        ) \
        { OFS=","; print $4,$5,$9,$10,$12,$13,$14,$17,$19,$36,$38,$39,$76 }' | \
            tee ec2_ri_apn1_$(date +%Y%m%d).csv

念の為、teeコマンドでファイルにも出力して、まとめて参照できるようにしていますが、執筆時点での結果は以下のようになりました。

"TermType","PriceDescription","Unit","PricePerUnit","LeaseContractLength","PurchaseOption","OfferingClass","Location","Instance Type","Tenancy","Operating System","License Model","Pre Installed S/W"
"Reserved","Upfront Fee","Quantity","1232","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.large","Shared","RHEL","No License required","NA"
"Reserved","Upfront Fee","Quantity","706","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.large","Shared","Linux","No License required","NA"
"Reserved","Upfront Fee","Quantity","3115","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.2xlarge","Shared","SUSE","No License required","NA"
"Reserved","Upfront Fee","Quantity","996","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.large","Shared","SUSE","No License required","NA"
"Reserved","Upfront Fee","Quantity","5939","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.4xlarge","Shared","SUSE","No License required","NA"
"Reserved","Upfront Fee","Quantity","1938","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.xlarge","Shared","RHEL","No License required","NA"
"Reserved","Upfront Fee","Quantity","72581","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.24xlarge","Shared","Windows","No License required","NA"
"Reserved","Upfront Fee","Quantity","35035","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.24xlarge","Shared","RHEL","No License required","NA"
"Reserved","Upfront Fee","Quantity","11589","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.8xlarge","Shared","SUSE","No License required","NA"
"Reserved","Upfront Fee","Quantity","11299","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.8xlarge","Shared","Linux","No License required","NA"
"Reserved","Upfront Fee","Quantity","1512","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.large","Shared","Windows","No License required","NA"
"Reserved","Upfront Fee","Quantity","6788","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.4xlarge","Shared","RHEL","No License required","NA"
"Reserved","Upfront Fee","Quantity","12097","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.4xlarge","Shared","Windows","No License required","NA"
"Reserved","Upfront Fee","Quantity","6048","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.2xlarge","Shared","Windows","No License required","NA"
"Reserved","Upfront Fee","Quantity","34186","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.metal","Shared","SUSE","No License required","NA"
"Reserved","Upfront Fee","Quantity","35035","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.metal","Shared","RHEL","No License required","NA"
"Reserved","Upfront Fee","Quantity","23736","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.16xlarge","Shared","RHEL","No License required","NA"
"Reserved","Upfront Fee","Quantity","2825","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.2xlarge","Shared","Linux","No License required","NA"
"Reserved","Upfront Fee","Quantity","5649","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.4xlarge","Shared","Linux","No License required","NA"
"Reserved","Upfront Fee","Quantity","3964","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.2xlarge","Shared","RHEL","No License required","NA"
"Reserved","Upfront Fee","Quantity","1702","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.xlarge","Shared","SUSE","No License required","NA"
"Reserved","Upfront Fee","Quantity","16948","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.12xlarge","Shared","Linux","No License required","NA"
"Reserved","Upfront Fee","Quantity","36290","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.12xlarge","Shared","Windows","No License required","NA"
"Reserved","Upfront Fee","Quantity","1412","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.xlarge","Shared","Linux","No License required","NA"
"Reserved","Upfront Fee","Quantity","33896","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.metal","Shared","Linux","No License required","NA"
"Reserved","Upfront Fee","Quantity","12438","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.8xlarge","Shared","RHEL","No License required","NA"
"Reserved","Upfront Fee","Quantity","17238","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.12xlarge","Shared","SUSE","No License required","NA"
"Reserved","Upfront Fee","Quantity","18087","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.12xlarge","Shared","RHEL","No License required","NA"
"Reserved","Upfront Fee","Quantity","72581","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.metal","Shared","Windows","No License required","NA"
"Reserved","Upfront Fee","Quantity","3024","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.xlarge","Shared","Windows","No License required","NA"
"Reserved","Upfront Fee","Quantity","22598","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.16xlarge","Shared","Linux","No License required","NA"
"Reserved","Upfront Fee","Quantity","48387","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.16xlarge","Shared","Windows","No License required","NA"
"Reserved","Upfront Fee","Quantity","34186","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.24xlarge","Shared","SUSE","No License required","NA"
"Reserved","Upfront Fee","Quantity","24194","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.8xlarge","Shared","Windows","No License required","NA"
"Reserved","Upfront Fee","Quantity","33896","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.24xlarge","Shared","Linux","No License required","NA"
"Reserved","Upfront Fee","Quantity","22888","1yr","All Upfront","standard","Asia Pacific (Tokyo)","m5.16xlarge","Shared","SUSE","No License required","NA"

まとめ

AWSの料金を調べるのってけっこう難しいですよね。これだけの仕組みを管理しているAWSもすごいなとは思いつつ、もう少しだけでもわかりやすくなると嬉しいなと言う気持ちになるときがしばしばあります。あまり需要もないだろうなとは思いつつも、自分自身が利用したいケースが何度かあったので、awkの文法ですぐにエラーを出してしまう未来の自分の為にメモを残しておこうと思います。

誰かのお役に立てば嬉しいです。