LoginSignup
0
0

More than 1 year has passed since last update.

AWS-CLIの設定(マルチアカウント)

Last updated at Posted at 2021-08-11

AWS CLIのインストール

AWS CLIのインストール(Mac)

$ sudo pip install awscli

or

$ brew install awscli

インストールの確認

$ which aws
/usr/local/bin/aws
$ aws --version
aws-cli/2.1.21 Python/3.7.4 Darwin/20.5.0 exe/x86_64 prompt/off 

AWS CLIの設定

事前準備

使用するAWSアカウント毎にアクセスキーとシークレットを取得しておく
ここでは3つのAWSアカウントが存在するとする

  1. サンドボックス環境用アカウント
  2. ベータ環境用アカウント
  3. 本番環境用アカウント

プロファイルの登録

サンドボックスアカウント用プロファイル

$ aws configure --profile sandbox
AWS Access Key ID [None]: xxxxxxxxxx
AWS Secret Access Key [None]:xxxxxxxxxx
Default region name [None]: ap-northeast-1
Default output format [None]: json

ベータ環境アカウント用プロファイル

$ aws configure --profile beta
AWS Access Key ID [None]: yyyyyyyyyy
AWS Secret Access Key [None]: yyyyyyyyyy
Default region name [None]: ap-northeast-1
Default output format [None]: json

本番環境アカウント用プロファイル

$ aws configure --profile production
AWS Access Key ID [None]: zzzzzzzzzz
AWS Secret Access Key [None]: zzzzzzzzzz
Default region name [None]:  ap-northeast-1
Default output format [None]: json

登録済設定の確認

クレデンシャル設定の確認

$ more ~/.aws/credentials
[default]
aws_access_key_id = dddddddddd
aws_secret_access_key = dddddddddd
[sandbox]
aws_access_key_id = xxxxxxxxxx
aws_secret_access_key = xxxxxxxxxx
[beta]
aws_access_key_id = yyyyyyyyyy
aws_secret_access_key = yyyyyyyyyy
[production]
aws_access_key_id = zzzzzzzzzz
aws_secret_access_key = zzzzzzzzzz

コンフィグの確認

$ more ~/.aws/config
[default]
output=json
region = ap-northeast-1
[profile sandbox]
region = ap-northeast-1
output = json
[profile beta]
region = ap-northeast-1
output = json
[profile production]
region =  ap-northeast-1
output = json

現行設定の確認

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

プロファイルを指定して実行

--profileオプションでプロファイルを指定して実行

$ aws s3 ls --profile sandbox

プロファイルの切り替え

$ export AWS_DEFAULT_PROFILE=sandbox

確認

$ aws configure list
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                  sandbox           manual    --profile
access_key     ****************xxxx shared-credentials-file    
secret_key     ****************xxxx shared-credentials-file    
    region           ap-northeast-1      config-file    ~/.aws/config

プロファイルの切り替え(AWSPを使用)

AWSP - AWS Profile Switcherをインストールしてプロファイルを切り替えるようにする

AWSPのインストール

$ npm install -g awsp

.bashrc (or .bash_profile)に以下を追記

alias awsp="source _awsp"

設定を反映

$ source ~/.bashrc

AWSPでプロファイルの切り替え

betaに変更してみる

$ awsp
AWS Profile Switcher
? Choose a profile (Use arrow keys)
  default 
  sandbox 
❯ beta 
  production 

確認

betaになっている

$ aws configure list
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                     beta           manual    --profile
access_key     ****************yyyy shared-credentials-file    
secret_key     ****************yyyy shared-credentials-file    
    region           ap-northeast-1      config-file    ~/.aws/config

サンドボックスに戻しておく

$ awsp
AWS Profile Switcher
? Choose a profile sandbox
Hello Prompt! 38 11:20:39 ~ $ aws configure list
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                  sandbox           manual    --profile
access_key     ****************xxxx shared-credentials-file    
secret_key     ****************xxxx shared-credentials-file    
    region           ap-northeast-1      config-file    ~/.aws/config

設定の削除

~/.aws/credentialsと、~/.aws/configを直接編集。当該箇所を削除して保存

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0