This repository was archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountCreationLambda.py
More file actions
607 lines (516 loc) · 31.1 KB
/
AccountCreationLambda.py
File metadata and controls
607 lines (516 loc) · 31.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
####
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
####
#!/usr/bin/env python
from __future__ import print_function
import boto3
import botocore
import time
import sys
import argparse
import os
import urllib
import json
from botocore.vendored import requests
'''AWS Organizations Create Account and Provision Resources via CloudFormation
This module creates a new account using Organizations, then calls CloudFormation to deploy baseline resources within that account via a local tempalte file.
'''
__version__ = '1.0'
__author__ = '@VKK@'
__email__ = 'vkkuchib@'
def get_client(service):
client = boto3.client(service)
return client
def create_account(event,accountname,accountemail,accountrole,access_to_billing,scp,root_id):
account_id = 'None'
client = get_client('organizations')
try:
print("Trying to create the account with {}".format(accountemail))
create_account_response = client.create_account(Email=accountemail, AccountName=accountname,
RoleName=accountrole,
IamUserAccessToBilling=access_to_billing)
# while(create_account_response['CreateAccountStatus']['State'] is 'IN_PROGRESS'):
# print(create_account_response['CreateAccountStatus']['State'])
time.sleep(40)
account_status = client.describe_create_account_status(CreateAccountRequestId=create_account_response['CreateAccountStatus']['Id'])
print("Account Creation status: {}".format(account_status['CreateAccountStatus']['State']))
if(account_status['CreateAccountStatus']['State'] == 'FAILED'):
print("Account Creation Failed. Reason : {}".format(account_status['CreateAccountStatus']['FailureReason']))
delete_respond_cloudformation(event, "FAILED", account_status['CreateAccountStatus']['FailureReason'])
sys.exit(1)
except botocore.exceptions.ClientError as e:
print("In the except module. Error : {}".format(e))
delete_respond_cloudformation(event, "FAILED", "Account Creation Failed. Deleting Lambda Function." +e+ ".")
time.sleep(10)
create_account_status_response = client.describe_create_account_status(CreateAccountRequestId=create_account_response.get('CreateAccountStatus').get('Id'))
account_id = create_account_status_response.get('CreateAccountStatus').get('AccountId')
while(account_id is None ):
create_account_status_response = client.describe_create_account_status(CreateAccountRequestId=create_account_response.get('CreateAccountStatus').get('Id'))
account_id = create_account_status_response.get('CreateAccountStatus').get('AccountId')
#move_response = client.move_account(AccountId=account_id,SourceParentId=root_id,DestinationParentId=organization_unit_id)
return(create_account_response,account_id)
def get_template(sourcebucket,baselinetemplate):
s3 = boto3.resource('s3')
try:
obj = s3.Object(sourcebucket,baselinetemplate)
return obj.get()['Body'].read().decode('utf-8')
except botocore.exceptions.ClientError as e:
print("Error accessing the source bucket. Error : {}".format(e))
return e
def delete_default_vpc(credentials,currentregion):
#print("Default VPC deletion in progress in {}".format(currentregion))
ec2_client = boto3.client('ec2',
aws_access_key_id=credentials['AccessKeyId'],
aws_secret_access_key=credentials['SecretAccessKey'],
aws_session_token=credentials['SessionToken'],
region_name=currentregion)
vpc_response = ec2_client.describe_vpcs()
for i in range(0,len(vpc_response['Vpcs'])):
if((vpc_response['Vpcs'][i]['InstanceTenancy']) == 'default'):
default_vpcid = vpc_response['Vpcs'][0]['VpcId']
subnet_response = ec2_client.describe_subnets()
subnet_delete_response = []
default_subnets = []
for i in range(0,len(subnet_response['Subnets'])):
if(subnet_response['Subnets'][i]['VpcId'] == default_vpcid):
default_subnets.append(subnet_response['Subnets'][i]['SubnetId'])
for i in range(0,len(default_subnets)):
subnet_delete_response.append(ec2_client.delete_subnet(SubnetId=default_subnets[i],DryRun=False))
#print("Default Subnets" + currentregion + "Deleted.")
igw_response = ec2_client.describe_internet_gateways()
for i in range(0,len(igw_response['InternetGateways'])):
for j in range(0,len(igw_response['InternetGateways'][i]['Attachments'])):
if(igw_response['InternetGateways'][i]['Attachments'][j]['VpcId'] == default_vpcid):
default_igw = igw_response['InternetGateways'][i]['InternetGatewayId']
#print(default_igw)
detach_default_igw_response = ec2_client.detach_internet_gateway(InternetGatewayId=default_igw,VpcId=default_vpcid,DryRun=False)
delete_internet_gateway_response = ec2_client.delete_internet_gateway(InternetGatewayId=default_igw)
#print("Default IGW " + currentregion + "Deleted.")
time.sleep(10)
delete_vpc_response = ec2_client.delete_vpc(VpcId=default_vpcid,DryRun=False)
print("Deleted Default VPC in {}".format(currentregion))
return delete_vpc_response
def create_custom_vpc(credentials,stackregion,AZ1Name,AZ2Name,VPCCIDR,SubnetAPublicCIDR,SubnetBPublicCIDR,SubnetAPrivateCIDR,SubnetBPrivateCIDR,VPCName):
#print(credentials,stackregion,AZ1Name,AZ2Name,VPCCIDR,SubnetAPublicCIDR,SubnetBPublicCIDR,SubnetAPrivateCIDR,SubnetBPrivateCIDR)
ec2 = boto3.client('ec2', aws_access_key_id=credentials['AccessKeyId'],
aws_secret_access_key=credentials['SecretAccessKey'],
aws_session_token=credentials['SessionToken'],
region_name=stackregion)
# create VPC
vpc = ec2.create_vpc(CidrBlock=VPCCIDR)
# # we can assign a name to vpc, or any resource, by using tag
#vpc.create_tags(Tags=[{"Key": "Name", "Value": VPCName}])
# vpc.wait_until_available()
vpc_id = vpc['Vpc']['VpcId']
create_tags_response = ec2.create_tags(Resources = [vpc_id], Tags = [ {'Key' : 'Name' , 'Value' : VPCName}])
print("VPC ID : {}".format(vpc_id))
#create an EIP for NAT Gateway
eipnatgwA = ec2.allocate_address(Domain='vpc')
eipnatgwB = ec2.allocate_address(Domain='vpc')
print("EIP for NatGW A : {}".format(eipnatgwA['AllocationId']))
print("EIP for NatGW B : {}".format(eipnatgwB['AllocationId']))
# create then attach internet gateway
ig = ec2.create_internet_gateway()
ig_id = ig['InternetGateway']['InternetGatewayId']
ec2.attach_internet_gateway(InternetGatewayId=ig_id,VpcId=vpc_id)
print("InternetGateway : {}".format(ig_id))
# create a route table and a public route
public_route_table = ec2.create_route_table(VpcId=vpc_id)
public_route_table_id = public_route_table['RouteTable']['RouteTableId']
public_route = ec2.create_route(DestinationCidrBlock='0.0.0.0/0',GatewayId=ig_id,RouteTableId=public_route_table_id)
print("Public Route Table ID : {}".format(public_route_table_id))
# create subnets
publicsubnetA = ec2.create_subnet(AvailabilityZone=AZ1Name,CidrBlock=SubnetAPublicCIDR, VpcId=vpc_id)
publicsubnetB = ec2.create_subnet(AvailabilityZone=AZ2Name,CidrBlock=SubnetBPublicCIDR, VpcId=vpc_id)
public_subnetA_id = publicsubnetA['Subnet']['SubnetId']
public_subnetB_id = publicsubnetB['Subnet']['SubnetId']
print("Public SubnetA ID : {}".format(public_subnetA_id))
print("Public SubnetB ID : {}".format(public_subnetB_id))
privatesubnetA = ec2.create_subnet(AvailabilityZone=AZ1Name,CidrBlock=SubnetAPrivateCIDR, VpcId=vpc_id)
privatesubnetB = ec2.create_subnet(AvailabilityZone=AZ2Name,CidrBlock=SubnetBPrivateCIDR, VpcId=vpc_id)
private_subnetA_id = privatesubnetA['Subnet']['SubnetId']
private_subnetB_id = privatesubnetB['Subnet']['SubnetId']
print("Private SubnetA ID : {}".format(private_subnetA_id))
print("Private SubnetB ID : {}".format(private_subnetB_id))
# associate the route table with the subnet
public_route_table_associationA = ec2.associate_route_table(SubnetId=public_subnetA_id,RouteTableId=public_route_table_id)
public_route_table_associationB = ec2.associate_route_table(SubnetId=public_subnetB_id,RouteTableId=public_route_table_id)
print("Public Route Table Association ID for Subnet A : {}".format(public_route_table_associationA['AssociationId']))
print("Public Route Table Association ID for Subnet B : {}".format(public_route_table_associationB['AssociationId']))
# create then attach a NAT gateway
ngwsubnetA = ec2.create_nat_gateway(AllocationId=eipnatgwA['AllocationId'],SubnetId=public_subnetA_id)
ngwsubnetB = ec2.create_nat_gateway(AllocationId=eipnatgwB['AllocationId'],SubnetId=public_subnetB_id)
ngwsubnetA_id = ngwsubnetA['NatGateway']['NatGatewayId']
ngwsubnetB_id = ngwsubnetB['NatGateway']['NatGatewayId']
print("NAT GW in Subnet A : {}".format(ngwsubnetA_id))
print("NAT GW in Subnet B : {}".format(ngwsubnetB_id))
time.sleep(60)
private_route_table_A = ec2.create_route_table(VpcId=vpc_id)
private_route_table_A_id = private_route_table_A['RouteTable']['RouteTableId']
private_route_A = ec2.create_route(DestinationCidrBlock='0.0.0.0/0',NatGatewayId=ngwsubnetA_id,RouteTableId=private_route_table_A_id)
print("Private Route Table A ID : {}".format(private_route_table_A_id))
private_route_table_B = ec2.create_route_table(VpcId=vpc_id)
private_route_table_B_id = private_route_table_B['RouteTable']['RouteTableId']
private_route_B = ec2.create_route(DestinationCidrBlock='0.0.0.0/0',NatGatewayId=ngwsubnetB_id,RouteTableId=private_route_table_B_id)
print("Private Route Table B ID : {}".format(private_route_table_B_id))
private_route_table_associationA = ec2.associate_route_table(SubnetId=private_subnetA_id,RouteTableId=private_route_table_A_id)
private_route_table_associationB = ec2.associate_route_table(SubnetId=private_subnetB_id,RouteTableId=private_route_table_B_id)
print("Private Route Table Association ID for Subnet A : {}".format(private_route_table_associationA['AssociationId']))
print("Private Route Table Association ID for Subnet B : {}".format(private_route_table_associationB['AssociationId']))
def deploy_resources(credentials, template, stackname, stackregion, adminusername, adminpassword,account_id,newrole_arn):
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)
creating_stack = True
try:
while creating_stack is True:
try:
creating_stack = False
create_stack_response = client.create_stack(
StackName=stackname,
TemplateBody=template,
Parameters=[
{
'ParameterKey' : 'AdminUsername',
'ParameterValue' : adminusername
},
{
'ParameterKey' : 'AdminPassword',
'ParameterValue' : adminpassword
},
{
'ParameterKey' : 'NewRoleArn',
'ParameterValue' : newrole_arn
}
],
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
except botocore.exceptions.ClientError as e:
print("Error deploying stack.There might be an error either accessing the Source bucket or accessing the baseline template from the source bucket.Error : {}".format(e))
return e
def assume_role(account_id, account_role):
sts_client = boto3.client('sts')
role_arn = 'arn:aws:iam::' + account_id + ':role/' + account_role
assuming_role = True
while assuming_role is True:
try:
assuming_role = False
assumedRoleObject = sts_client.assume_role(
RoleArn=role_arn,
RoleSessionName="NewAccountRole"
)
except botocore.exceptions.ClientError as e:
assuming_role = True
print(e)
print("Retrying...")
time.sleep(60)
# From the response that contains the assumed role, get the temporary
# credentials that can be used to make subsequent API calls
return assumedRoleObject['Credentials']
def get_ou_name_id(root_id,organization_unit_name):
ou_client = get_client('organizations')
list_of_OU_ids = []
list_of_OU_names = []
ou_name_to_id = {}
list_of_OUs_response = ou_client.list_organizational_units_for_parent(ParentId=root_id)
for i in list_of_OUs_response['OrganizationalUnits']:
list_of_OU_ids.append(i['Id'])
list_of_OU_names.append(i['Name'])
if(organization_unit_name not in list_of_OU_names):
print("The provided Organization Unit Name doesnt exist. Creating an OU named: {}".format(organization_unit_name))
try:
ou_creation_response = ou_client.create_organizational_unit(ParentId=root_id,Name=organization_unit_name)
for k,v in ou_creation_response.items():
for k1,v1 in v.items():
if(k1 == 'Name'):
organization_unit_name = v1
if(k1 == 'Id'):
organization_unit_id = v1
except botocore.exceptions.ClientError as e:
print("Error in creating the OU: {}".format(e))
respond_cloudformation(event, "FAILED", { "Message": "Could not list out AWS Organization OUs. Account creation Aborted."})
else:
for i in range(len(list_of_OU_names)):
ou_name_to_id[list_of_OU_names[i]] = list_of_OU_ids[i]
organization_unit_id = ou_name_to_id[organization_unit_name]
return(organization_unit_name,organization_unit_id)
def create_newrole(newrole,top_level_account,credentials,newrolepolicy):
iam_client = boto3.client('iam',aws_access_key_id=credentials['AccessKeyId'],
aws_secret_access_key=credentials['SecretAccessKey'],
aws_session_token=credentials['SessionToken'])
print("arn:aws:iam::"+top_level_account+":root")
trust_policy_document = json.dumps(
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::"+top_level_account+":root"
},
"Action": "sts:AssumeRole"
}
]
}
)
print(trust_policy_document)
#new_role_policy = json.dumps(newrolepolicy)
print(newrolepolicy)
try:
create_role_response = iam_client.create_role(RoleName=newrole,AssumeRolePolicyDocument=trust_policy_document,Description=newrole,MaxSessionDuration=3600)
print(create_role_response['Role']['Arn'])
except botocore.exceptions.ClientError as e:
print("Error Occured in creating a role : {}",format(e))
try:
#attach_policy_response = iam_client.attach_role_policy(RoleName=newrole,PolicyArn='arn:aws:iam::aws:policy/AdministratorAccess')
update_role_response = iam_client.put_role_policy(RoleName=newrole,PolicyName='NewRolePolicy',PolicyDocument=newrolepolicy)
except botocore.exceptions.ClientError as e:
print("Error attaching policy to the role : {}".format(e))
print("{},{},{}".format(newrole,top_level_account,credentials))
return create_role_response['Role']['Arn']
def selfinvoke(event,status):
lambda_client = boto3.client('lambda')
function_name = os.environ['AWS_LAMBDA_FUNCTION_NAME']
event['RequestType'] = status
print('invoking itself ' + function_name)
response = lambda_client.invoke(FunctionName=function_name, InvocationType='Event',Payload=json.dumps(event))
def respond_cloudformation(event, status, data=None):
responseBody = {
'Status': status,
'Reason': 'See the details in CloudWatch Log Stream',
'PhysicalResourceId': event['ServiceToken'],
'StackId': event['StackId'],
'RequestId': event['RequestId'],
'LogicalResourceId': event['LogicalResourceId'],
'Data': data
}
print('Response = ' + json.dumps(responseBody))
print(event)
requests.put(event['ResponseURL'], data=json.dumps(responseBody))
def delete_respond_cloudformation(event, status, message):
responseBody = {
'Status': status,
'Reason': message,
'PhysicalResourceId': event['ServiceToken'],
'StackId': event['StackId'],
'RequestId': event['RequestId'],
'LogicalResourceId': event['LogicalResourceId']
}
requests.put(event['ResponseURL'], data=json.dumps(responseBody))
lambda_client = get_client('lambda')
function_name = os.environ['AWS_LAMBDA_FUNCTION_NAME']
print('Deleting resources and rolling back the stack.')
lambda_client.delete_function(FunctionName=function_name)
#requests.put(event['ResponseURL'], data=json.dumps(responseBody))
def main(event,context):
print(event)
client = get_client('organizations')
accountname = os.environ['accountname']
accountemail = os.environ['accountemail']
newrole = os.environ['newrole']
newrolepolicy = os.environ['newrolepolicy']
organization_unit_name = os.environ['organizationunitname']
accountrole = 'OrganizationAccountAccessRole'
stackname = os.environ['stackname']
stackregion = os.environ['stackregion']
adminusername = os.environ['adminusername']
adminpassword = os.environ['adminpassword']
sourcebucket = os.environ['sourcebucket']
baselinetemplate = os.environ['baselinetemplate']
AZ1Name = os.environ['AZ1Name']
AZ2Name = os.environ['AZ2Name']
VPCCIDR = os.environ['VPCCIDR']
SubnetAPublicCIDR = os.environ['SubnetAPublicCIDR']
SubnetBPublicCIDR = os.environ['SubnetBPublicCIDR']
SubnetAPrivateCIDR = os.environ['SubnetAPrivateCIDR']
SubnetBPrivateCIDR = os.environ['SubnetBPrivateCIDR']
VPCName = os.environ['VPCName']
access_to_billing = "DENY"
scp = None
#account_id = None
RegiontoAZMap = {
"ap-northeast-1": ["ap-northeast-1a","ap-northeast-1c"],
"ap-northeast-2": [ "ap-northeast-2a","ap-northeast-2c"],
"ap-northeast-3": [ "ap-northeast-3a" ],
"ap-south-1": [ "ap-south-1a","ap-south-1b"],
"ap-southeast-1": [ "ap-southeast-1a","ap-southeast-1b","ap-southeast-1c"],
"ap-southeast-2": [ "ap-southeast-2a","ap-southeast-2b","ap-southeast-2c"],
"ca-central-1": ["ca-central-1a","ca-central-1b"],
"eu-central-1": ["eu-central-1a","eu-central-1b","eu-central-1c"],
"eu-west-1": [ "eu-west-1a","eu-west-1b","eu-west-1c"],
"eu-west-2": [ "eu-west-2a","eu-west-2b","eu-west-2c"],
"eu-west-3": [ "eu-west-3a","eu-west-3b","eu-west-3c"],
"sa-east-1": [ "sa-east-1a","sa-east-1c"],
"us-east-1": [ "us-east-1a","us-east-1b","us-east-1c","us-east-1d","us-east-1e","us-east-1f"],
"us-east-2": [ "us-east-2a","us-east-2b","us-east-2c"],
"us-west-1": [ "us-west-1b","us-west-1c"],
"us-west-2": [ "us-west-2a","us-west-2b","us-west-2c"]
}
if (event['RequestType'] == 'Create'):
selfinvoke(event,'Wait')
top_level_account = event['ServiceToken'].split(':')[4]
org_client = get_client('organizations')
preferred_az_list = RegiontoAZMap[stackregion]
if(AZ1Name in preferred_az_list and AZ2Name in preferred_az_list):
print("The selected AZs and the stackregion are compatible.")
else:
print("{} and {} are not in the selected stack region: {}".format(AZ1Name,AZ2Name,stackregion))
# delete_respond_cloudformation(event, "FAILED", {
# #"Message":"AZ1Name: "+AZ1Name+" AZ2Name: "+AZ2Name+" is not from the selected region:"+stackregion+".Stack Regions and VPC AZ's should be in the same AWS region."})
# "Message" : "The selected AWS Region and AZs wont match.The AZs selected for deploying the VPC should be in the "+stackregion+".",
# })
# if(not accountname or not accountemail or not newrole or not newrolepolicy or not organization_unit_name or not stackname or not stackregion or not adminusername or not adminpassword or not sourcebucket or not baselinetemplate or not AZ1Name or not AZ2Name or not VPCCIDR or not SubnetAPublicCIDR or not SubnetBPublicCIDR or not SubnetAPrivateCIDR or not SubnetBPrivateCIDR):
# print("Please provide all parameter values and try again.")
# delete_respond_cloudformation(event, "FAILED", {"Message":"Provide all the parameters and launch the product again"})
try:
list_roots_response = org_client.list_roots()
#print(list_roots_response)
root_id = list_roots_response['Roots'][0]['Id']
except:
root_id = "Error"
if root_id is not "Error":
print("Creating new account: " + accountname + " (" + accountemail + ")")
### List the available AWS Oranization OU's
#if(organization_unit_name is not None):
#(organization_unit_name,organization_unit_id) = get_ou_name_id(root_id,organization_unit_name)
(create_account_response,account_id) = create_account(event,accountname,accountemail,accountrole,access_to_billing,scp,root_id)
#print(create_account_response)
print("Created acount:{}\n".format(account_id))
#attach_policy_response = org_client.attach_policy(PolicyId=scp_id,TargetId=account_id)
credentials = assume_role(account_id, accountrole)
#print("Deploying resources from " + templatefile + " as " + stackname + " in " + stackregion)
# template = get_template(sourcebucket,baselinetemplate)
# stack = deploy_resources(credentials, template, stackname, stackregion, adminusername, adminpassword,account_id)
# print(stack)
ec2_client = get_client('ec2')
try:
custom_vpc_id = create_custom_vpc(credentials,stackregion,AZ1Name,AZ2Name,VPCCIDR,SubnetAPublicCIDR,SubnetBPublicCIDR,SubnetAPrivateCIDR,SubnetBPrivateCIDR,VPCName)
except botocore.exceptions.ClientError as e:
print("There might be an error creating the custom VPC. Error : {}".format(e))
try:
newrole_arn = create_newrole(newrole,top_level_account,credentials,newrolepolicy)
except botocore.exceptions.ClientError as e:
print("Error creating the specified role. Error : {}".format(e))
newrole_arn = "arn:aws:iam::"+account_id+":role/"+newrole
print(newrole_arn)
template = get_template(sourcebucket,baselinetemplate)
stack = deploy_resources(credentials, template, stackname, stackregion, adminusername, adminpassword,account_id,newrole_arn)
print(stack)
print("Resources deployment for account " + account_id + " (" + accountemail + ") complete !!")
regions = []
regions_response = ec2_client.describe_regions()
for i in range(0,len(regions_response['Regions'])):
regions.append(regions_response['Regions'][i]['RegionName'])
for r in regions:
try:
#print('In the VPC deletion block - {}'.format(r))
delete_vpc_response = delete_default_vpc(credentials,r)
except botocore.exceptions.ClientError as e:
print("An error occured while deleting Default VPC in {}. Error: {}".format(r,e))
i+=1
root_id = client.list_roots().get('Roots')[0].get('Id')
#print(root_id)
#print('Outside try block - {}'.format(organization_unit_name))
if(organization_unit_name!='None'):
try:
(organization_unit_name,organization_unit_id) = get_ou_name_id(root_id,organization_unit_name)
move_response = org_client.move_account(AccountId=account_id,SourceParentId=root_id,DestinationParentId=organization_unit_id)
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r} "
message = template.format(type(ex).__name__, ex.args)
print(message)
if scp is not None:
attach_policy_response = client.attach_policy(PolicyId=scp, TargetId=account_id)
print("Attach policy response "+str(attach_policy_response))
#respond_cloudformation(event, "SUCCESS", { "Message": "Account Created! URL : https://" +account_id+".signin.aws.amazon.com/console", "AccountID" : account_id, "LoginURL" : "https://console.aws.amazon.com", "Username" : adminusername })
respond_cloudformation(event, "SUCCESS", { "Message": "Account Created!",
"LoginURL" : "https://"+account_id+".signin.aws.amazon.com/console?region="+stackregion+"#",
"AccountID" : account_id,
"Username" : adminusername,
"Role" : newrole,
"Stackregion": stackregion })
else:
print("Cannot access the AWS Organization ROOT. Contact the master account Administrator for more details.")
#sys.exit(1)
delete_respond_cloudformation(event, "FAILED", "Cannot access the AWS Organization ROOT. Contact the master account Administrator for more details.Deleting Lambda Function.")
if(event['RequestType'] == 'Update'):
print("Template in Update Status")
respond_cloudformation(event, "SUCCESS", { "Message": "Resource update successful!" })
#respond_cloudformation(event, "SUCCESS", { "Message": "Account Created!","Login URL : "https://" +account_id+".signin.aws.amazon.com/console", "AccountID" : account_id, "Username" : adminusername, "Role" : newrole })
# elif(event['RequestType'] == 'Wait'):
# # account_status = 'IN_PROGRESS'
# # create_account_status_response = client.describe_create_account_status(CreateAccountRequestId=create_account_response.get('CreateAccountStatus').get('Id'))
# # while account_status == 'IN_PROGRESS':
# # create_account_status_response = client.describe_create_account_status(CreateAccountRequestId=create_account_response.get('CreateAccountStatus').get('Id'))
# # print("Create In Progress. Create Account Status Response : {} \n".format(create_account_status_response))
# # account_status = create_account_status_response.get('CreateAccountStatus').get('State')
# # if account_status == 'SUCCEEDED':
# # account_id = create_account_status_response.get('CreateAccountStatus').get('AccountId')
# # print("Account Creation SUCCEEDED. Create Account Status Response for account URL : {}\n".format(create_account_status_response))
# # elif account_status == 'FAILED':
# # print("Account creation failed: " + create_account_status_response.get('CreateAccountStatus').get('FailureReason'))
# time.sleep(30)
# respond_cloudformation(event, "Aborted", { "Message": "Retuned back form the wait condition !!" })
# exit()
elif(event['RequestType'] == 'Delete'):
try:
delete_respond_cloudformation(event, "SUCCESS", "Delete Request Initiated. Deleting Lambda Function.")
except:
print("Couldnt initiate delete response.")