toto_1212

技術のログをツラツラ書きます。自分用ですが参考にしていただけたら嬉しいです。間違ってたらドンドン突っ込んでください。

新しくなったawscliを使う

もう1ヶ月経ちますがAWSの新CLIがリリースされました。
f:id:toatoshi:20131007134117j:plain

今までのapi-toolsで問題なかったのですが、アカウントが跨げること対応サービスが増えたこと
に魅力を感じ、遅ればせながらインストールをしたので導入備忘です。

手持ちのubuntu12.4とWindows7両方に入れましたので両方書いときます。

【Windows】

AWS Command Line Interfaceにアクセスし、手持ちの環境(64-bit or 32-bit)のものをダウンロードする。
f:id:toatoshi:20131007134209j:plain

②ダウンロードしたmsiファイルを実行し、ウィザードで「Next」をクリックする。
f:id:toatoshi:20131007134220j:plain

③内容をみてAgreementにチェックを入れて「Next」をクリックする。
f:id:toatoshi:20131007134229j:plain

④カスタマイズ(インストール先等)の変更の必要事項があれば実施し、「Next」をクリックする。
f:id:toatoshi:20131007134237j:plain

⑤「Install」をクリックする。
f:id:toatoshi:20131007134250j:plain

⑥「finish」をクリックしてインストール完了。
f:id:toatoshi:20131007134303j:plain

⑦任意のフォルダへ設定ファイルを配置する。

C:\Users\username\aws\awscli.conf

[default]
aws_access_key_id=xxxxxxxxxxxxxxxxxxxxx
aws_secret_access_key=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
region=ap-northeast-1

⑧設定ファイルのパスを環境変数にセットする。

cmd.exeにて以下を実行

set AWS_CONFIG_FILE=C:\Users\username\aws\awscli.conf

※システム環境変数に登録すればsetコマンドでの登録は不要です。


これで導入は終わり。

試しに以下コマンドを実行してみる。

aws ec2 describe-instances

{
    "Reservations": [
        {
            "OwnerId": "25xxxx36xxxx", 
            "ReservationId": "x-xxxxxx3", 
            "Groups": [], 
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
 ・・・・
aws ec2 describe-addresses
{
    "Addresses": [
        {
            "Domain": "vpc",
            "InstanceId": "i-xxxxxxxx",
            "NetworkInterfaceId": "eni-xxxxxxxx",
            "AssociationId": "eipassoc-xxxxxxxx",
            "NetworkInterfaceOwnerId": "xxxx2836xxxx",
            "PublicIp": "xxx.xxx.xxx.xxx",
            "AllocationId": "eipalloc-xxxxxx",
            "PrivateIpAddress": "172.20.1.195"
        }
    ]
}

デフォルトはjson形式で受け取るデータとしてはいいのですが見にくいので、
outputオプションを付けてみると、、、

aws ec2 describe-addresses --output table
 ----------------------------------------------------
 |                 DescribeAddresses                |
 +--------------------------------------------------+
 ||                    Addresses                   ||
 |+--------------------------+---------------------+|
 ||  AllocationId            |  eipalloc-xxxxxxxx  ||
 ||  AssociationId           |  eipassoc-xxxxxxxx  ||
 ||  Domain                  |  vpc                ||
 ||  InstanceId              |  i-xxxxxxxx         ||
 ||  NetworkInterfaceId      |  eni-xxxxxxxx       ||
 ||  NetworkInterfaceOwnerId |  xxxx2836xxxx       ||
 ||  PrivateIpAddress        |  172.20.1.195       ||
 ||  PublicIp                |  xxx.xxx.xxx.xxx    ||
 |+--------------------------+---------------------+|


aws ec2 describe-addresses --output text
vpc     i-xxxxxxxx      eni-xxxxxxxx    eipassoc-xxxxxxxx       xxxx2836xxxx
xxx.xxx.xxx.xxx    eipalloc-xxxxxxxx       172.20.1.195

オプションは「json」、「text」、「table」の3種類。

ubuntu

①pipコマンドが使えるようpipをインストールする。

$sudo apt-get install python-pip

②awscliをインストールする。

$sudo pip install awscli

③任意のフォルダへ設定ファイルを配置する。

$vi /home/username/aws/awscli.conf

[default]
aws_access_key_id=xxxxxxxxxxxxxxxxxxxx
aws_secret_access_key=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
region=ap-northeast-1

④設定ファイルのパスを環境変数にセットする

ターミナルを開き

$export AWS_CONFIG_FILE=/home/username/aws/awscli.conf

※.profileに追記し、source .profileか再ログインすれば次回から実行不要です

これで導入は終わり。

Windowsには無いと思われるが、Unix/Linux系OSには以下をターミナル上で実行することでコマンド補完機能が使える

$complete -C aws_completer aws

コマンドの実行結果はWindowsと同じなので割愛。


「アカウントが跨げる~」って冒頭に書きましたが、やり方はこんな感じ。
Windows、ubuntuともやり方は同じで、awscli.confを開き、以下のように記載する。

[default]
aws_access_key_id=xxxxxxxxxxxxxxxxxxxx
aws_secret_access_key=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
region=ap-northeast-1

[profile toto_1212_1]
aws_access_key_id=xxxxxxxxxxxxxxxxxxxx
aws_secret_access_key=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
region=ap-northeast-1

[profile toto_1212_2]
aws_access_key_id=xxxxxxxxxxxxxxxxxxxx
aws_secret_access_key=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
region=ap-northeast-1

cmd.exeかターミナルの自分の環境で以下を実行する。

$aws ec2 describe-tags --output table
 ---------------------------------------------------------------
 |                        DescribeTags                         |
 +-------------------------------------------------------------+
 ||                           Tags                            ||
 |+------+-------------+----------------+---------------------+|
 ||  Key | ResourceId  | ResourceType   |        Value        ||
 |+------+-------------+----------------+---------------------+|
 ||  Name|  i-xxxxxxxx |  instance      |  test_site      ||
 ||  Name|  i-xxxxxxxx |  instance      |  test_site1         ||
 ||  Name|  i-xxxxxxxx |  instance      |  test_site2         ||
 |+------+-------------+----------------+---------------------+|

$aws ec2 describe-tags --profile toto_1212_1 --output table
 --------------
 |DescribeTags|
 +------------+

$aws ec2 describe-tags --profile toto_1212_2 --output table
 ----------------------------------------------------------------
 |                         DescribeTags                         |
 +--------------------------------------------------------------+
 ||                            Tags                            ||
 |+-----+-----------------+----------------+-------------------+|
 || Key |   ResourceId    | ResourceType   |       Value       ||
 |+-----+-----------------+----------------+-------------------+|
 ||Name |  snap-xxxxxxxx  |  snapshot      |  test_drive     ||
 ||Name |  i-xxxxxxxx     |  instance      |  test_site_pic1   ||
 |+-----+-----------------+----------------+-------------------+|

何も指定しないとdefaultのアカウントでオプションでprofile名を指定すると、指定したアカウントの情報が表示されます。こんな感じで簡単なprofile設定とコマンド時の指定でわずらわしいアカウント跨ぎを簡単に実現できます。

WindowsOS時でコマンド補完できるやり方があったら教えてください。