This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_stackset.py
186 lines (153 loc) · 6.16 KB
/
create_stackset.py
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
"""
Creates a stackset in an AWS organization master account and provision stack
instances across any number of organizational units (OUs) in the organization.
Reference:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacksets-orgs-enable-trusted-access.html
Requirements:
* Installed botocore via `pip install botocore` or equivalent
* Python 3.6+
* Active AWS credentials belonging to your organization's master account
* The credentials must include:
* AWSOrganizationsReadOnlyAccess
* AWSCloudFormationFullAccess
"""
# pylint: disable=broad-except
import argparse
import sys
import botocore
import botocore.session
from botocore.exceptions import ClientError
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def fail(message, err=None):
eprint(f'ERROR: {message}')
if err:
eprint(err)
sys.exit(1)
def read_file(path):
with open(path, 'r') as f:
return f.read()
def create_stack_set(cfn_client, stackset_name, description, tmpl, parameters):
params = [dict(ParameterKey=p[0], ParameterValue=p[1]) for p in parameters]
auto_deployment = {'Enabled': True,
'RetainStacksOnAccountRemoval': False}
return cfn_client.create_stack_set(
StackSetName=stackset_name,
Description=description,
TemplateBody=tmpl,
Capabilities=['CAPABILITY_NAMED_IAM'],
Tags=[],
PermissionModel='SERVICE_MANAGED',
AutoDeployment=auto_deployment,
Parameters=params)
def create_stack_instances(cfn_client, stackset_name, region, ous):
print('create instances', ous)
return cfn_client.create_stack_instances(
StackSetName=stackset_name,
Regions=[region],
DeploymentTargets={'OrganizationalUnitIds': ous},
OperationPreferences={
'FailureToleranceCount': 20,
'MaxConcurrentCount': 20,
}
)['OperationId']
def list_children(orgs_client, parent_id, child_type):
children = []
next_token = None
while True:
kwargs = {'ParentId': parent_id, 'ChildType': child_type}
if next_token:
kwargs['NextToken'] = next_token
resp = orgs_client.list_children(**kwargs)
children.extend(resp['Children'])
next_token = resp.get('NextToken')
if not next_token:
return children
def get_root(orgs_client):
roots = orgs_client.list_roots()['Roots']
if len(roots) != 1:
raise ValueError('Expected exactly one root; got %d' % len(roots))
return roots[0]['Id']
def list_organizational_units(orgs_client, parent_id=None):
# If no parent was specified, automatically choose the root OU
if parent_id is None:
parent_id = get_root(orgs_client)
# Find immediate children and then recurse
ous = list_children(orgs_client, parent_id, 'ORGANIZATIONAL_UNIT')
for ou in ous:
ous.extend(list_organizational_units(orgs_client, ou['Id']))
return ous
def validate_ous(requested_ous, existing_ous):
"""
Confirms that the specified organizational unit IDs exist. Also converts
an asterisk to all known OUs. Raises an error on an unknown OU.
"""
if not requested_ous:
raise ValueError('No organizational units specified')
requested = set(requested_ous)
existing = set(existing_ous)
if '*' in requested:
return sorted(list(existing))
missing_ous = requested - existing
if missing_ous:
raise ValueError(f'Unknown OUs: {missing_ous}')
return sorted(list(requested))
def main():
parser = argparse.ArgumentParser(
description='Create a stackset in an AWS master account')
parser.add_argument('-t', '--template',
default='./cloudformation/account.yaml',
help='Cloudformation template for stacks')
parser.add_argument('-n', '--name', required=True, help='Stackset name')
parser.add_argument('-d', '--description', required=True,
help='Stackset description')
parser.add_argument('--ous', default=['*'], nargs='+',
help='Organizational units (default: all OUs)')
parser.add_argument('--parameters', default=[], nargs='+',
help='Stack template parameters in k=v format')
parser.add_argument('-r', '--region', default='us-east-1',
help='AWS region (default: us-east-1)')
args = parser.parse_args()
try:
session = botocore.session.get_session()
sts_client = session.create_client('sts')
org_client = session.create_client('organizations')
cfn_client = session.create_client('cloudformation')
except Exception as exc:
fail('Failed to create AWS clients', exc)
try:
ident = sts_client.get_caller_identity()
except ClientError as exc:
fail('Failed to get AWS identity', exc)
try:
org = org_client.describe_organization()['Organization']
except Exception as exc:
fail('Failed to describe AWS organization', exc)
print(f'Identity ARN: {ident["Arn"]}')
print(f'Identity account: {ident["Account"]}')
print(f'Master account: {org["MasterAccountId"]}')
print(f'Organization ID: {org["Id"]}')
if ident['Account'] != org['MasterAccountId']:
fail('This script must be run with credentials for the '
'organization master account')
try:
template = read_file(args.template)
except Exception as exc:
fail('Failed to read cloudformation template', exc)
try:
params = [p.split('=') for p in args.parameters]
create_stack_set(cfn_client, args.name,
args.description, template, params)
print(f'Stackset {args.name} created')
except Exception as exc:
fail('Failed to create stackset', exc)
if not args.ous:
print('No organizational units specified')
sys.exit(0)
existing_ous = [ou['Id'] for ou in list_organizational_units(org_client)]
ous = validate_ous(args.ous, existing_ous)
create_stack_instances(cfn_client, args.name, args.region, ous)
print(f'Creating stacks instances in {len(ous)} ous')
print(" ".join(ous))
if __name__ == '__main__':
main()