AWS CLI + MFA を試してみた

awsmfa を利用してみた

コマンドを一発打つだけで一時セッションが発行される

  • MFA設定した環境でもコマンド一つでセッションが保存されるため特に困らない
  • ただ、yubikeyなどの物理キーがないときにMFAのsecret をどう保存したらよいかがわからなかった

設定したポリシー

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AllDenyWithoutMFA",
            "Effect": "Deny",
            "Action": [
                "*"
            ],
            "Resource": [
                "*"
            ],
            "Condition": {
                "BoolIfExists": {
                    "aws:MultiFactorAuthPresent": false
                }
            }
        }
    ]
}
# エラー確認
aws ec2 describe-instances  --region ap-northeast-1 --profile mfa-test
An error occurred (UnauthorizedOperation) when calling the DescribeInstances operation: You are not authorized to perform this operation.
# MFA不要な一時キー発行 を発行して .aws/credentials の profile mfa  に登録
awsmfa --serial-number arn_of_mfa_device --mfa-profile-name mfa --profile mfa-test ワンタイムパスワード
# 一時セッションで実行
aws ec2 describe-instances  --region ap-northeast-1 --profile mfa
{
    "Reservations": []
}