I am using Django as the framework. I am using boto3 to create an AWS account in my views function. Every created aws account will have an AccountId. Before going into further details, here is my snippet :
org = boto3.client('organizations')
acc = org.create_account(
Email=email,
AccountName=lab_name,
IamUserAccessToBilling='ALLOW'
)
cid = acc['CreateAccountStatus']['Id']
time.sleep(70)
#GET ACCOUNT DETAILS
status = org.describe_create_account_status(
CreateAccountRequestId=cid
)
accid = status['CreateAccountStatus']['AccountId']
Initially I am creating the account. Like I mentioned before it takes some time (around 1 to 1.5 mins) to create the account. Then I need to GET the account details, one of the details being the AccountId. I tried increasing the sleep time to resolve this issue but that didn't help. I am getting an error when I try to GET the AccountId value in 'accid' declaration line. The error I am getting is:
KeyError: AccountId doesn't exist
This is probably happening because the account is not getting created yet and before that event my code is trying to fetch the AccountId value. How can I get the AccountId value then ? Should I try putting it in a loop or using try and except block to avoid the error message ? Please help. Thanks in advance.