forked from jamniel/AWSChinaCreateAccount
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_deploy_account.py
More file actions
181 lines (146 loc) · 5.69 KB
/
lambda_deploy_account.py
File metadata and controls
181 lines (146 loc) · 5.69 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
'''Provision Bootstrap Resources target account via CloudFormation
This module calls CloudFormation to deploy bootstrap resources in target account via a tempalte file in master account S3 Bucket.
'''
import json
import boto3
import botocore
import time
import sys
import argparse
import os
def lambda_handler(event, context):
account_name = event['account_name']
account_email = event['account_email']
account_id = event['output']['account_id']
ou_name = event['output']['ou_name']
organization_unit_id = event.get('organization_unit_id')
scp = event.get('scp')
account_role = os.environ['account_role']
stack_name = os.environ['stack_name']
stack_region = os.environ['stack_region']
bucket_name = os.environ['bucket_name']
template_file = os.environ['template_file']
metadata_bucket_name = os.environ['metadata_bucket_name']
print("Getting credential")
credentials = assume_role(account_id, account_role)
print("Deploying resources from " + template_file + " in " + bucket_name + " as " + stack_name + " in " + stack_region)
template = get_template(bucket_name, template_file)
stack = deploy_resources(credentials, template, stack_name, stack_region)
print("Resources deployed for account " + account_id + " (" + account_email + ")")
change_alias_status = change_account_alias(credentials, account_name)
print ("Account alias successfully changed!")
return {
'statusCode': 200,
'status': 'Success',
'account_name': account_name,
'account_email': account_email,
'account_id': account_id,
'ou_name': ou_name,
'stack_id': stack["Stacks"][0]["StackId"]
}
def assume_role(account_id, account_role):
'''
Assume admin role within the newly created account and return credentials
'''
sts_client = boto3.client('sts')
role_arn = 'arn:aws-cn:iam::' + account_id + ':role/' + account_role
# assume_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(10)
# get the temporary credentials
return assumedRoleObject['Credentials']
def get_template(bucket_name, template_file):
'''
Read a template file from S3 bucket and return the contents
'''
print("Reading resources from " + template_file)
s3_client = boto3.client('s3')
s3_response = s3_client.get_object(
Bucket=bucket_name,
Key=template_file,
)
cf_template_bytes = s3_response['Body'].read()
cf_template = str(cf_template_bytes, encoding = "utf8")
return cf_template
def deploy_resources(credentials, template, stack_name, stack_region):
'''
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=stack_region)
print("Creating stack " + stack_name + " in " + stack_region)
# Deploy stack in account. Error will be captured by StepFunction and start retry.
create_stack_response = client.create_stack(
StackName=stack_name,
TemplateBody=template,
NotificationARNs=[],
Capabilities=[
'CAPABILITY_NAMED_IAM',
],
OnFailure='ROLLBACK',
Tags=[
{
'Key': 'ManagedResource',
'Value': 'True'
},
{
'Key': 'DeployDate',
'Value': datestamp
},
{
'Key': 'isLandingZoneResource',
'Value': 'True'
}
]
)
stack_building = True
print("Stack creation in process...")
#print(create_stack_response)
while stack_building is True:
event_list = client.describe_stack_events(StackName=stack_name).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(15)
stack = client.describe_stacks(StackName=stack_name)
return stack
def change_account_alias(credentials, account_name):
'''
Change AWS account alias name.
'''
lower_account_name = account_name.lower()
print ("Changing account alias to " + lower_account_name)
iam_client = boto3.client('iam',
aws_access_key_id=credentials['AccessKeyId'],
aws_secret_access_key=credentials['SecretAccessKey'],
aws_session_token=credentials['SessionToken']
)
response = iam_client.create_account_alias(
AccountAlias = lower_account_name
)
return response