in resources/AccountCreationLambda.py [0:0]
def deploy_resources(credentials, template, stackname, stackregion, ServiceCatalogUserName, ServiceCatalogUserPassword,account_id):
'''
Create a CloudFormation stack of resources within the new account
'''
datestamp = time.strftime("%d/%m/%Y")
client = boto3.client('cloudformation',
aws_access_key_id=credentials['AccessKeyId'],
aws_secret_access_key=credentials['SecretAccessKey'],
aws_session_token=credentials['SessionToken'],
region_name=stackregion)
print("Creating stack " + stackname + " in " + account_id)
time.sleep(120)
creating_stack = True
while creating_stack is True:
try:
creating_stack = False
create_stack_response = client.create_stack(
StackName=stackname,
TemplateBody=template,
Parameters=[
{
'ParameterKey' : 'ServiceCatalogUserName',
'ParameterValue' : ServiceCatalogUserName
},
{
'ParameterKey' : 'ServiceCatalogUserPassword',
'ParameterValue' : ServiceCatalogUserPassword
}
],
NotificationARNs=[],
Capabilities=[
'CAPABILITY_NAMED_IAM',
],
OnFailure='ROLLBACK',
Tags=[
{
'Key': 'ManagedResource',
'Value': 'True'
},
{
'Key': 'DeployDate',
'Value': datestamp
}
]
)
except botocore.exceptions.ClientError as e:
creating_stack = True
print(e)
print("Retrying...")
time.sleep(10)
stack_building = True
print("Stack creation in process...")
print(create_stack_response)
while stack_building is True:
event_list = client.describe_stack_events(StackName=stackname).get("StackEvents")
stack_event = event_list[0]
if (stack_event.get('ResourceType') == 'AWS::CloudFormation::Stack' and
stack_event.get('ResourceStatus') == 'CREATE_COMPLETE'):
stack_building = False
print("Stack construction complete.")
elif (stack_event.get('ResourceType') == 'AWS::CloudFormation::Stack' and
stack_event.get('ResourceStatus') == 'ROLLBACK_COMPLETE'):
stack_building = False
print("Stack construction failed.")
sys.exit(1)
else:
print(stack_event)
print("Stack building . . .")
time.sleep(10)
stack = client.describe_stacks(StackName=stackname)
return stack