AWS CLI

AWS CLIの操作に関する備忘メモ。

AWS CLIとは

AWSを操作するための、CLIツール。

AWS CLIのインストール

以下のように curl(1) 経由で実施するのが公式手順の模様。

$ curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

docs.aws.amazon.com

認証情報の初期設定

AWS CLI のインストール完了後、 aws configure を実行することで、対話形式でAWS CLIの動作に必要な認証情報を設定することができる。
※以下のCLIの実行結果は公式ドキュメントより引用

$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: json

この際に設定した アクセスキーIDシークレットアクセスキー は、 ~/.aws/credentials に永続化される。

cat ~/.aws/credentials
[default]
aws_access_key_id = xxx
aws_secret_access_key = xxx

またリージョンの情報は ~/.aws/config に永続化される。

cat ~/.aws/config
[default]
region = ap-northeast-1

これらの情報は、 aws configure list で確認することができる。

$ 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

プロファイル

プロファイルとは

プロファイルアクセスキーIDシークレットアクセスキー を持つコレクションのようだ。

プロファイルのイメージ

AWS CLIのユーザーは、複数のプロファイルを使い分けることで認証情報の切り替えを行うことができる。

$ aws s3 ls --profile my-profile

プロファイルの新規作成

プロファイルの新規作成に際しては、 aws configure --profile を使う。

$ aws configure --profile sample-profile
AWS Access Key ID [None]: ****************XXXXX
AWS Secret Access Key [None]: ****************XXXXX
Default region name [None]: ap-northeast-1
Default output format [None]:

作成されたプロファイルの設定値は、 ~/.aws/credentials ~/.aws/config それぞれに [profile プロファイル名] のセクションと共に追記される。

$ cat ~/.aws/credentials
[default]
aws_access_key_id = ****************XXXX
aws_secret_access_key = ****************XXXX

[sample-profile]
aws_access_key_id = ****************XXXX
aws_secret_access_key = ****************XXXX
$ cat config
[default]
region = ap-northeast-1

[profile sample-profile]
region =ap-northeast-1

参考記事

/* https://sunrise033.com/entry/hatena-blog-how-to-hierarchicalize-categories */