LoginSignup
0
1

More than 1 year has passed since last update.

aws-vault で AWS Profile に対応していない AWS ツールを使おう!

Last updated at Posted at 2022-06-03

困りごと

最近では AWS アカウントは MFA が有効化されてたり、Assume role でクロスアカウントで認証をしてたりします。
その為、AWS CLI では以下の様に --profile {profile-name} を用いて認証をしています。

aws s3 ls --profile your-aws-profile

しかし、世の中の AWS にアクセスするツールの全てが「AWS Profile を指定する機能」に対応している訳ではありません。
Terraform CLI 等、多くのツールは実行シェル上に、ただ単に以下の環境変数がセットされている事を期待しています。

AWS_REGION=ap-northeast-1
AWS_ACCESS_KEY_ID=xxxxxxxxxxxxxxxx
AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxx

# Switch role (Assume role) の場合は、以下も必要.
AWS_SESSION_TOKEN=xxxxxxxxxxxxxxxx

aws-vault を使おう!

その問題、 aws-vault を使うと解決できます。

aws-vault とは、~/.aws/config~/.aws/credentials に書かれたプロファイル情報を AWS 仕様の通りに解釈・処理し、前述の環境変数をセットした状態で任意のコマンド実行を手助けしてくれます。

sample-usages
$ aws-vault exec your-aws-profile
Enter MFA code for arn:aws:iam::000000000000:mfa/your.name: 999999

bash-3.2$ aws s3 ls
2022-04-06 17:22:40 your-s3-bucket-999999999999
...

環境

macOS Big Sur 11.6

前提

次の様な ~/.aws/config , ~/.aws/credentials を持っていると仮定します。

  • (1) MFA が有効化されている.
  • (2) アカウント 000000000000 のユーザー your.name が Switch role して、アカウント 999999999999 (role=your.role) に入る.
~/.aws/credentials
[default]
aws_access_key_id = xxxxxxxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxxxxxxx
~/.aws/config
[default]
region=ap-northeast-1

[profile your-aws-profile]
region=ap-northeast-1
mfa_serial = arn:aws:iam::000000000000:mfa/your.name
role_arn = arn:aws:iam::999999999999:role/your.role
source_profile = default

...

aws-vault の 初回 Setup

何はともあれ、aws-vault をインストールしましょう。

$ brew install --cask aws-vault

(普段の AWS CLI と同様に) default profile のみ、Access Key ID / Secret Access Key を登録します。

$ aws-vault add default

Enter Access Key ID: xxxxxxxxxxxxxxxx
Enter Secret Access Key: 
Added credentials to profile "default" in vault

登録成功すると Credentials の欄に default と出ます。

$ aws-vault list

Profile                  Credentials              Sessions
=======                  ===========              ========
default                  default                  -
your-aws-profile                   -                        -

実行する

早速 aws-vault を実行してみましょう。 ~/.aws/config 設定の通り MFA の入力を求められます。

$ aws-vault exec your-aws-profile
Enter MFA code for arn:aws:iam::000000000000:mfa/your.name: 999999

成功すると bash Shell session が起動します。
この Shell 上では先程 aws-vault で指定した AWS Profile が有効になっています。

bash-3.2$ aws s3 ls

2022-04-01 16:12:22 your-s3-bucket-999999999999
...

aws s3 ls --profile your-aws-profile した時と同じ結果になっている。

仕組み解説

以下の通り aws-vault が生成した sub-shell 上では、MFA や Assume role を実行して発行された AWS 一時トークンが設定されています。

$ aws-vault exec your-aws-profile -- env | grep AWS_

AWS_VAULT=your-aws-profile
AWS_DEFAULT_REGION=ap-northeast-1
AWS_REGION=ap-northeast-1
AWS_ACCESS_KEY_ID=xxxxxxxxxxxxxxxx
AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxx
AWS_SESSION_TOKEN=xxxxxxxxxxxxxxxx
AWS_SECURITY_TOKEN=xxxxxxxxxxxxxxxx
AWS_SESSION_EXPIRATION=2022-05-19T12:11:14Z

見ての通り aws-vault-- 以降にコマンドを指定して実行もできます。

AWS CLI や各種言語の AWS SDK は 環境変数で設定された認証情報を最も優先して参照する ので、この Shell session では特に profile 指定をしなくても、aws-vault で指定した profile が有効になっている訳です。

0
1
1

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
1