Amazon Web Services 資料一覧

macOS SierraにAWS CLI環境を構築する手順

2017/7/10更新

対応バージョン: 10.12.5

macOS SierraにAWS CLI環境を構築する手順を示す。

準備

最初にMac App StoreからXcodeをインストールしておく。

AWS CLIインストール

AWS CLIのインストールにはPythonのパッケージマネージャであるpipコマンドを使用するのでpip -> AWS CLIの順でインストールする。

pipインストール
% sudo easy_install pip

% pip --version
pip 9.0.1 from /Library/Python/2.7/site-packages (python 2.7)
awscliインストール
% pip install awscli --upgrade --user

PATH環境変数に以下のパスを追加
$HOME/Library/Python/2.7/bin

% aws --version
aws-cli/1.11.117 Python/2.7.10 Darwin/16.6.0 botocore/1.5.80

JSON Lintインストール

AWS CLIにおけるJSONデータの表示・加工に便利なJSON Lintをインストールする。

JSON LintのインストールにはNode.js付属のnpmコマンドを使用するのでNode.js -> JSON Lintの順でインストールする。

nodebrewインストール
% curl https://raw.githubusercontent.com/hokaccha/nodebrew/master/nodebrew | perl - setup

PATH環境変数に以下のパスを追加
$HOME/.nodebrew/current/bin

% nodebrew selfupdate
Node.jsインストール
% nodebrew install latest

PATH環境変数に以下のパスを追加
$HOME/.nodebrew/node/v8.1.3/bin

% node -v
v8.1.3

% npm -v
5.0.3
JSON Lintインストール
% npm install jsonlint

PATH環境変数に以下のパスを追加
$HOME/node_modules/.bin

% jsonlint -v
1.6.2

% echo '{"key":"value"}' | jsonlint
{
  "key": "value"
}

初期設定

Access KeyとSecret Access Keyを設定してAWS CLIが使用できるようにする。

% aws configure list
      Name                 Value          Type  Location
      ----                 -----          ----  --------
   profile             <not set>          None  None
access_key             <not set>          None  None
secret_key             <not set>          None  None
    region             <not set>          None  None

% aws configure
AWS Access Key ID [None]: xxxxxxxxxxxxxxxxxx
AWS Secret Access Key [None]: xxxxxxxxxxxxxxxxxx
Default region name [None]: ap-northeast-1 <- 東京リージョンの場合
Default output format [None]: json <- 他にtable、textが指定可能。好みに応じて指定

% aws configure list
      Name                 Value          Type  Location
      ----                 -----          ----  --------
   profile             <not set>          None  None
access_key  ******************** shared-credentials-file
secret_key  ******************** shared-credentials-file
    region        ap-northeast-1   config-file  ~/.aws/config

設定は以下の2ファイルに格納されるが、それぞれのファイルの[default]の部分に一意の名前を付ければ複数の設定(プロファイル)を共存させられる。その場合、awsコマンドを実行する時に--profileに続けてプロファイル名を指定する。

$HOME/.aws/config

コマンド出力結果フォーマットとリージョン

[default]
output = json
region = ap-northeast-1
$HOME/.aws/credentials

認証情報

[default]
aws_access_key_id = xxxxxxxxxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxxxxxxxxx

動作確認

インスタンス一覧取得
% aws ec2 describe-instances | jq '.Reservations [] .Instances [] .InstanceId'
インスタンス起動
% aws ec2 start-instances --instance-ids i-xxxxxxxxxxxxxxxxx
インスタンス停止
% aws ec2 stop-instances --instance-ids i-xxxxxxxxxxxxxxxxx
インスタンス削除
% aws ec2 terminate-instances --instance-ids i-xxxxxxxxxxxxxxxxx
IAMユーザ取得
% aws iam list-users
{
    "Users": [
        {
            "UserName": "dev", 
            "Path": "/", 
            "CreateDate": "2017-07/10T04:34:38Z", 
            "UserId": "xxxxxxxxxxxxxxxxxxxxx", 
            "Arn": "arn:aws:iam::xxxxxxxxxxxx:user/dev"
        }
    ]
}

% aws iam get-user --query 'User.Arn'
"arn:aws:iam::xxxxxxxxxxxx:user/dev"

参考サイト