-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrds_autostart.yml
384 lines (345 loc) · 13.2 KB
/
rds_autostart.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#
# Cloud formation to create step fuction that monitors RDS events to auto shutdown servers started due to 7 day max stop rule in AWS.
# Written by: Jim Zucker
# Date: Nov 25, 2020
# References
# Notification RDS-EVENT-0154 The DB instance is being started due to it exceeding the maximum allowed time being stopped.
# Execute step function: https://meetrix.io/blog/aws/07-passing-data-between-lambda-in-aws-step-function.html
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
AWSTemplateFormatVersion: "2010-09-09"
Description: Trap notification RDS-EVENT-0154 "The DB instance is being started due to it exceeding the maximum allowed time being stopped" and stop instance.
################################################################################################
## Lambda to listen to events
##
## This lambda is triggers by the event from RDS
## and his only job is to start the state machine
##
################################################################################################
Resources:
#create a role for lambda
rdsAutoStopFunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: root
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:*
Resource: arn:aws:logs:*:*:*
- Effect: Allow
Action:
- "states:StartExecution"
Resource: !Ref rdsAutoStopStateMachine
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
- 'arn:aws:iam::aws:policy/AmazonSQSFullAccess'
rdsAutoStopLambda:
Type: AWS::Lambda::Function
Properties:
FunctionName: rdsAutoStopLambda
Role: !GetAtt rdsAutoStopFunctionRole.Arn
Timeout: 900
Handler: index.handler
Runtime: python3.9
Code:
ZipFile: |
import json
import boto3
import traceback
import os
def handler(event, context):
try:
print(json.dumps(event))
#RDS-EVENT-0154: DB instance is being started due to it exceeding the maximum allowed time being stopped.
auto_started_event_id="RDS-EVENT-0154"
#this is for debuggins
debug_instance="rds-stop-test"
state_machine = os.environ['RDS_STATE_MACHINE_ARN']
message = json.loads(event['Records'][0]['Sns']['Message'])
rds_name = message['Source ID']
event_id = message['Event ID'].split('#')[1]
event_message = message['Event Message']
print("rds_name={} / event_id={} / event_message={}".format(rds_name,event_id,event_message) )
#for debugging
if rds_name == debug_instance :
print(json.dumps(event))
print(state_machine)
#
# trigger if we are not stopping and its and auto_started_event or debug_instance
#
if event_id == auto_started_event_id or rds_name == debug_instance :
client = boto3.client('stepfunctions')
response = client.start_execution(
stateMachineArn=state_machine,
input=json.dumps(event)
)
return
except Exception as e:
traceback.print_exc()
Description: Trigger a Step Function to run.
Environment:
Variables:
RDS_STATE_MACHINE_ARN: !Ref rdsAutoStopStateMachine
rdsAutoStopLambdaPermission:
Type: 'AWS::Lambda::Permission'
Properties:
Action: 'lambda:InvokeFunction'
FunctionName: !Ref rdsAutoStopLambda
Principal: sns.amazonaws.com
SourceArn: !Ref rdsNotifcationTopic
# if you dont define this it will get created but will have a indefinite retention
# so we define it to ensure lgos roll
rdsAutoStopLambdaLogGroup:
Type: 'AWS::Logs::LogGroup'
Properties:
LogGroupName: !Sub "/aws/lambda/${rdsAutoStopLambda}"
RetentionInDays: '7'
################################################################################################
################################################################################################
## SNS Topic & event Listner
## Setup SNS Topic to listen to RDS events an trigger rdsAutoStopLambdaLogGroup:
################################################################################################
#Topic for Lambda to listen to
rdsNotifcationTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: rdsNotifcationTopic
TopicName: rdsNotifcationTopic
Subscription:
- Endpoint: !GetAtt rdsAutoStopLambda.Arn
Protocol: lambda
#ask RDS to publish all notifications
rdsNotifyEventsSubscription:
Type: AWS::RDS::EventSubscription
Properties:
Enabled: true
EventCategories:
- notification
SnsTopicArn: !Ref rdsNotifcationTopic
SourceType: db-instance
# create Queue for retry if we time out to make sure the instance stops
# This is because the server is often in 'not availalbe state' because it was just started
rdsAutoStopRetrySQSQueue:
Type: 'AWS::SQS::Queue'
rdsAutoStopLambdsSQSPolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues: [!Ref 'rdsAutoStopRetrySQSQueue']
PolicyDocument:
Version: '2008-10-17'
Id: PublicationPolicy1
Statement:
- Sid: Allow-Lambda-SendMessage
Effect: Allow
Principal: "*"
Action: "sqs:*"
Resource: "*"
Condition:
ArnEquals:
aws:SourceArn: !Ref 'rdsAutoStopLambda'
################################################################################################
################################################################################################
## Worker lambda
## This Lamdba does all the work, when called from the State Machine
################################################################################################
rdsAutoStopWorkerFunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: root
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:*
Resource: arn:aws:logs:*:*:*
- Effect: Allow
Action:
- rds:DescribeDbInstances
- rds:StopDbInstance
Resource: "*"
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
- 'arn:aws:iam::aws:policy/AmazonSQSFullAccess'
rdsAutoStopLambdaWorker:
Type: AWS::Lambda::Function
Properties:
FunctionName: rdsAutoStopLambdaWorker
Role: !GetAtt rdsAutoStopWorkerFunctionRole.Arn
Timeout: 30
Handler: index.handler
Runtime: python3.9
Code:
ZipFile: |
import json
import traceback
import time
import boto3
def handler(event, context):
#RDS-EVENT-0154: DB instance is being started due to it exceeding the maximum allowed time being stopped.
auto_started_event_id="RDS-EVENT-0154"
debug_instance="rds-stop-test"
try:
message = json.loads(event['Records'][0]['Sns']['Message'])
rds_name = message['Source ID']
event_id = message['Event ID'].split('#')[1]
event_message = message['Event Message']
client = boto3.client('rds')
response = client.describe_db_instances(DBInstanceIdentifier=rds_name)
status = response['DBInstances'][0]['DBInstanceStatus']
print("rds_name={} / event_id={} / event_message={} / status={}".format(rds_name,event_id, event_message, status) )
# if it is stopped we are done
if status != "stopped" :
client.stop_db_instance(DBInstanceIdentifier=rds_name)
return
except Exception as e:
traceback.print_exc()
raise e
Description: Stop RDS if it is autostarted.
# if you dont define this it will get created but will have a indefinite retention
# so we define it to ensure lgos roll
rdsAutoStopLambdaWorkderLogGroup:
Type: 'AWS::Logs::LogGroup'
Properties:
LogGroupName: !Sub "/aws/lambda/${rdsAutoStopLambdaWorker}"
RetentionInDays: '7'
################################################################################################
################################################################################################
## Step Function State Machine
## Because stoping an RDS has to wait until its running use a state machine
## to keep calling lambda until it can be stopped. (We need the step function because labmda
## could time out)
################################################################################################
rdsAutoStopStatesExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: !Sub "states.${AWS::Region}.amazonaws.com"
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: StepFunctionExecRole
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- lambda:InvokeFunction
- lambda:ListFunctions
Resource: !GetAtt [ rdsAutoStopLambdaWorker, Arn ]
- Effect: Allow
Action:
- logs:CreateLogDelivery
- logs:GetLogDelivery
- logs:UpdateLogDelivery
- logs:DeleteLogDelivery
- logs:ListLogDeliveries
- logs:PutResourcePolicy
- logs:DescribeResourcePolicies
- logs:DescribeLogGroups
Resource: "*"
rdsAutoStopStateMachineLogGroup:
Type: 'AWS::Logs::LogGroup'
Properties:
LogGroupName: "/aws/lambda/rdsAutoStopStateMachine"
RetentionInDays: '7'
#
# keep trying 7 times or about 2 hours
# until the worker lambda succeeds in its mission
#
#
rdsAutoStopStateMachine:
Type: "AWS::StepFunctions::StateMachine"
Properties:
LoggingConfiguration:
Destinations:
- CloudWatchLogsLogGroup:
LogGroupArn: !GetAtt rdsAutoStopStateMachineLogGroup.Arn
IncludeExecutionData: True
Level: ALL
DefinitionString:
!Sub
- |-
{
"Comment": "Step function to stop RDS and wait until its done",
"StartAt": "rdsStop",
"States": {
"rdsStop": {
"Type": "Task",
"Resource": "${lambdaArn}",
"Retry": [
{
"ErrorEquals": [
"States.TaskFailed", "Lambda.ServiceException", "Lambda.AWSLambdaException", "Lambda.SdkClientException"
],
"IntervalSeconds": 60,
"MaxAttempts": 7,
"BackoffRate": 2
}
],
"End": true
}
}
}
- {lambdaArn: !GetAtt [ rdsAutoStopLambdaWorker, Arn ]}
RoleArn: !GetAtt [ rdsAutoStopStatesExecutionRole, Arn ]
################################################################################################
Outputs:
rdsNotifcationTopicArn:
Value: !Ref rdsNotifcationTopic
rdsNotifyEventsSubscription:
Value: !Ref rdsNotifyEventsSubscription
rdsAutoStopLambdaArn:
Value: !GetAtt rdsAutoStopLambda.Arn
rdsAutoStopFunctionRoleArn:
Value: !GetAtt rdsAutoStopFunctionRole.Arn
rdsAutoStopRetrySQSQueueArn:
Value: !GetAtt rdsAutoStopRetrySQSQueue.Arn
rdsAutoStopLambdsSQSPolicy:
Value: !Ref rdsAutoStopLambdsSQSPolicy
rdsAutoStopStateMachine:
Value: !Ref rdsAutoStopStateMachine
rdsAutoStopStatesExecutionRole:
Value: !Ref rdsAutoStopStatesExecutionRole