-
Notifications
You must be signed in to change notification settings - Fork 2
/
ManagementPrep.yml
335 lines (314 loc) · 12.5 KB
/
ManagementPrep.yml
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
########################################################################################################################
# Any code, applications, scripts, templates, proofs of concept, documentation and other items provided by AWS under #
# this SOW are "AWS Content," as defined in the Agreement, and are provided for illustration purposes only. All such #
# AWS Content is provided solely at the option of AWS, and is subject to the terms of the Addendum and the Agreement. #
# Customer is solely responsible for using, deploying, testing, and supporting any code and applications provided by #
# AWS under this SOW. #
########################################################################################################################
AWSTemplateFormatVersion: '2010-09-09'
Description: Prepares the Management Account for adding custom solutions
Parameters:
OrganizationId:
Type: 'String'
Description: AWS Organization Id of Control Tower
OrganizationsArn:
Type: 'String'
Description: ARN of the AWS Organization
ManagementAccountId:
Type: 'String'
Description: AWS Account ID of the Organization Management Account
AuditAccountId:
Type: 'String'
Description: AWS Account ID of the Audit Account
LogsArchiveAccountId:
Type: 'String'
Description: AWS Account ID of the Logs Archive Account
NetworkHubAccountId:
Type: 'String'
Description: AWS Account ID of the Network Hub Account
CommonServicesAccountId:
Type: 'String'
Description: AWS Account ID of the Common Services Account
SecurityServicesAccountId:
Type: 'String'
Description: AWS Account ID of the Security Services Account
DevelopmentOrganizationUnitId:
Type: 'String'
Description: AWS Organization unit Id for Development Accounts
SharedServicesOrganizationUnitId:
Type: 'String'
Description: AWS Organization unit Id for Non Production Accounts
ProductionOrganizationUnitId:
Type: 'String'
Description: AWS Organization unit Id for Production Accounts
NameforSSMParameterforOrganizationId:
Type: String
Description: Name of the SSM Parameter under which the AWS Organization ID of the LZ will be stored
Default: /org/core/OrganizationID
NameforSSMParameterforOrganizationsArn:
Type: String
Description: Name of the SSM Parameter under which the name of Organizations Arn will be stored
Default: /org/core/OrganizationsArn
NameforSSMParameterforDevelopmentOrganizationUnitId:
Type: String
Description: Name of the SSM Parameter under which the name of Development OU will be stored
Default: /org/core/DevelopmentOU
NameforSSMParameterforSharedServicestOrganizationUnitId:
Type: String
Description: Name of the SSM Parameter under which the name of SharedServices OU will be stored
Default: /org/core/SharedServicesOU
NameforSSMParameterforProductionOrganizationUnitId:
Type: String
Description: Name of the SSM Parameter under which the name of SharedServices OU will be stored
Default: /org/core/ProductionOU
NameforSSMParameterforManagementAccountId:
Type: String
Description: Name of the SSM Parameter under which the name of Management AWS account ID will be stored
Default: /org/core/ManagementAccountId
NameforSSMParameterforNetworkHubAccountId:
Type: String
Description: Name of the SSM Parameter under which the name of NetworkHub account will be stored
Default: /org/core/NetworkHubAccountId
NameforSSMParameterforCommonServicesAccountId:
Type: String
Description: Name of the SSM Parameter under which the name of CommonServices account will be stored
Default: /org/sharedservice/commonservices/CommonServicesAccount
NameforSSMParameterforSecurityAccountId:
Type: String
Description: Name of the SSM Parameter under which the name of CommonServices account will be stored
Default: /org/sharedservice/security/SecurityAccount
NameforSSMParameterforListofAccounts:
Type: String
Description: Name of the SSM Parameter under which the list of all AWS accounts in the LZ will be stored
Default: /org/accounts
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
-
Label:
default: "Key Account IDs"
Parameters:
- ManagementAccountId
- AuditAccountId
- LogsArchiveAccountId
- NetworkHubAccountId
- CommonServicesAccountId
- SecurityServicesAccountId
-
Label:
default: "Organization and Organization Units"
Parameters:
- OrganizationId
- DevelopmentOrganizationUnitId
- SharedServicesOrganizationUnitId
- ProductionOrganizationUnitId
-
Label:
default: "SSM Parameter Keys"
Parameters:
- NameforSSMParameterforOrganizationId
- NameforSSMParameterforDevelopmentOrganizationUnitId
- NameforSSMParameterforSharedServicestOrganizationUnitId
- NameforSSMParameterforProductionOrganizationUnitId
- NameforSSMParameterforManagementAccountId
- NameforSSMParameterforNetworkHubAccountId
- NameforSSMParameterforCommonServicesAccountId
- NameforSSMParameterforSecurityAccountId
- NameforSSMParameterforListofAccounts
Resources:
OrganizationIDSSMParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Ref NameforSSMParameterforOrganizationId
Type: String
Value: !Ref OrganizationId
DevelopmentOrganizationUnitIDSSMParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Ref NameforSSMParameterforDevelopmentOrganizationUnitId
Type: String
Value: !Ref DevelopmentOrganizationUnitId
SharedServicesOrganizationUnitIDSSMParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Ref NameforSSMParameterforSharedServicestOrganizationUnitId
Type: String
Value: !Ref SharedServicesOrganizationUnitId
ProductionOrganizationUnitIDSSMParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Ref NameforSSMParameterforProductionOrganizationUnitId
Type: String
Value: !Ref ProductionOrganizationUnitId
ManagementAccountIdSSMParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Ref NameforSSMParameterforManagementAccountId
Type: String
Value: !Ref ManagementAccountId
NetworkHubAccountIDSSMParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Ref NameforSSMParameterforNetworkHubAccountId
Type: String
Value: !Ref NetworkHubAccountId
OrganizationsArnSSMParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Ref NameforSSMParameterforOrganizationsArn
Type: String
Value: !Ref OrganizationsArn
CommonServicesAccountIdSSMParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Ref NameforSSMParameterforCommonServicesAccountId
Type: String
Value: !Ref CommonServicesAccountId
SecurityAccountIdSSMParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Ref NameforSSMParameterforSecurityAccountId
Type: String
Value: !Ref SecurityServicesAccountId
EnableRAMLambdaFunctionRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: EnableRAMSharing
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- organizations:EnableAWSServiceAccess
- iam:CreateServiceLinkedRole
- organizations:DescribeOrganization
- ram:EnableSharingWithAwsOrganization
- organizations:RegisterDelegatedAdministrator
Resource: "*"
EnableRAMLambdaFunction: # needs no monitoring because it is used as a custom resource
Type: 'AWS::Lambda::Function'
Properties:
Code:
ZipFile:
!Sub |
# Import statements
import boto3, sys
import json
import cfnresponse
import os
from botocore.exceptions import ClientError
import urllib.request
# Start Lambda function
def enable_ram(event, context):
print("Enabling Resource Access Manager to Interact with Organization")
try:
client = boto3.client('ram')
response = client.enable_sharing_with_aws_organization()
print("SUCCESS")
responseStatus = 'SUCCESS'
responseData = {}
sendResponse(event, context, responseStatus, responseData)
except ClientError as ex:
print('Something went wrong - see below')
print(ex.response['Error']['Message'])
print("Respond: FAILED")
responseStatus = 'FAILED'
responseData = {}
sendResponse(event, context, responseStatus, responseData)
def sendResponse(event, context, responseStatus, responseData):
data = json.dumps({
'Status': responseStatus,
'PhysicalResourceId': context.log_stream_name,
'StackId': event['StackId'],
'RequestId': event['RequestId'],
'LogicalResourceId': event['LogicalResourceId'],
'Data': responseData
})
print(data)
opener = urllib.request.build_opener(urllib.request.HTTPHandler)
request = urllib.request.Request(url=event['ResponseURL'], data=data.encode('utf-8'))
request.add_header('Content-Type', '')
request.get_method = lambda: 'PUT'
url = opener.open(request)
print('HTTP Request Sent')
return
def lambda_handler(event, context):
client = event['ResourceProperties']
print('event=', event)
print('client=', client)
if event['RequestType'] in ["Create","Update"]:
print(f"{event['RequestType']}")
enable_ram(event, context)
# Handle a CloudFormation resource delete event
if event['RequestType'] == "Delete":
print("Received Delete request: Nothing to Delete")
responseStatus = 'SUCCESS'
responseData = {}
sendResponse(event, context, responseStatus, responseData)
Handler: "index.lambda_handler"
MemorySize: 128
Role: !GetAtt "EnableRAMLambdaFunctionRole.Arn"
Runtime: "python3.6"
Timeout: 60
RAMEnabler:
Type: Custom::RAMEnabler
DependsOn:
- EnableRAMLambdaFunction
Properties:
ServiceToken: !GetAtt EnableRAMLambdaFunction.Arn
AWSControlTowerExecutionforManagement:
Type: AWS::IAM::Role
Properties:
RoleName: AWSControlTowerExecution
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AdministratorAccess
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
AWS:
- !Join [ "", ["arn:aws:iam::", !Ref ManagementAccountId, ":root"]]
Action:
- sts:AssumeRole
Path: "/"
rOrgRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: "CrossAccountOrganization"
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
AWS: "*"
Action:
- 'sts:AssumeRole'
Condition:
StringEquals:
"aws:PrincipalOrgID": !Ref OrganizationId
Path: '/'
Policies:
- PolicyName: Organizationrolecrossaccount
PolicyDocument:
Statement:
- Effect: Allow
Action:
- 'organizations:ListAccountsForParent'
- 'organizations:ListRoots'
- 'organizations:DescribeOrganizationalUnit'
- 'organizations:ListChildren'
Resource: '*'