Skip to content

Instantly share code, notes, and snippets.

@jsamuel1
Created March 1, 2021 05:14
Show Gist options
  • Save jsamuel1/6af7b26ae1567cf845959fadc655e27a to your computer and use it in GitHub Desktop.
Save jsamuel1/6af7b26ae1567cf845959fadc655e27a to your computer and use it in GitHub Desktop.
Move accounts in an AWS Organization from one OU to a new OU - change variable in script then run from cloudshell
#!/usr/bin/env python
from __future__ import print_function
import boto3
import botocore
import time
import sys
import argparse
def move_account_ou(
organization_unit_id,
new_org_id,
account_id
):
client = boto3.client('organizations')
try:
move_account_response = client.move_account(AccountId=account_id, SourceParentId=organization_unit_id, DestinationParentId=new_org_id)
except botocore.exceptions.ClientError as e:
print(e)
sys.exit(1)
def main(arguments):
organization_unit_id = 'ou-xxxx-xxxxxxxx'
new_org_id = 'r-xxxx'
client = boto3.client('organizations')
accounts = client.list_accounts_for_parent(ParentId=organization_unit_id, MaxResults=10)
print(accounts)
for account in accounts['Accounts']:
print(account)
move_account_ou(organization_unit_id, new_org_id, account['Id'])
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment