macOSにAWS CLIをインストール

macOSから、コマンドラインでS3やEC2を操作する為にインストールしました。

環境

OS: macOS Sierra

AWS CLIをインストール

  1. Pythonのパッケージ管理システムpip経由でインストールします。よって、まずは下記のコマンドでpipをインストールします。
    $ curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
    $ sudo python get-pip.py
  2. pipでAWS CLIをインストールします。
    $ sudo pip install awscli
    

    通常、上記のコマンドでインストール出来るようなのですが、私は以下のエラーが表示されインストール出来ませんでした。

    DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
        Uninstalling six-1.4.1:
    Exception:
    〜スタックトレースがずらずら〜
    

    sixが既にインストールされている事が原因なので、以下のコマンドで既存のsixを無視し、aws cliをインストールしました。

    $ sudo pip install awscli --upgrade --ignore-installed six

AWS CLIの設定

aws configureコマンドを実行し、設定します。

$ aws configure
AWS Access Key ID [None]: <あなたのAWS Access Key IDを入力>
AWS Secret Access Key [None]: <あなたのAWS Secret Access Keyを入力>
Default region name [None]: ap-northeast-1                            
Default output format [None]: json

AWS Access Key ID、AWS Secret Access Keyは、AWS IAMでユーザーを作成し取得して下さい。ユーザーにはPowerUserAccessポリシーをアタッチして下さい。ほとんどの処理が行えるはずです。

Default region nameは、普段使用するリージョンを設定して下さい。リージョンの一覧はこちら

Default output formatは、JSON, text, tableを選択出来ます。何も指定しない場合、JSONとなります。

動作確認

AWS CLIが利用出来るようになったか簡単な処理を実行して確認してみましょう。

下記は、AWS CLI経由でS3からファイルをダウンロードする手順と結果です。試してみて下さい。

下記のファイルfoo.txtがS3バケットに保存されているとします。

bar
$ aws s3 cp s3://<あなたのS3バケット>/foo.txt ./
download: s3://<あなたのS3バケット>/foo.txt to ./foo.txt 
$ ls
foo.txt
$ cat foo.txt
bar
$

以上です。