AWS CLI(コマンドラインツール)の、プロファイル(認証・設定情報)はdefault以外に名前を指定して複数設定することができます。
この名前付きプロファイルは、開発環境、ステージング環境、本番環境とそれぞれの環境をマルチアカウントで運用していく際に非常に便利です。(というかほぼ必須で使うことになると思います)
またAWS をコード化するためのツール Terraform でもプロファイルを指定して環境を使い分けたりすることもできるため、ぜひ設定しておきましょう。
手順
プロファイル作成
作成
プロファイルを作成します。–profile オプションで複数作成できます。
% aws configure
AWS Access Key ID [None]: 11111
AWS Secret Access Key [None]: 22222
Default region name [None]: ap-northeast-1
Default output format [None]:
%
%
% aws configure --profile user-2
AWS Access Key ID [None]: 33333
AWS Secret Access Key [None]: 44444
Default region name [None]:
Default output format [None]:
%
%
% aws configure --profile user-3
AWS Access Key ID [None]: 55555
AWS Secret Access Key [None]: 66666
Default region name [None]: us-east-1
Default output format [None]: json
%
AWS CLI V2であれば認証情報をWebコンソールからダウンロードしたCSVよりインポートできます
% aws --version
aws-cli/2.0.31 Python/3.7.4 Darwin/19.5.0 botocore/2.0.0dev35
%
% aws configure import --csv file://new_user_credentials.csv
Successfully imported 1 profile(s)
%
設定ファイルはホームディレクトリ配下に作成されます。
機密性の高い認証情報が credentials、設定オプションが config に出力されます。
configは、環境変数 AWS_CONFIG_FILE を使用して保存場所の変更が可能です。
% cat ~/.aws/credentials
[default]
aws_access_key_id = 11111
aws_secret_access_key = 22222
[user-2]
aws_access_key_id = 33333
aws_secret_access_key = 44444
[user-3]
aws_access_key_id = 55555
aws_secret_access_key = 66666
%
%
% cat ~/.aws/config
[default]
region = ap-northeast-1
[profile user-2]
[profile user-3]
region = us-east-1
output = json
%
プロファイル確認
プロファイル一覧
AWS CLI V2 で可能です
% aws configure list-profiles
default
user-2
user-3
%
適用中のプロファイル
not setの場合はdefaultが適用されているようです
% aws configure list
Name Value Type Location
---- ----- ---- --------
profile <not set> None None
access_key ****************1111 shared-credentials-file
secret_key ****************2222 shared-credentials-file
region ap-northeast-1 config-file ~/.aws/config
%
プロファイル切替
環境変数での切替
環境変数AWS_PROFILEを設定することで切り替えます。
% export AWS_PROFILE=user-2
% aws configure list
Name Value Type Location
---- ----- ---- --------
profile user-2 manual --profile
access_key ****************3333 shared-credentials-file
secret_key ****************4444 shared-credentials-file
region <not set> None None
%
%
% export AWS_PROFILE=default
% aws configure list
Name Value Type Location
---- ----- ---- --------
profile default manual --profile
access_key ****************1111 shared-credentials-file
secret_key ****************2222 shared-credentials-file
region ap-northeast-1 config-file ~/.aws/config
%
削除(unset)した場合は、defaultへ戻るようです
% unset AWS_PROFILE
% aws configure list
Name Value Type Location
---- ----- ---- --------
profile <not set> None None
access_key ****************1111 shared-credentials-file
secret_key ****************2222 shared-credentials-file
region ap-northeast-1 config-file ~/.aws/config
%
コマンドラインオプションでの切替
% aws s3 ls
2020-xx-xx 19:15:12 default-user-bucket
%
%
% aws s3 ls --profile user-2
2020-xx-xx 19:20:33 user-2-bucket
%
参考
AWS CLI の設定 - AWS Command Line Interface
AWS Command Line Interface (AWS CLI) を設定し、AWS とやり取りするための設定を指定します。