【发布时间】:2019-06-24 21:50:35
【问题描述】:
我正在使用 python 和 boto3 列出我的组织拥有的资源。我列出了我的主帐户中的资源没有问题,但我还需要列出子帐户中的资源。我可以获取儿童帐户 ID,但仅此而已。
有什么帮助吗?
【问题讨论】:
标签: python amazon-web-services boto3 aws-organizations
我正在使用 python 和 boto3 列出我的组织拥有的资源。我列出了我的主帐户中的资源没有问题,但我还需要列出子帐户中的资源。我可以获取儿童帐户 ID,但仅此而已。
有什么帮助吗?
【问题讨论】:
标签: python amazon-web-services boto3 aws-organizations
您将需要访问一组属于子帐户的凭据。
来自Accessing and Administering the Member Accounts in Your Organization - AWS Organizations:
当您使用 AWS Organizations 控制台创建成员账户时,AWS Organizations 会自动在该账户中创建一个 IAM 角色。此角色在成员账户中具有完全管理权限。该角色还配置为授予对组织主账户的访问权限。
要使用此角色访问成员账户,您必须从有权担任该角色的主账户中以用户身份登录。
因此,您可以在子账户中担任 IAM 角色,然后它会提供一组临时凭证,可与 boto3 一起用于对子账户进行 API 调用。 p>
import boto3
role_info = {
'RoleArn': 'arn:aws:iam::<AWS_ACCOUNT_NUMBER>:role/<AWS_ROLE_NAME>',
'RoleSessionName': '<SOME_SESSION_NAME>'
}
client = boto3.client('sts')
credentials = client.assume_role(**role_info)
session = boto3.session.Session(
aws_access_key_id=credentials['Credentials']['AccessKeyId'],
aws_secret_access_key=credentials['Credentials']['SecretAccessKey'],
aws_session_token=credentials['Credentials']['SessionToken']
)
更简单的方法是将角色作为新配置文件放入您的 .aws/config 文件中。然后,您可以在进行函数调用时指定配置文件:
# In ~/.aws/credentials:
[master]
aws_access_key_id=foo
aws_secret_access_key=bar
# In ~/.aws/config
[profile child1]
role_arn=arn:aws:iam:...
source_profile=master
像这样使用它:
session = boto3.session.Session(profile_name='dev')
s3 = session.client('s3')
见:How to choose an AWS profile when using boto3 to connect to CloudFront
【讨论】: