This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
forked from awslabs/rds-snapshot-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
at_snapshots_tool_rds_source.yaml
679 lines (678 loc) · 19.2 KB
/
at_snapshots_tool_rds_source.yaml
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
---
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
CodeBucket:
Type: String
Default: DEFAULT_BUCKET
Description: Name of the bucket that contains the lambda functions to deploy.
Leave the default value to download the code from the AWS Managed buckets
InstanceNamePattern:
Type: String
Default: ALL_INSTANCES
Description: Python regex for matching cluster identifiers to backup. Use "ALL_INSTANCES"
to back up every RDS instance in the region.
KmsKeyId:
Type: String
Description: The ARN of the non-default KMS key to be used in encrytption of an RDS instance.
BackupInterval:
Type: Number
Default: '24'
Description: Interval for backups in hours. Default is 24
DestinationAccount:
Type: Number
Default: '000000000000'
Description: Destination account with no dashes.
ShareSnapshots:
Type: String
Default: 'TRUE'
AllowedValues:
- 'TRUE'
- 'FALSE'
BackupSchedule:
Type: String
Default: 0 1 * * ? *
Description: 'Backup schedule in Cloudwatch Event cron format. Needs to run at
least once for every Interval. The default value runs once every at 1AM UTC.
More information: http://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html'
RetentionDays:
Type: Number
Default: '7'
Description: Number of days to keep snapshots in retention before deleting them
LogLevel:
Type: String
Default: ERROR
Description: Log level for Lambda functions (DEBUG, INFO, WARN, ERROR, CRITICAL
are valid values).
LambdaCWLogRetention:
Type: Number
Default: '7'
Description: Number of days to retain logs from the lambda functions in CloudWatch
Logs
SourceRegionOverride:
Type: String
Default: 'NO'
Description: Set to the region where your RDS instances run, only if such region
does not support Step Functions. Leave as NO otherwise
DeleteOldSnapshots:
Type: String
Default: 'TRUE'
Description: Set to TRUE to enable deletion of snapshot based on RetentionDays.
Set to FALSE to disable
AllowedValues:
- 'TRUE'
- 'FALSE'
TaggedInstance:
Type: String
Default: 'FALSE'
Description: Set to TRUE to filter instances that have tag share_snapshot set
to True. Set to FALSE to disable
AllowedValues:
- 'TRUE'
- 'FALSE'
Conditions:
Share:
Fn::Equals:
- Ref: ShareSnapshots
- 'TRUE'
DeleteOld:
Fn::Equals:
- Ref: DeleteOldSnapshots
- 'TRUE'
DefaultBucket:
Fn::Equals:
- Ref: CodeBucket
- DEFAULT_BUCKET
Mappings:
Buckets:
us-east-1:
Bucket: snapshots-tool-rds-us-east-1
us-west-2:
Bucket: snapshots-tool-rds-us-west-2
us-east-2:
Bucket: snapshots-tool-rds-us-east-2
ap-southeast-2:
Bucket: snapshots-tool-rds-ap-southeast-2
ap-northeast-1:
Bucket: snapshots-tool-rds-ap-northeast-1
eu-west-1:
Bucket: snapshots-tool-rds-eu-west-1
eu-central-1:
Bucket: snapshots-tool-rds-eu-central-1
ca-central-1:
Bucket: snapshots-tool-rds-ca-central-1
eu-west-2:
Bucket: snapshots-tool-rds-eu-west-2
us-west-1:
Bucket: snapshots-tool-rds-us-west-1
ap-northeast-2:
Bucket: snapshots-tool-rds-ap-northeast-2
ap-southeast-1:
Bucket: snapshots-tool-rds-ap-southeast-1-real
Resources:
topicBackupsFailed:
Type: AWS::SNS::Topic
Properties:
DisplayName: backups_failed_rds
topicShareFailed:
Type: AWS::SNS::Topic
Properties:
DisplayName: share_failed_rds
topicDeleteOldFailed:
Type: AWS::SNS::Topic
Properties:
DisplayName: delete_old_failed_rds
snspolicySnapshotsRDS:
Type: AWS::SNS::TopicPolicy
Properties:
Topics:
- Ref: topicBackupsFailed
- Ref: topicShareFailed
- Ref: topicDeleteOldFailed
PolicyDocument:
Version: '2008-10-17'
Id: __default_policy_ID
Statement:
- Sid: __default_statement_ID
Effect: Allow
Principal:
AWS: "*"
Action:
- SNS:GetTopicAttributes
- SNS:SetTopicAttributes
- SNS:AddPermission
- SNS:RemovePermission
- SNS:DeleteTopic
- SNS:Subscribe
- SNS:ListSubscriptionsByTopic
- SNS:Publish
- SNS:Receive
Resource: "*"
Condition:
StringEquals:
AWS:SourceOwner:
Ref: AWS::AccountId
alarmcwBackupsFailed:
Type: AWS::CloudWatch::Alarm
Properties:
ActionsEnabled: 'true'
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: '1'
MetricName: ExecutionsFailed
Namespace: AWS/States
Period: '300'
Statistic: Sum
Threshold: '1.0'
AlarmActions:
- Ref: topicBackupsFailed
Dimensions:
- Name: StateMachineArn
Value:
Ref: stateMachineTakeSnapshotsRDS
alarmcwShareFailed:
Condition: Share
Type: AWS::CloudWatch::Alarm
Properties:
ActionsEnabled: 'true'
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: '2'
MetricName: ExecutionsFailed
Namespace: AWS/States
Period: '3600'
Statistic: Sum
Threshold: '2.0'
AlarmActions:
- Ref: topicShareFailed
Dimensions:
- Name: StateMachineArn
Value:
Ref: statemachineShareSnapshotsRDS
alarmcwDeleteOldFailed:
Condition: DeleteOld
Type: AWS::CloudWatch::Alarm
Properties:
ActionsEnabled: 'true'
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: '2'
MetricName: ExecutionsFailed
Namespace: AWS/States
Period: '3600'
Statistic: Sum
Threshold: '2.0'
AlarmActions:
- Ref: topicDeleteOldFailed
Dimensions:
- Name: StateMachineArn
Value:
Ref: statemachineDeleteOldSnapshotsRDS
iamroleSnapshotsRDS:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: inline_policy_snapshots_rds_cw_logs
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: arn:aws:logs:*:*:*
- PolicyName: inline_policy_snapshots_rds
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- rds:CreateDBSnapshot
- rds:DeleteDBSnapshot
- rds:CopyDBSnapshot
- rds:DescribeDBInstances
- rds:DescribeDBSnapshots
- rds:ModifyDBSnapshotAttribute
- rds:DescribeDBSnapshotAttributes
- rds:ListTagsForResource
- rds:RemoveTagsFromResource
Resource: "*"
- PolicyName: inline_policy_snapshot_rds_kms_access
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: AllowUseOfTheKey
Effect: Allow
Action:
- kms:Encrypt
- kms:Decrypt
- kms:ReEncrypt*
- kms:GenerateDataKey*
- kms:DescribeKey
Resource:
- "*"
- Sid: AllowAttachmentOfPersistentResources
Effect: Allow
Action:
- kms:CreateGrant
- kms:ListGrants
- kms:RevokeGrant
Resource:
- "*"
lambdaTakeSnapshotsRDS:
Type: AWS::Lambda::Function
Properties:
Code:
S3Bucket:
Fn::If:
- DefaultBucket
- Fn::FindInMap:
- Buckets
- Ref: AWS::Region
- Bucket
- Ref: CodeBucket
S3Key: take_snapshots_rds.zip
MemorySize: 512
Description: 'This functions triggers snapshots creation for RDS instances.
It checks for existing snapshots following the pattern and interval specified
in the environment variables with the following format: <dbinstancename>-YYYY-MM-DD-HH-MM'
Environment:
Variables:
INTERVAL:
Ref: BackupInterval
PATTERN:
Ref: InstanceNamePattern
LOG_LEVEL:
Ref: LogLevel
REGION_OVERRIDE:
Ref: SourceRegionOverride
TAGGEDINSTANCE:
Ref: TaggedInstance
KMS_KEY_ID:
Ref: KmsKeyId
Role:
Fn::GetAtt:
- iamroleSnapshotsRDS
- Arn
Runtime: python3.6
Handler: lambda_function.lambda_handler
Timeout: 300
lambdaShareSnapshotsRDS:
Type: AWS::Lambda::Function
Condition: Share
Properties:
Code:
S3Bucket:
Fn::If:
- DefaultBucket
- Fn::FindInMap:
- Buckets
- Ref: AWS::Region
- Bucket
- Ref: CodeBucket
S3Key: share_snapshots_rds.zip
MemorySize: 512
Description: 'This function shares snapshots created by the take_snapshots_rds
function with DEST_ACCOUNT specified in the environment variables. '
Environment:
Variables:
DEST_ACCOUNT:
Ref: DestinationAccount
LOG_LEVEL:
Ref: LogLevel
PATTERN:
Ref: InstanceNamePattern
REGION_OVERRIDE:
Ref: SourceRegionOverride
KMS_KEY_ID:
Ref: KmsKeyId
Role:
Fn::GetAtt:
- iamroleSnapshotsRDS
- Arn
Runtime: python3.6
Handler: lambda_function.lambda_handler
Timeout: 300
lambdaDeleteOldSnapshotsRDS:
Type: AWS::Lambda::Function
Condition: DeleteOld
Properties:
Code:
S3Bucket:
Fn::If:
- DefaultBucket
- Fn::FindInMap:
- Buckets
- Ref: AWS::Region
- Bucket
- Ref: CodeBucket
S3Key: delete_old_snapshots_rds.zip
MemorySize: 512
Description: 'This function deletes snapshots created by the take_snapshots_rds
function. '
Environment:
Variables:
RETENTION_DAYS:
Ref: RetentionDays
PATTERN:
Ref: InstanceNamePattern
LOG_LEVEL:
Ref: LogLevel
REGION_OVERRIDE:
Ref: SourceRegionOverride
Role:
Fn::GetAtt:
- iamroleSnapshotsRDS
- Arn
Runtime: python3.6
Handler: lambda_function.lambda_handler
Timeout: 300
iamroleStateExecution:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
Fn::Join:
- ''
- - states.
- Ref: AWS::Region
- ".amazonaws.com"
Action: sts:AssumeRole
Policies:
- PolicyName: inline_policy_snapshots_rds
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- lambda:InvokeFunction
Resource: "*"
stateMachineTakeSnapshotsRDS:
Type: AWS::StepFunctions::StateMachine
Properties:
DefinitionString:
Fn::Join:
- ''
- - Fn::Join:
- "\n"
- - ' {"Comment":"Triggers snapshot backup for RDS instances",'
- ' "StartAt":"TakeSnapshots",'
- ' "States":{'
- ' "TakeSnapshots":{'
- ' "Type":"Task",'
- ' "Resource": '
- "\""
- Fn::GetAtt:
- lambdaTakeSnapshotsRDS
- Arn
- |-
"
,
- Fn::Join:
- "\n"
- - ' "Retry":['
- " {"
- ' "ErrorEquals":[ '
- ' "SnapshotToolException"'
- " ],"
- ' "IntervalSeconds":300,'
- ' "MaxAttempts":20,'
- ' "BackoffRate":1'
- " },"
- " {"
- ' "ErrorEquals":[ '
- ' "States.ALL"], '
- ' "IntervalSeconds": 30,'
- ' "MaxAttempts": 20,'
- ' "BackoffRate": 1'
- " }"
- " ],"
- ' "End": true '
- " }"
- " }}"
RoleArn:
Fn::GetAtt:
- iamroleStateExecution
- Arn
statemachineShareSnapshotsRDS:
Type: AWS::StepFunctions::StateMachine
Condition: Share
Properties:
DefinitionString:
Fn::Join:
- ''
- - Fn::Join:
- "\n"
- - ' {"Comment":"Shares snapshots with DEST_ACCOUNT",'
- ' "StartAt":"ShareSnapshots",'
- ' "States":{'
- ' "ShareSnapshots":{'
- ' "Type":"Task",'
- ' "Resource": '
- "\""
- Fn::GetAtt:
- lambdaShareSnapshotsRDS
- Arn
- |-
"
,
- Fn::Join:
- "\n"
- - ' "Retry":['
- " {"
- ' "ErrorEquals":[ '
- ' "SnapshotToolException"'
- " ],"
- ' "IntervalSeconds":300,'
- ' "MaxAttempts":3,'
- ' "BackoffRate":1'
- " },"
- " {"
- ' "ErrorEquals":[ '
- ' "States.ALL"], '
- ' "IntervalSeconds": 30,'
- ' "MaxAttempts": 20,'
- ' "BackoffRate": 1'
- " }"
- " ],"
- ' "End": true '
- " }"
- " }}"
RoleArn:
Fn::GetAtt:
- iamroleStateExecution
- Arn
statemachineDeleteOldSnapshotsRDS:
Type: AWS::StepFunctions::StateMachine
Condition: DeleteOld
Properties:
DefinitionString:
Fn::Join:
- ''
- - Fn::Join:
- "\n"
- - ' {"Comment":"DeleteOld management for RDS snapshots",'
- ' "StartAt":"DeleteOld",'
- ' "States":{'
- ' "DeleteOld":{'
- ' "Type":"Task",'
- ' "Resource": '
- "\""
- Fn::GetAtt:
- lambdaDeleteOldSnapshotsRDS
- Arn
- |-
"
,
- Fn::Join:
- "\n"
- - ' "Retry":['
- " {"
- ' "ErrorEquals":[ '
- ' "SnapshotToolException"'
- " ],"
- ' "IntervalSeconds":300,'
- ' "MaxAttempts":7,'
- ' "BackoffRate":1'
- " },"
- " {"
- ' "ErrorEquals":[ '
- ' "States.ALL"], '
- ' "IntervalSeconds": 30,'
- ' "MaxAttempts": 20,'
- ' "BackoffRate": 1'
- " }"
- " ],"
- ' "End": true '
- " }"
- " }}"
RoleArn:
Fn::GetAtt:
- iamroleStateExecution
- Arn
iamroleStepInvocation:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: events.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: inline_policy_state_invocation
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- states:StartExecution
Resource: "*"
cwEventBackupRDS:
Type: AWS::Events::Rule
Properties:
Description: Triggers the TakeSnapshotsRDS state machine
ScheduleExpression:
Fn::Join:
- ''
- - cron(
- Ref: BackupSchedule
- ")"
State: ENABLED
Targets:
- Arn:
Ref: stateMachineTakeSnapshotsRDS
Id: Target1
RoleArn:
Fn::GetAtt:
- iamroleStepInvocation
- Arn
cwEventShareSnapshotsRDS:
Type: AWS::Events::Rule
Condition: Share
Properties:
Description: Triggers the ShareSnapshotsRDS state machine
ScheduleExpression:
Fn::Join:
- ''
- - cron(
- "/10 * * * ? *"
- ")"
State: ENABLED
Targets:
- Arn:
Ref: statemachineShareSnapshotsRDS
Id: Target1
RoleArn:
Fn::GetAtt:
- iamroleStepInvocation
- Arn
cwEventDeleteOldSnapshotsRDS:
Type: AWS::Events::Rule
Condition: DeleteOld
Properties:
Description: Triggers the DeleteOldSnapshotsRDS state machine
ScheduleExpression:
Fn::Join:
- ''
- - cron(
- 0 /1 * * ? *
- ")"
State: ENABLED
Targets:
- Arn:
Ref: statemachineDeleteOldSnapshotsRDS
Id: Target1
RoleArn:
Fn::GetAtt:
- iamroleStepInvocation
- Arn
cwloggrouplambdaTakeSnapshotsRDS:
Type: AWS::Logs::LogGroup
Description: Log group for the lambdaTakeSnapshotsRDS function's logs
DependsOn: lambdaTakeSnapshotsRDS
Properties:
RetentionInDays:
Ref: LambdaCWLogRetention
LogGroupName:
Fn::Sub:
- "/aws/lambda/${func}"
- func:
Ref: lambdaTakeSnapshotsRDS
cwloggrouplambdaShareSnapshotsRDS:
Condition: Share
Type: AWS::Logs::LogGroup
Description: Log group for the lambdaShareSnapshotsRDS function's logs
DependsOn: lambdaShareSnapshotsRDS
Properties:
RetentionInDays:
Ref: LambdaCWLogRetention
LogGroupName:
Fn::Sub:
- "/aws/lambda/${func}"
- func:
Ref: lambdaShareSnapshotsRDS
cwloggrouplambdaDeleteOldSnapshotsRDS:
Type: AWS::Logs::LogGroup
Description: Log group for the lambdaDeleteOldSnapshotsRDS function's logs
DependsOn: lambdaDeleteOldSnapshotsRDS
Properties:
RetentionInDays:
Ref: LambdaCWLogRetention
LogGroupName:
Fn::Sub:
- "/aws/lambda/${func}"
- func:
Ref: lambdaDeleteOldSnapshotsRDS
Outputs:
BackupFailedTopic:
Description: Subscribe to this topic to receive alerts of failed backups
Value:
Ref: topicBackupsFailed
ShareFailedTopic:
Condition: Share
Description: Subscribe to this topic to receive alerts of failures at sharing
snapshots with destination account
Value:
Ref: topicShareFailed
DeleteOldFailedTopic:
Condition: DeleteOld
Description: Subscribe to this topic to receive alerts of failures at deleting
old snapshots
Value:
Ref: topicDeleteOldFailed
SourceURL:
Description: For more information and documentation, see the source repository
at GitHub.
Value: https://github.com/awslabs/rds-snapshot-tool
Description: Snapshots Tool for RDS cross-region and cross-account (source account
stack)