建议使用多个AWS账户管理凭证的方法吗?

发布于 2021-01-29 18:58:46

管理多个Amazon Web Services(AWS)帐户的最佳方法是什么boto

我对正在使用的BotoConfig文件很熟悉。但是每个文件仅描述一个帐户…而且我正在与多个组织合作。出于所有常规的法律,财务和安全原因,这些帐户无法混合。

目前,我boto每个帐户使用一个配置文件。例如:

  • ~/.boto 默认账户
  • ~/.boto_clowncollege 用于“小丑学院”帐户
  • ~/.boto_razorassoc 用于“ razorassoc”帐户
  • ~/.boto_xyz 对于“ xyz”帐户

然后是这样的:

def boto_config_path(account=None):
    """
    Given an account name, return the path to the corresponding boto
    configuration file. If no account given, return the default config file.
    """
    path = '~/.boto' + ('_' + account if account else '')
    clean_path = os.path.abspath(os.path.expanduser(path))
    if os.path.isfile(clean_path):
        return clean_path
    else:
        errmsg = "cannot find boto config file {} for {}".format(clean_path, account)
        raise ValueError(errmsg)

def aws_credentials(account=None):
    """
    Return a tuple of AWS credentials (access key id and secret access key) for
    the given account.
    """
    try:
        cfg = INIConfig(open(boto_config_path(account)))
        return ( cfg.Credentials.aws_access_key_id, cfg.Credentials.aws_secret_access_key )
    except Exception:
        raise

conn = EC2Connection(*aws_credentials('razorassoc'))

是好还是坏?建议的改进?

关注者
0
被浏览
47
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    将来,boto将提供更好的工具来帮助您管理多个凭据,但是目前有一些环境变量可能会有所帮助。

    首先,您可以将BOTO_CONFIG设置为指向您要使用的boto配置文件,它将覆盖正常位置中找到的所有配置文件。

    其次,您可以将BOTO_PATH设置为以冒号分隔的位置列表,以查找boto配置文件,它将在正常搜索位置之前首先在其中搜索。

    这些都不能满足您的实际需求,但是可以减少代码量,从而更轻松地完成任务。

    如果您有关于如何在Boto中进行操作的想法,请告诉我!



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看