Skip to content

Instantly share code, notes, and snippets.

@reecestart
Created January 24, 2018 03:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reecestart/767d4e6fd23d020f1333e41d9678e6c5 to your computer and use it in GitHub Desktop.
Save reecestart/767d4e6fd23d020f1333e41d9678e6c5 to your computer and use it in GitHub Desktop.
Create AWS Account with Python using Organizations
import time
import boto3
import pprint
# setup pprint
pp = pprint.PrettyPrinter(indent=1)
# define the connection
client = boto3.client('organizations')
# If you're using Python 3 change the below raw_input to input
NewAccountEmail = raw_input('Enter a unique Email Address for the new AWS account: ')
NewAccountName = raw_input('Enter the Name for the new AWS account: ')
NewAccountRole = raw_input('Enter the Name for Administrative Role the new AWS account: ')
response = client.create_account(
Email=NewAccountEmail,
AccountName=NewAccountName,
RoleName=NewAccountRole,
IamUserAccessToBilling='ALLOW'
)
CreateAccountStatus = str(response['CreateAccountStatus']['Id'])
pp.pprint('Checking Create Account Request Id: ' + CreateAccountStatus)
time.sleep(10) # delays for 10 seconds because the organizations client doesn't have a waiter
response = client.describe_create_account_status(
CreateAccountRequestId=CreateAccountStatus
)
NewAccountID = str(response['CreateAccountStatus']['AccountId'])
pp.pprint('New Account Id: ' + NewAccountID)
pp.pprint('New Account Name: ' + NewAccountName)
@toddlers
Copy link

toddlers commented Aug 14, 2021

just an update, you can create custom waiter

import boto3
from botocore.exceptions import WaiterError
from botocore.waiter import WaiterModel
from botocore.waiter import create_waiter_with_client

if __name__ == '__main__':
    delay = 2
    max_attempts = 2
    org = boto3.client('organizations')
    resp = org.create_account(
                Email='aws.foo@aws-notifications.myorg.io',
                AccountName='someaccountname',
                RoleName='myrolename'
            )
    account_id =  resp.get('AccountId')
    waiter_name = 'AccountCreated'

    waiter_config = {
        'version': 2,
        'waiters': {
            'AccountCreated': {
                'operation': 'DescribeCreateAccountStatus',
                'delay': delay,
                'maxAttempts': max_attempts,
                'acceptors':[
                    {
                        "matcher": "path",
                        "expected": "IN_PROGRESS",
                        "argument": "CreateAccountStatus.State",
                        "state": "retry"
                    },
                    {
                        "matcher": "path",
                        "expected": "SUCCEEDED",
                        "argument": "CreateAccountStatus.State",
                        "state": "success"
                    },
                    {
                        "matcher": "path",
                        "expected": "FAILED",
                        "argument": "CreateAccountStatus.State",
                        "state": "failure"
                    }
                ],
            },
        },
    }

    waiter_model = WaiterModel(waiter_config)
    custom_waiter = create_waiter_with_client(waiter_name=waiter_name,waiter_model=waiter_model, client=org)
    try:
        custom_waiter.wait(CreateAccountRequestId=resp.get('CreateAccountStatus').get('Id'))
        print('account created')
    except WaiterError as e:
            print(e)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment