-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount_setup.py
executable file
·267 lines (243 loc) · 9.22 KB
/
account_setup.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
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
import boto3
from botocore.exceptions import ClientError
import json
import os
import sys
import time
import zipfile
#Sets up our boto3 clients
iamClient = boto3.client('iam')
eventsClient = boto3.client('events')
lambdaClient = boto3.client('lambda')
#This is the policy we'll attach to the role
policy_document = {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:*",
"lambda:*"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
#The role service document
role_document= {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
def zip_function():
"""
Zips up our function into a package to be uploaded
"""
output_path = os.getcwd() + '/dist.zip'
folder_path = os.curdir + '/dist'
grab_lambda = os.walk(folder_path)
length = len(folder_path)
try:
zipped_lambda = zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED)
for root, folders, files in grab_lambda:
for folder_name in folders:
absolute_path = os.path.join(root, folder_name)
shortened_path = os.path.join(root[length:], folder_name)
print("Adding '%s' to package." % shortened_path)
zipped_lambda.write(absolute_path, shortened_path)
for file_name in files:
absolute_path = os.path.join(root, file_name)
shortened_path = os.path.join(root[length:], file_name)
print("Adding '%s' to package." % shortened_path)
zipped_lambda.write(absolute_path, shortened_path)
print("lambda packaged successfully.")
return True
except IOError:
print(message)
sys.exit(1)
except OSError:
print(message)
sys.exit(1)
except zipfile.BadZipfile:
print(message)
sys.exit(1)
finally:
zipped_lambda.close()
class LambdaRole(object):
def __init__(self, role_document, policy_document):
self.role_document = role_document
self. policy_document = policy_document
def create_role(self):
"""
Creates a role
"""
print("Creating role")
try:
create_role = iamClient.create_role(
Path='/',
RoleName='cloudwatch-cleanup-role',
AssumeRolePolicyDocument=json.dumps(self.role_document),
Description='IAM role for CloudWatch Cleanup Lambda'
)
except ClientError as error:
print(error.response)
finally:
print("Role created")
return create_role['Role']['Arn'], create_role['Role']['RoleName']
def create_policy(self):
"""
Creates a policy for the above role
"""
print("Creating policy")
try:
create_policy = iamClient.create_policy(
Path='/',
PolicyName='cloudwatch-cleanup-lambda-policy',
PolicyDocument=json.dumps(self.policy_document),
Description='IAM policy for Cloudwatch Cleanup Lambda Role'
)
except ClientError as error:
print(error.response)
finally:
print("Policy created")
return create_policy['Policy']['Arn']
def attach_policy(self, role_name, policy_arn):
"""
Attaches policy to role
"""
print("Attaching role to policy")
try:
attach_policy = iamClient.attach_role_policy(RoleName=role_name, PolicyArn=policy_arn)
except ClientError as error:
print(error.response)
finally:
if attach_policy['ResponseMetadata']['HTTPStatusCode'] == 200:
print("Policy attached successfully")
return True
else:
return False
class CloudWatchEvent(object):
def __init__(self, rule_name, rate):
self.rule_name = rule_name
self.rate = rate
def create_cloudwatch_rule(self):
"""
Creates cloudwatch rule
"""
print("Creating cloudwatch rule %s" % self.rule_name)
try:
create_rule = eventsClient.put_rule(
Name=self.rule_name,
ScheduleExpression="rate(%s)" % self.rate,
State="ENABLED",
Description="Event rule to trigger Cloudwatch Cleanup Lambda every %s" % self.rate
)
except ClientError as error:
print(error.response)
finally:
return create_rule['RuleArn']
def put_rule_policy(self, lambda_name, lambda_arn):
"""
Adds our lambda as the rule policy
"""
print("Adding target to CloudWatch rule")
try:
put_policy = eventsClient.put_targets(
Rule=self.rule_name,
Targets=[{
'Id': lambda_name,
'Arn': lambda_arn
}]
)
except ClientError as error:
print(error)
finally:
if put_policy['FailedEntryCount'] == 0:
print("Event target added successfully")
return True
class Lambda(object):
def __init__(self, lambda_name, role_arn):
self.lambda_name = lambda_name
self.role_arn = role_arn
def create(self):
"""
Creates the lambda
"""
print("Attempting to create lambda")
try:
create_lambda = lambdaClient.create_function(
FunctionName=self.lambda_name,
Runtime='python3.6',
Role=self.role_arn,
Handler='main.handler',
Code={
'ZipFile': open('dist.zip', 'rb').read()
},
Description='CloudWatch Logs Cleanup Lambda',
Timeout=30,
MemorySize=128,
Tags={
"Name": "cloudWatch-logs-manager"
}
)
except ClientError as error:
print(error.response)
finally:
print("Lambda created successfully")
return create_lambda['FunctionArn']
def add_invoke_permission(self, source_arn):
"""
Adds permission for the cloudwatch rule
"""
print("Adding event permission to lambda")
try:
add_perms = lambdaClient.add_permission(
FunctionName=self.lambda_name,
StatementId='cloudwatch-event-trigger-lambda',
Action='lambda:InvokeFunction',
Principal='events.amazonaws.com',
SourceArn=source_arn
)
except ClientError as error:
print(error.response)
finally:
if add_perms['ResponseMetadata']['HTTPStatusCode'] == 200:
print("Permission addedd successfully")
return True
def main():
"""
The below variables can be changed to whatever you see fit.
If you are going to update the run rate it should remain in the format 'X days' or 'X mins'
"""
LAMBDA_NAME = 'cloudwatch-logs-manager-lambda'
RULE_NAME = 'Cloudwatch-Cleanup-Rule'
RUN_RATE = '14 days'
"""
Starts us off by zipping the function
"""
zip_function()
#Creates a role
role_object = LambdaRole(role_document, policy_document)
role_arn, role_name = role_object.create_role()
policy_arn = role_object.create_policy()
attach = role_object.attach_policy(role_name, policy_arn)
print("Sleeping for 10 seconds")
time.sleep(10)
#Create the lambda
lambda_object = Lambda(LAMBDA_NAME, role_arn)
lambda_arn = lambda_object.create()
#Create the rule
event_object = CloudWatchEvent(rule_name=RULE_NAME, rate=RUN_RATE)
event_rule_arn = event_object.create_cloudwatch_rule()
#Policies
add_invoke_perms = lambda_object.add_invoke_permission(event_rule_arn)
add_event_target = event_object.put_rule_policy(LAMBDA_NAME, lambda_arn)
if __name__ == '__main__':
main()