在AWS中,可以使用AWS Organizations来管理多个AWS账户。AWS Organizations提供了一种集中管理多个AWS账户的方式,使您可以以集中的方式管理和控制多个账户的资源和权限。
以下是一个使用AWS SDK for Python(Boto3)来创建和管理AWS Organizations中的多个账户的示例代码:
import boto3
# 创建AWS Organizations客户端
org_client = boto3.client('organizations')
# 创建新的AWS账户
def create_aws_account(email, account_name):
create_account_response = org_client.create_account(
Email=email,
AccountName=account_name
)
account_id = create_account_response['CreateAccountStatus']['AccountId']
print(f"New account created with ID: {account_id}")
return account_id
# 列出所有AWS账户
def list_aws_accounts():
list_accounts_response = org_client.list_accounts()
accounts = list_accounts_response['Accounts']
for account in accounts:
print(f"Account ID: {account['Id']}, Account Name: {account['Name']}")
# 将新账户添加到组织
def add_account_to_organization(account_id):
org_client.move_account(
AccountId=account_id,
DestinationParentId='YOUR_ORGANIZATION_ROOT_ID'
)
print("Account added to organization")
# 示例用法
new_account_id = create_aws_account('example@email.com', 'New Account')
add_account_to_organization(new_account_id)
list_aws_accounts()
请注意,在上面的代码示例中,您需要将'YOUR_ORGANIZATION_ROOT_ID'替换为您的AWS Organizations根组织ID。
上述代码将创建一个新的AWS账户,并将其添加到AWS Organizations中的组织中。然后,它将列出所有的AWS账户,以确保新账户已成功添加到组织中。
请确保您已正确配置AWS CLI凭证,以便脚本可以正常运行。
下一篇:AWS联合用户映射