LoginSignup
8
5

More than 3 years have passed since last update.

aws-vault で AssumeRole できるようにする。

Last updated at Posted at 2019-06-15

はじめに

アホみたいなスクリプト作って AssumeRole してたんですが早く aws-vault 使えばよかったと後悔しました。

aws-vault について

Install

$ brew cask install aws-vault

Assume するユーザーの Profile 追加

# ご自身のIAMユーザーの credentials を入力する
$ aws-vault add my-profile
Enter Access Key ID: ???
Enter Secret Access Key: ???
Added credentials to profile "my-profile" in vault

# 問題なければ ~/.aws/config に下記 が入力さるはず。
$ cat ~/.aws/config
[profile my-profile]

AssumeRole の Profile を設定する

$ vim ~/.aws/config

[profile my-profile]

# 以下を追加する。
[profile my-role]
role_arn=arn:aws:iam::123456789:role/{{ Role Name }}
source_profile=my-profile
region=ap-northeast-1

動作確認

aws-vault login

設定がうまく行っていればブラウザが開きコンソールにログインできるはずです。

$ aws-vault login my-profile

aws-vault exec

-- でコマンドをチェーンして一時認証情報を取得しリソースにアクセスします。

$ aws-vault exec my-profile -- aws sqs list-queues
{
    "QueueUrls": [
        ...
    ]
}

試しに env コマンドを実行して grep してみると認証情報が環境変数に適用されていることがわかります。

$ aws-vault exec my-profile -- env | grep AWS
AWS_VAULT=my-profile
AWS_DEFAULT_REGION=ap-northeast-1
AWS_REGION=ap-northeast-1
AWS_ACCESS_KEY_ID=???
AWS_SECRET_ACCESS_KEY=???
AWS_SESSION_TOKEN=???
AWS_SECURITY_TOKEN=???

終わりに

aws-vault めっちゃ快適。

8
5
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
8
5