LoginSignup
2
0

More than 3 years have passed since last update.

terraformerをmaster accountからassume roleでアクセス&MFA必須なAWS環境で使う

Posted at

TL:DR

  • AWSではMaster AccountにIAM Userでログインし、他のアカウント(OU)へアクセスできる権限をもったRoleへSwitch/AssumeRoleして必要な作業を行うケースが多いです。
  • ポリシーとしてSwitch/AssumeRoleの際に、"MFAが有効化"されていることを必須としていることも多いです。
  • 上記環境にてSwitch/AssumeRole先の情報をterraformerで引っ張る場合、AssumeRoleとMFAへの対応を行う必要があります。
  • STSのToken管理はaws-vaultにお任せ。aws-vaultでAssumeRoleとMFA対応をします。
  • なので実態としては、aws-vaultでterraformerをAssumeRole&MFA対応させるお話です。

aws-vaultでterraformerをAssumeRole&MFA対応させる

aws-vault
https://github.com/99designs/aws-vault

terraformer
https://github.com/GoogleCloudPlatform/terraformer

terraformerのインストール

リポジトリのREADMEにあるこの辺を参考にどうぞ。

今回はhomebrewで
brew install terraformer

aws-vaultでMFAするための設定

  • 事前に必要なもの

    • terraformerが利用するMaster Account上のIAM UserのAccess KeyとSecrets(A)
    • (A)に紐付いたMFAデバイスのARN(B)
      • こういうやつ → arn:aws:iam::xxxxxxxxxxxx:mfa/yourIAM
    • (A)からAssume/SwitchRole可能なRoleのARN(C)
      • こういうやつ → arn:aws:iam::xxxxxxxxxxxx:role/hoge-prov
  • aws-vaultで使うprofile追加

$ aws-vault add hoge-prov
※Access KeyとSecretsを聞かれるので(A)を登録する
  • profileに下記を登録する
    • MFAデバイスのARN(B)
    • AssumeRoleするRoleのARN(C)
~/.aws/config
~(snip)~

[profile hoge-prov] ← 先程追加したprofileの項
mfa_serial = arn:aws:iam::xxxxxxxxxxxx:mfa/yourIAM ←ここに(B)を登録する
role_arn = arn:aws:iam::xxxxxxxxxxxx:role/hoge-prov ←ここに(C)を登録する

aws-vaultでMFAしつつterraformer importを実行してみる

$ aws-vault exec hoge-prov -- terraformer import aws --resources=s3 --regions=ap-northeast-1
Enter token for arn:aws:iam::xxxxxxxxxxxx:mfa/yourIAM: 
※^ MFAデバイスのTokenを入力してあげましょう

以下実行結果
2019/12/26 16:26:24 aws importing region ap-northeast-1
2019/12/26 16:26:24 aws importing... s3
2019/12/26 16:26:30 Refreshing state... aws_s3_bucket_policy.xxxx
2019/12/26 16:26:30 Refreshing state... aws_s3_bucket.xxxx
2019/12/26 16:26:31 aws Connecting.... 
2019/12/26 16:26:31 aws save s3
2019/12/26 16:26:31 aws save tfstate for s3

こんな感じにterraformerでのimport対象リソースのtfが ./generated 以下に生成されていればok
Screen Shot 2019-12-26 at 18.17.52.png

terraformerの処理に時間がかかりすぎてセッション切れしちゃう場合

execの下記オプションでセッションの有効期限を変更できるようなのでお試しあれ

$ aws-vault exec --help
~(snip)~

  -t, --session-ttl=4h           Expiration time for aws session
      --assume-role-ttl=15m      Expiration time for aws assumed role

~(snip)~
2
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
2
0