您可以使用AWS的boto3库来列出组织中的所有ORG ID,包括嵌套的。以下是一个示例代码:
import boto3
def list_nested_org_ids(org_client, parent_id):
org_ids = []
# 获取当前组织单元下的所有子组织单元
response = org_client.list_children(
ParentId=parent_id,
ChildType='ORGANIZATIONAL_UNIT'
)
for child in response['Children']:
# 如果子组织单元是一个ORG(组织),则将其ID添加到列表中
if child['Type'] == 'ORGANIZATION':
org_ids.append(child['Id'])
else:
# 如果子组织单元是一个OU(组织单元),则递归调用函数来获取其下的所有ORG ID
org_ids.extend(list_nested_org_ids(org_client, child['Id']))
return org_ids
# 创建组织服务的客户端
org_client = boto3.client('organizations')
# 获取根组织的ID
response = org_client.describe_organization()
root_id = response['Organization']['Id']
# 列出所有组织ID
org_ids = list_nested_org_ids(org_client, root_id)
# 打印所有组织ID
for org_id in org_ids:
print(org_id)
请确保您已经安装了boto3库,并且配置了正确的AWS凭证。这段代码会递归地遍历组织单元树,并将所有组织(ORG)的ID添加到一个列表中,然后将其打印出来。