-
Notifications
You must be signed in to change notification settings - Fork 2
/
deployautotag.py
210 lines (167 loc) · 5.97 KB
/
deployautotag.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# -*- coding: utf-8 -*-
import boto3
import json
import random
from botocore.exceptions import ClientError
from datetime import datetime
CHARS = 'abcdefghijklmnopqrstuvwxyz0123456789'
def get_random_string(length=12, allowed_chars=CHARS):
"""Generate random string of given length with allowed chars."""
return ''.join(random.choice(allowed_chars) for _ in range(length))
def get_account_id():
"""Retrieve principal account id."""
return boto3.client('sts').get_caller_identity().get('Account')
def create_bucket(name, policy=None):
"""Create s3 bucket and attach cloudtrail policy."""
s3 = boto3.client('s3')
s3.create_bucket(Bucket=bucket)
print("S3 bucket %s created." % bucket)
if policy:
s3.put_bucket_policy(
Bucket=bucket,
Policy=json.dumps(bucketPolicy)
)
print("Policy attached to S3 bucket.")
return bucket
def create_role(roleName, policyName, rolePolicy):
"""Create or retrieve role and attach inline policy."""
iam = boto3.client('iam')
policyDoc = {
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Principal" : {"Service" : ["lambda.amazonaws.com"]},
"Action" : ["sts:AssumeRole"]
}
]
}
roles = [role['RoleName'] for role in iam.list_roles()['Roles']]
if roleName in roles:
print('Role %s exists' % roleName)
role = iam.get_role(
RoleName=roleName
)['Role']
else:
role = iam.create_role(
RoleName=roleName,
AssumeRolePolicyDocument=json.dumps(policyDoc)
)['Role']
print('Created role %s' % roleName)
policies = iam.list_role_policies(RoleName=roleName)['PolicyNames']
if policyName not in policies:
iam.put_role_policy(
RoleName=roleName,
PolicyName=policyName,
PolicyDocument=json.dumps(rolePolicy)
)
print('Put role policy %s' % policyName)
return role['Arn']
def enable_cloud_trail(bucket, name):
"""Create and enable cloud trail if it does not exist already."""
cloudtrail = boto3.client('cloudtrail')
try:
cloudtrail.create_trail(
Name=name,
S3BucketName=bucket,
IncludeGlobalServiceEvents=True,
IsMultiRegionTrail=True,
EnableLogFileValidation=True,
)
cloudtrail.start_logging(Name=name)
print("Cloud trail enabled and logging.")
except ClientError as e:
if 'TrailAlreadyExists' in e.message:
print('Trail already exists')
return name
def upload_file(bucket, filename):
"""Upload file to given s3 bucket and overwrite if exists."""
client = boto3.client('s3')
with open(filename, 'rb') as data:
client.upload_fileobj(data, bucket, filename)
print("Cloud formation template with name %s uploaded." % filename)
return filename
def cloud_formation(bucket, template, function, region, role):
"""Deploy cloud formation to given region."""
client = boto3.client('cloudformation', region_name=region)
url = 'https://s3.amazonaws.com/{bucket}/{template}'.format(bucket=bucket,
template=template)
client.create_stack(
StackName='AutoTagResources',
TemplateURL=url,
DisableRollback=False,
Capabilities=['CAPABILITY_IAM'],
Parameters=[
{'ParameterKey': 'LambdaRoleArn', 'Para meterValue': role},
{'ParameterKey': 'LambdaFunction', 'ParameterValue': function},
]
)
print("Successfully configured auto tag in %s" % region)
def get_available_regions(service):
"""List available regions for service."""
s = boto3.session.Session()
return s.get_available_regions(service)
if __name__ == "__main__":
rolePolicy = {
"Version" : "2012-10-17",
"Statement" : [
{
"Sid" : "Stmt1458923097000",
"Effect" : "Allow",
"Action" : ["cloudtrail:LookupEvents"],
"Resource" : ["*"]
},
{
"Sid" : "Stmt1458923121000",
"Effect" : "Allow",
"Action" : [
"ec2:CreateTags",
"ec2:Describe*",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource" : ["*"]
}
]
}
# Create role with inline policy
role = create_role(roleName='LambdaAutoTagRole',
policyName='LambdaAutoTagPolicy',
rolePolicy=rolePolicy)
bucket = '%s-%s' % ('autotag-resources', get_random_string())
bucketPolicy = {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AWSCloudTrailAclCheck20150319",
"Effect": "Allow",
"Principal": {"Service": "cloudtrail.amazonaws.com"},
"Action": "s3:GetBucketAcl",
"Resource": "arn:aws:s3:::%s" % bucket
},
{
"Sid": "AWSCloudTrailWrite20150319",
"Effect": "Allow",
"Principal": {"Service": "cloudtrail.amazonaws.com"},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::%s/AWSLogs/%s/*" % (bucket,
get_account_id()),
"Condition": {"StringEquals": {"s3:x-amz-acl":
"bucket-owner-full-control"}
}
}
]
}
# Create bucket
bucket = create_bucket(name=bucket, policy=bucketPolicy)
# Enable cloud trail for all regions
cloud_trail = enable_cloud_trail(bucket, 'AutoTagResources')
# Upload cloud formation template to s3 bucket
template = upload_file(bucket, 'AutoTag.template')
# Read lambda function
with open('autotag.py', 'rb') as f:
function = f.read()
# Deploy cloud formation to all lambda available regions
for region in get_available_regions('lamdba'):
cloud_formation(bucket, template, function, region, role)