How to use MFA with AWS CLI?

前端 未结 13 1333
太阳男子
太阳男子 2020-12-05 00:05

How do I type in the MFA code when using the AWS CLI? I have checked the documentation page of IAM http://docs.aws.amazon.com/cli/latest/reference/iam/index.html.

I

13条回答
  •  鱼传尺愫
    2020-12-05 00:36

    Wrote a tool to add MFA support for standard IAM user profiles until @outcoldman PR gets merged: https://github.com/tongueroo/aws-mfa-secure

    Setup for those in a hurry

    1. Install gem
    gem install aws-mfa-secure
    
    1. Setup your ~/.aws/credentials with mfa_serial

    ~/.aws/credentials:

    [mfa]
    aws_access_key_id = BKCAXZ6ODJLQ1EXAMPLE
    aws_secret_access_key = ABCDl4hXikfOHTvNqFAnb2Ea62bUuu/eUEXAMPLE
    mfa_serial = arn:aws:iam::112233445566:mfa/MFAUser
    
    1. Add the alias to your ~/.bash_profile
    alias aws="aws-mfa-secure session"
    

    Restart your terminal.

    Example with Output

    $ export AWS_PROFILE=mfa
    $ aws s3 ls
    Please provide your MFA code: 751888
    2019-09-21 15:53:34 my-example-test-bucket
    $ aws s3 ls
    2019-09-21 15:53:34 my-example-test-bucket
    $
    

    Assume Role Profiles

    Assume role profiles work already for the AWS CLI, here's an example:

    ~/.aws/credentials:

    [mfa]
    aws_access_key_id = BKCAXZ6ODJLQ1EXAMPLE
    aws_secret_access_key = ABCDl4hXikfOHTvNqFAnb2Ea62bUuu/eUEXAMPLE
    mfa_serial = arn:aws:iam::112233445566:mfa/MFAUser
    
    [assumed-role]
    role_arn = arn:aws:iam::112233445566:role/Admin
    source_profile = mfa
    role_session_name = MFAUser
    mfa_serial = arn:aws:iam::112233445566:mfa/MFAUser
    

提交回复
热议问题