Is there a way to export an AWS CLI Profile to Environment Variables?

后端 未结 7 972
半阙折子戏
半阙折子戏 2020-12-29 23:41

When working with certain third-party tools like Terraform, it\'s not easily possible to specify an AWS CLI profile, and I like working with the environment variables better

7条回答
  •  猫巷女王i
    2020-12-30 00:15

    None of these allow for role assumption in profiles (which I use heavily). I made the following very short script in python3 that uses boto3 to do the heavy lifting of role assumption and the like. It may be helpful.

    #!/usr/bin/env python3
    
    # export the AWS environment for a given profile
    
    import boto3
    import argparse
    
    parser = argparse.ArgumentParser(prog="exportaws",
        description="Extract AWS credentials for a profile as env variables.")
    parser.add_argument("profile", help="profile name in ~/.aws/config.")
    args = parser.parse_args()
    creds = boto3.session.Session(profile_name=args.profile).get_credentials()
    print(f'export AWS_ACCESS_KEY={creds.access_key}')
    print(f'export AWS_SECRET_ACCESS_KEY={creds.secret_key}')
    print(f'export AWS_SESSION_TOKEN={creds.token}')
    

提交回复
热议问题