AWS CLIの実行ユーザ(プロファイル)を変更

AWSのCLIの実行ユーザ(プロファイル)を変更するサンプルです。

目次

サンプル 概要
  実行ユーザ(プロファイル)を登録する
プロファイル名を指定して変更する

概要

  • EC2の実行ユーザ(プロファイル)をロールからIAMユーザに変更し再度ロールに戻します。
  • 実行ユーザ(プロファイル)に名称をつけて区別できます。

 

実行ユーザ(プロファイル)を登録する

1.現時点の実行ユーザを確認します。EC2にロールをつけているのでロールになっています。

[ec2-user@ip-10-0-10-4 ~]$ aws configure list
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                <not set>             None    None
access_key     ********************         iam-role
secret_key     ********************         iam-role
    region                <not set>             None    None
[ec2-user@ip-10-0-10-4 ~]$

 

2.aws configureで実行ユーザを登録します。

[ec2-user@ip-10-0-10-4 ~]$ aws configure
AWS Access Key ID [None]: アクセスキー
AWS Secret Access Key [None]: シークレットキー
Default region name [None]: ap-northeast-1
Default output format [None]:
[ec2-user@ip-10-0-10-4 ~]$

 

3.実行ユーザ(プロファイル)を確認すると登録したユーザに変わっています。

[ec2-user@ip-10-0-10-4 ~]$ aws configure list
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                  default           manual    --profile
access_key     ******************** shared-credentials-file
secret_key     ******************** shared-credentials-file
    region           ap-northeast-1      config-file    ~/.aws/config
[ec2-user@ip-10-0-10-4 ~]$

4行目のprofileの値(Value)はdefaultになっています。

 

4.aws configureコマンドで設定した値は、以下にファイルで配置されます。

[ec2-user@ip-10-0-10-4 ~]$ ls -l ~/.aws
total 8
-rw------- 1 ec2-user ec2-user  34 Apr  8 21:23 config
-rw------- 1 ec2-user ec2-user 116 Apr  8 21:23 credentials
[ec2-user@ip-10-0-10-4 ~]$
[ec2-user@ip-10-0-10-4 ~]$ cat ~/.aws/config
[default]
region = ap-northeast-1
[ec2-user@ip-10-0-10-4 ~]$ cat ~/.aws/credentials
[default]
aws_access_key_id = アクセスキー
aws_secret_access_key = シークレットキー
[ec2-user@ip-10-0-10-4 ~]$

 

元のロールに戻す

5.設定された値のファイルを削除します。

rm -r ~/.aws

フォルダごと削除され、アクセスの権限がなくなります。

 

6.実行ユーザを確認するとロールになっています。

[ec2-user@ip-10-0-10-4 ~]$ aws configure list
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                <not set>             None    None
access_key     ********************         iam-role
secret_key     ********************         iam-role
    region                <not set>             None    None
[ec2-user@ip-10-0-10-4 ~]$

 

プロファイル名を指定して変更する

1.実行ユーザ(プロファイル)の登録で以下のようにプロファイル名を指定して登録します。

[ec2-user@ip-10-0-10-4 ~]$ aws configure --profile testprofile1
AWS Access Key ID [None]: アクセスキー
AWS Secret Access Key [None]: シークレットキー
Default region name [None]: ap-northeast-1
Default output format [None]:
[ec2-user@ip-10-0-10-4 ~]$

 

2.exportコマンドでプロファイルを変更します。

[ec2-user@ip-10-0-10-4 ~]$ export AWS_DEFAULT_PROFILE=testprofile1

exportコマンドを指定しないと実行ユーザは(プロファイル)は変わりません。

 

3.実行ユーザを確認すると指定したプロファイルに変わっています。

[ec2-user@ip-10-0-10-4 ~]$ aws configure list
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile             testprofile1           manual    --profile
access_key     ****************2ARQ shared-credentials-file
secret_key     ****************vsad shared-credentials-file
    region           ap-northeast-1      config-file    ~/.aws/config
[ec2-user@ip-10-0-10-4 ~]$

4行目のprofileの値(Value)はtestprofile1になっています。

 

環境変数のAWS_DEFAULT_PROFILEの値はechoコマンドで確認できます。

[ec2-user@ip-10-0-10-4 ~]$ echo $AWS_DEFAULT_PROFILE
testprofile1
[ec2-user@ip-10-0-10-4 ~]$

環境変数をechoで確認するときは先頭にドルマーク($)をつけます。

関連の記事

AWS EC2からS3へアクセスする(ユーザーのアクセスキー)

△上に戻る