-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathAurora-Postgres-DB-Cluster.yml
963 lines (889 loc) · 33.2 KB
/
Aurora-Postgres-DB-Cluster.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
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
AWSTemplateFormatVersion: '2010-09-09'
Description: 'CloudFormation Template to create Aurora Postgresql Cluster DB Instance'
###############################################################################
# Parameters
###############################################################################
Parameters:
ParentVPCStack:
Description: 'Provide Stack name of parent VPC stack based on VPC-3AZs yaml template. Refer Cloudformation dashboard in AWS Console to get this.'
Type: String
MinLength: '1'
MaxLength: '128'
AllowedPattern: '^[a-zA-Z]+[0-9a-zA-Z\-]*$'
ParentSSHBastionStack:
Description: 'Provide Stack name of parent Amazon Linux bastion host stack based on VPC-SSH-Bastion yaml template. Refer Cloudformation dashboard in AWS Console to get this.'
Type: String
MinLength: '1'
MaxLength: '128'
AllowedPattern: '^[a-zA-Z]+[0-9a-zA-Z\-]*$'
DBName:
Description: Database Name
Type: String
MinLength: '1'
MaxLength: '64'
AllowedPattern: "^[a-zA-Z]+[0-9a-zA-Z_]*$"
ConstraintDescription: Must start with a letter. Only numbers, letters, and _ accepted. max length 64 characters
DBPort:
Description: TCP/IP Port for the Database Instance
Type: Number
Default: 5432
ConstraintDescription: 'Must be in the range [1150-65535]'
MinValue: 1150
MaxValue: 65535
DBUsername:
Description: Database master username
Type: String
MinLength: '1'
MaxLength: '16'
AllowedPattern: "^[a-zA-Z]+[0-9a-zA-Z_]*$"
ConstraintDescription: Must start with a letter. Only numbers, letters, and _ accepted. max length 16 characters
DBEngineVersion:
Description: Select Database Engine Version
Type: String
Default: 13.7
AllowedValues:
- 11.16
- 12.11
- 13.7
- 14.3
DBInstanceClass:
Default: db.r6g.large
Description: Database Instance Class
Type: String
AllowedValues:
- db.t4g.medium
- db.t4g.large
- db.r5.large
- db.r5.xlarge
- db.r5.2xlarge
- db.r5.4xlarge
- db.r5.8xlarge
- db.r5.12xlarge
- db.r5.16xlarge
- db.r5.24xlarge
- db.r6g.large
- db.r6g.xlarge
- db.r6g.2xlarge
- db.r6g.4xlarge
- db.r6g.8xlarge
- db.r6g.12xlarge
- db.r6g.16xlarge
- db.x2g.large
- db.x2g.xlarge
- db.x2g.2xlarge
- db.x2g.4xlarge
- db.x2g.8xlarge
- db.x2g.12xlarge
- db.x2g.16xlarge
DBSnapshotName:
Description: Optional. DB Snapshot ID to restore database. Leave this blank if you are not restoring from a snapshot.
Type: String
Default: ""
LambdaBootStrapS3Bucket:
Description: Optional. Specify S3 bucket name for e.g. apgbootstrapscripts where Lambda DB Bootstrap Python script is stored.
Default: ''
Type: String
LambdaBootStrapS3Key:
Description: Optional. Specify S3 key for e.g. lambda/dbbootstrap.zip where Lambda DB Bootstrap Python script is stored.
Default: ''
Type: String
###########################################################################
# Mandatory tags that will be added to all resources that support tags
###########################################################################
EnvironmentStage:
Type: String
Description: The environment tag is used to designate the Environment Stage of the associated AWS resource.
AllowedValues:
- dev
- test
- pre-prod
- prod
Default: dev
Application:
Type: String
Description: The Application tag is used to designate the application of the associated AWS resource. In this capacity application does not refer to an installed software component, but rather the overall business application that the resource supports.
AllowedPattern: "^[a-zA-Z]+[a-zA-Z ]+[a-zA-Z]+$"
ConstraintDescription: provide a valid application name containing only letters and spaces
ApplicationVersion:
Type: String
Description: The ApplicationVersion tag is used to designate the specific version of the application. Format should be in the Pattern - "#.#.#"
Default: '1.0.0'
AllowedPattern: '^[a-zA-Z0-9\.\-]+$'
ConstraintDescription: provide a valid application version
ProjectCostCenter:
Type: String
Description: The ProjectCostCenter tag is used to designate the cost center associated with the project of the given AWS resource.
AllowedPattern: "^[a-zA-Z0-9]+$"
ConstraintDescription: provide a valid cost center
ServiceOwnersEmailContact:
Type: String
Description: The ServiceOwnersEmailContact tag is used to designate business owner(s) email address associated with the given AWS resource for sending outage or maintenance notifications
AllowedPattern: '^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$'
ConstraintDescription: provide a valid email address.
NotificationList:
Type: String
Description: The Email notification list is used to configure a SNS topic for sending cloudwatch alarm and RDS Event notifications
AllowedPattern: '^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$'
ConstraintDescription: provide a valid email address.
Confidentiality:
Type: String
Description: The Confidentiality tag is used to designate the confidentiality classification of the data that is associated with the resource.
AllowedValues:
- public
- private
- confidential
- pii/phi
Compliance:
Type: String
Description: The Compliance tag is used to specify the Compliance level for the AWS resource.
AllowedValues:
- hipaa
- sox
- fips
- other
- none
###############################################################################
# Parameter groups
###############################################################################
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
-
Label:
default: Environment
Parameters:
- EnvironmentStage
-
Label:
default: DB Parameters
Parameters:
- DBName
- DBPort
- DBUsername
- DBInstanceClass
- DBEngineVersion
- DBSnapshotName
- NotificationList
- LambdaBootStrapS3Bucket
- LambdaBootStrapS3Key
-
Label:
default: Networking
Parameters:
- ParentVPCStack
- ParentSSHBastionStack
-
Label:
default: Mandatory Tags
Parameters:
- Application
- ApplicationVersion
- ProjectCostCenter
- ServiceOwnersEmailContact
- Confidentiality
- Compliance
###############################################################################
# Mappings
###############################################################################
Mappings:
DBFamilyMap:
"11.16":
"family": "aurora-postgresql11"
"12.11":
"family": "aurora-postgresql12"
"13.7":
"family": "aurora-postgresql13"
"14.3":
"family": "aurora-postgresql14"
###############################################################################
# Conditions
###############################################################################
Conditions:
IsUseDBSnapshot: !Not [!Equals [!Ref DBSnapshotName, ""]]
IsNotUseDBSnapshot: !Not [Condition: IsUseDBSnapshot]
IsProd: !Equals [!Ref EnvironmentStage, 'prod']
IsReplica: !Or [!Equals [!Ref EnvironmentStage, 'pre-prod'], Condition: IsProd]
DoDBBootStrap: !And
- !Not [!Equals [!Ref LambdaBootStrapS3Bucket, '']]
- !Not [!Equals [!Ref LambdaBootStrapS3Key, '']]
- !Not [Condition: IsUseDBSnapshot]
DoEnableIAM: !Not [!Equals [!Ref DBEngineVersion, '9.6.8']]
###############################################################################
# Resources
###############################################################################
Resources:
MonitoringIAMRole:
Type: AWS::IAM::Role
Condition: IsProd
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "monitoring.rds.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole
DBBootStrapLambdaRole:
Type: AWS::IAM::Role
Condition: DoDBBootStrap
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole'
Policies:
-
PolicyName: "secretaccess"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action: "secretsmanager:GetSecretValue"
Resource: !Ref AuroraMasterSecret
DBSNSTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint: !Ref NotificationList
Protocol: email
DBSubnetGroup:
Type: 'AWS::RDS::DBSubnetGroup'
Properties:
DBSubnetGroupDescription: !Ref 'AWS::StackName'
SubnetIds: !Split [',', {'Fn::ImportValue': !Sub '${ParentVPCStack}-SubnetsPrivate'}]
ClusterSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: !Ref 'AWS::StackName'
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: !Ref DBPort
ToPort: !Ref DBPort
SourceSecurityGroupId: {'Fn::ImportValue': !Sub '${ParentSSHBastionStack}-BastionSecurityGroupID'}
Description: 'Access to Bastion Host Security Group'
- IpProtocol: tcp
FromPort: !Ref DBPort
ToPort: !Ref DBPort
SourceSecurityGroupId: {'Fn::ImportValue': !Sub '${ParentVPCStack}-SecretRotationLambdaSecurityGroup'}
Description: 'Access to Lambda Security Group'
VpcId: {'Fn::ImportValue': !Sub '${ParentVPCStack}-VPC'}
Tags:
- Key: Name
Value: !Sub '${AWS::StackName}-AuroraClusterSecurityGroup'
ClusterSecurityGroupIngress:
Type: 'AWS::EC2::SecurityGroupIngress'
Properties:
GroupId: !GetAtt 'ClusterSecurityGroup.GroupId'
IpProtocol: -1
SourceSecurityGroupId: !Ref ClusterSecurityGroup
Description: 'Self Reference'
RDSDBClusterParameterGroup:
Type: AWS::RDS::DBClusterParameterGroup
Properties:
Description: !Join [ "- ", [ "Aurora PG Cluster Parameter Group for Cloudformation Stack ", !Ref DBName ] ]
Family: !FindInMap [DBFamilyMap, !Ref DBEngineVersion, "family"]
Parameters:
rds.force_ssl: 1
DBParamGroup:
Type: AWS::RDS::DBParameterGroup
Properties:
Description: !Join [ "- ", [ "Aurora PG Database Instance Parameter Group for Cloudformation Stack ", !Ref DBName ] ]
Family: !FindInMap [DBFamilyMap, !Ref DBEngineVersion, "family"]
Parameters:
shared_preload_libraries: auto_explain,pg_stat_statements,pg_hint_plan,pgaudit
log_statement: "ddl"
log_connections: 1
log_disconnections: 1
log_lock_waits: 1
log_min_duration_statement: 5000
auto_explain.log_min_duration: 5000
auto_explain.log_verbose: 1
log_rotation_age: 1440
log_rotation_size: 102400
rds.log_retention_period: 10080
random_page_cost: 1
track_activity_query_size: 16384
idle_in_transaction_session_timeout: 7200000
statement_timeout: 7200000
search_path: '"$user",public'
AuroraKMSCMK:
Type: 'AWS::KMS::Key'
DeletionPolicy: Retain
Properties:
KeyPolicy:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
AWS: !Sub 'arn:aws:iam::${AWS::AccountId}:root'
Action: 'kms:*'
Resource: '*'
- Effect: Allow
Principal:
AWS: '*'
Action:
- 'kms:Encrypt'
- 'kms:Decrypt'
- 'kms:ReEncrypt*'
- 'kms:GenerateDataKey*'
- 'kms:CreateGrant'
- 'kms:ListGrants'
- 'kms:DescribeKey'
Resource: '*'
Condition:
StringEquals:
'kms:CallerAccount': !Ref 'AWS::AccountId'
'kms:ViaService': !Sub 'rds.${AWS::Region}.amazonaws.com'
AuroraKMSCMKAlias:
Type: 'AWS::KMS::Alias'
DeletionPolicy: Retain
DependsOn: AuroraDBCluster
Properties:
AliasName: !Sub 'alias/${AuroraDBCluster}'
TargetKeyId: !Ref AuroraKMSCMK
AuroraMasterSecret:
Condition: IsNotUseDBSnapshot
Type: AWS::SecretsManager::Secret
Properties:
Name: !Join ['/', [!Ref EnvironmentStage, 'aurora-pg', !Ref 'AWS::StackName']]
Description: !Join ['', ['Aurora PostgreSQL Master User Secret ', 'for CloudFormation Stack ', !Ref 'AWS::StackName']]
Tags:
-
Key: EnvironmentStage
Value: !Ref EnvironmentStage
-
Key: DatabaseEngine
Value: 'Aurora PostgreSQL'
-
Key: StackID
Value: !Ref 'AWS::StackId'
GenerateSecretString:
SecretStringTemplate: !Join ['', ['{"username": "', !Ref DBUsername, '"}']]
GenerateStringKey: "password"
ExcludeCharacters: '"@/\'
PasswordLength: 16
SecretAuroraClusterAttachment:
Condition: IsNotUseDBSnapshot
Type: AWS::SecretsManager::SecretTargetAttachment
Properties:
SecretId: !Ref AuroraMasterSecret
TargetId: !Ref AuroraDBCluster
TargetType: AWS::RDS::DBCluster
AuroraSecretResourcePolicy:
Condition: IsNotUseDBSnapshot
Type: AWS::SecretsManager::ResourcePolicy
Properties:
SecretId: !Ref AuroraMasterSecret
ResourcePolicy:
Version: "2012-10-17"
Statement:
-
Effect: "Deny"
Principal:
AWS: !Sub "arn:aws:iam::${AWS::AccountId}:root"
Action: "secretsmanager:DeleteSecret"
Resource: "*"
CreateSecretRotationLambdaRole:
Condition: IsNotUseDBSnapshot
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole'
- 'arn:aws:iam::aws:policy/SecretsManagerReadWrite'
- 'arn:aws:iam::aws:policy/IAMFullAccess'
Policies:
-
PolicyName: "AdditionalPermissions"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "cloudformation:DescribeStackResources"
- "cloudformation:DeleteStack"
Resource: !Sub 'arn:aws:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/serverlessrepo-${AWS::StackName}-SecretRotationLambdaStack/*'
-
Effect: "Allow"
Action:
- "lambda:DeleteFunction"
- "lambda:GetFunctionConfiguration"
- "lambda:RemovePermission"
- "lambda:TagResource"
Resource: !Sub 'arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:SecretsManager-SecretRotationFn-${AWS::StackName}'
-
Effect: "Allow"
Action:
- "iam:DetachRolePolicy"
Resource: !Sub 'arn:aws:iam::${AWS::AccountId}:role/serverlessrepo*'
CreateSecretRotationLambdaFn:
Condition: IsNotUseDBSnapshot
Type: AWS::Lambda::Function
DependsOn:
- CreateSecretRotationLambdaRole
Properties:
Code:
ZipFile: |
import cfnresponse
import boto3
from botocore.vendored import requests
def lambda_handler(event, context):
slrepoclient = boto3.client('serverlessrepo')
cfclient = boto3.client('cloudformation')
lambdaclient = boto3.client('lambda')
SecretsManagerEndpoint = event['ResourceProperties']['SecretsManagerEndpoint']
SecretRotationLambdaFnName = event['ResourceProperties']['SecretRotationLambdaFnName']
SecretRotationLambdaStackName = event['ResourceProperties']['SecretRotationLambdaStackName']
SubnetIds = event['ResourceProperties']['SubnetIds']
SecretRotationLambdaSG = event['ResourceProperties']['SecretRotationLambdaSG']
responseData = {}
try:
if event['RequestType'] == 'Create':
serverlessreporesponse = slrepoclient.create_cloud_formation_change_set(ApplicationId='arn:aws:serverlessrepo:us-east-1:297356227824:applications/SecretsManagerRDSPostgreSQLRotationSingleUser',
Capabilities=['CAPABILITY_IAM', 'CAPABILITY_RESOURCE_POLICY'],
ParameterOverrides=[
{
'Name': 'endpoint',
'Value': SecretsManagerEndpoint
},
{
'Name': 'functionName',
'Value': SecretRotationLambdaFnName
},
],
StackName=SecretRotationLambdaStackName)
waiter = cfclient.get_waiter('change_set_create_complete')
waiter.wait(ChangeSetName=serverlessreporesponse['ChangeSetId'],WaiterConfig={'Delay': 10,'MaxAttempts': 60})
cloudformationresponse = cfclient.execute_change_set(ChangeSetName=serverlessreporesponse['ChangeSetId'])
waiter = cfclient.get_waiter('stack_create_complete')
waiter.wait(StackName=serverlessreporesponse['StackId'],WaiterConfig={'Delay': 10,'MaxAttempts': 60})
lambdaresponse = lambdaclient.add_permission(FunctionName=SecretRotationLambdaFnName,StatementId='SecretsManagerAccess',Action='lambda:InvokeFunction',Principal='secretsmanager.amazonaws.com')
lambdaresponse = lambdaclient.update_function_configuration(FunctionName=SecretRotationLambdaFnName,VpcConfig={'SubnetIds': SubnetIds,'SecurityGroupIds': SecretRotationLambdaSG})
responseData['Data'] = "SUCCESS: Secret Rotation Lambda created successfully."
responseData['SecretRotationLambdaARN'] = lambdaclient.get_function(FunctionName=SecretRotationLambdaFnName)['Configuration']['FunctionArn']
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData, "None")
elif event['RequestType'] == 'Delete':
SecretRotationLambdaStackName = 'serverlessrepo-' + SecretRotationLambdaStackName
response = cfclient.delete_stack(StackName=SecretRotationLambdaStackName)
waiter = cfclient.get_waiter('stack_delete_complete')
waiter.wait(StackName='string',WaiterConfig={'Delay': 10,'MaxAttempts': 60})
responseData['Data'] = "SUCCESS: Stack delete complete."
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData, "None")
else:
responseData['Data'] = "{} is unsupported stack operation for this lambda function.".format(event['RequestType'])
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData, "None")
except Exception as e:
print(e)
responseData['Data'] = "ERROR: Exception encountered!"
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData, "None")
Description: >-
Create Secret Rotation Lambda function using AWS Serverless Application Repository with template provided by AWS Secrets Manager
Handler: index.lambda_handler
MemorySize: 128
Role: !GetAtt CreateSecretRotationLambdaRole.Arn
Runtime: python3.9
Timeout: 120
VpcConfig:
SecurityGroupIds: !Split [',', {'Fn::ImportValue': !Sub '${ParentVPCStack}-SecretRotationLambdaSecurityGroup'}]
SubnetIds: !Split [',', {'Fn::ImportValue': !Sub '${ParentVPCStack}-SubnetsPrivate'}]
CreateSecretRotationLambdaFnTrigger:
Condition: IsNotUseDBSnapshot
Type: Custom::LambdaAPGSecretsManager
Version: "1.0"
Properties:
ServiceToken: !GetAtt 'CreateSecretRotationLambdaFn.Arn'
SecretsManagerEndpoint: !Sub 'https://secretsmanager.${AWS::Region}.amazonaws.com'
SecretRotationLambdaFnName: !Sub 'SecretsManager-SecretRotationFn-${AWS::StackName}'
SecretRotationLambdaStackName: !Sub '${AWS::StackName}-SecretRotationLambdaStack'
SubnetIds: !Split [',', {'Fn::ImportValue': !Sub '${ParentVPCStack}-SubnetsPrivate'}]
SecretRotationLambdaSG: !Split [',', {'Fn::ImportValue': !Sub '${ParentVPCStack}-SecretRotationLambdaSecurityGroup'}]
AuroraSecretRotationSchedule:
Condition: IsNotUseDBSnapshot
Type: AWS::SecretsManager::RotationSchedule
DependsOn:
- SecretAuroraClusterAttachment
- AuroraDBFirstInstance
Properties:
SecretId: !Ref AuroraMasterSecret
RotationLambdaARN: !GetAtt CreateSecretRotationLambdaFnTrigger.SecretRotationLambdaARN
RotationRules:
AutomaticallyAfterDays: 30
AuroraDBCluster:
Type: AWS::RDS::DBCluster
DeletionPolicy: Snapshot
UpdateReplacePolicy: Snapshot
Properties:
Engine: aurora-postgresql
EngineVersion: !Ref DBEngineVersion
DatabaseName: !If [IsUseDBSnapshot, !Ref "AWS::NoValue", !Ref DBName]
Port: !Ref DBPort
MasterUsername:
!If [IsUseDBSnapshot, !Ref "AWS::NoValue", !Join ['', ['{{resolve:secretsmanager:', !Ref AuroraMasterSecret, ':SecretString:username}}' ]]]
MasterUserPassword:
!If [IsUseDBSnapshot, !Ref "AWS::NoValue", !Join ['', ['{{resolve:secretsmanager:', !Ref AuroraMasterSecret, ':SecretString:password}}' ]]]
DBSubnetGroupName: !Ref DBSubnetGroup
VpcSecurityGroupIds:
- !Ref ClusterSecurityGroup
BackupRetentionPeriod: !If [IsProd, 35, 7]
DBClusterParameterGroupName: !Ref RDSDBClusterParameterGroup
SnapshotIdentifier: !If [IsUseDBSnapshot, !Ref DBSnapshotName, !Ref "AWS::NoValue"]
StorageEncrypted: !If [IsUseDBSnapshot, !Ref "AWS::NoValue", true]
KmsKeyId: !If [IsNotUseDBSnapshot, !Ref AuroraKMSCMK, !Ref 'AWS::NoValue']
EnableIAMDatabaseAuthentication: !If [DoEnableIAM, true, !Ref "AWS::NoValue"]
Tags:
-
Key: EnvironmentStage
Value: !Ref EnvironmentStage
-
Key: Application
Value: !Ref Application
-
Key: ApplicationVersion
Value: !Ref ApplicationVersion
-
Key: ProjectCostCenter
Value: !Ref ProjectCostCenter
-
Key: ServiceOwnersEmailContact
Value: !Ref ServiceOwnersEmailContact
-
Key: Confidentiality
Value: !Ref Confidentiality
-
Key: Compliance
Value: !Ref Compliance
AuroraDBFirstInstance:
Type: AWS::RDS::DBInstance
Properties:
CopyTagsToSnapshot: true
DBInstanceClass:
Ref: DBInstanceClass
DBClusterIdentifier: !Ref AuroraDBCluster
Engine: aurora-postgresql
EngineVersion: !Ref DBEngineVersion
DBParameterGroupName:
Ref: DBParamGroup
MonitoringInterval: !If [IsProd, 1, 0]
MonitoringRoleArn: !If [IsProd, !GetAtt MonitoringIAMRole.Arn, !Ref "AWS::NoValue"]
AutoMinorVersionUpgrade: !If [IsProd, 'false', 'true']
DBSubnetGroupName: !Ref DBSubnetGroup
PubliclyAccessible: false
EnablePerformanceInsights: true
PerformanceInsightsKMSKeyId: !Ref AuroraKMSCMK
PerformanceInsightsRetentionPeriod: !If [IsProd, 731, 7]
Tags:
-
Key: EnvironmentStage
Value: !Ref EnvironmentStage
-
Key: Application
Value: !Ref Application
-
Key: ApplicationVersion
Value: !Ref ApplicationVersion
-
Key: ProjectCostCenter
Value: !Ref ProjectCostCenter
-
Key: ServiceOwnersEmailContact
Value: !Ref ServiceOwnersEmailContact
-
Key: Confidentiality
Value: !Ref Confidentiality
-
Key: Compliance
Value: !Ref Compliance
AuroraDBSecondInstance:
Condition: IsReplica
Type: AWS::RDS::DBInstance
DependsOn:
- AuroraDBFirstInstance
Properties:
CopyTagsToSnapshot: true
DBInstanceClass:
Ref: DBInstanceClass
DBClusterIdentifier: !Ref AuroraDBCluster
Engine: aurora-postgresql
EngineVersion: !Ref DBEngineVersion
DBParameterGroupName:
Ref: DBParamGroup
MonitoringInterval: !If [IsProd, 1, 0]
MonitoringRoleArn: !If [IsProd, !GetAtt MonitoringIAMRole.Arn, !Ref "AWS::NoValue"]
AutoMinorVersionUpgrade: !If [IsProd, 'false', 'true']
DBSubnetGroupName: !Ref DBSubnetGroup
PubliclyAccessible: false
EnablePerformanceInsights: true
PerformanceInsightsKMSKeyId: !Ref AuroraKMSCMK
PerformanceInsightsRetentionPeriod: !If [IsProd, 731, 7]
Tags:
-
Key: EnvironmentStage
Value: !Ref EnvironmentStage
-
Key: Application
Value: !Ref Application
-
Key: ApplicationVersion
Value: !Ref ApplicationVersion
-
Key: ProjectCostCenter
Value: !Ref ProjectCostCenter
-
Key: ServiceOwnersEmailContact
Value: !Ref ServiceOwnersEmailContact
-
Key: Confidentiality
Value: !Ref Confidentiality
-
Key: Compliance
Value: !Ref Compliance
CPUUtilizationAlarm1:
Type: "AWS::CloudWatch::Alarm"
Properties:
ActionsEnabled: true
AlarmActions:
- Ref: DBSNSTopic
AlarmDescription: 'CPU_Utilization'
Dimensions:
- Name: DBInstanceIdentifier
Value:
Ref: AuroraDBFirstInstance
MetricName: CPUUtilization
Statistic: Maximum
Namespace: 'AWS/RDS'
Threshold: '80'
Unit: Percent
ComparisonOperator: 'GreaterThanOrEqualToThreshold'
Period: '60'
EvaluationPeriods: '5'
TreatMissingData: 'notBreaching'
CPUUtilizationAlarm2:
Condition: IsReplica
Type: "AWS::CloudWatch::Alarm"
Properties:
ActionsEnabled: true
AlarmActions:
- Ref: DBSNSTopic
AlarmDescription: 'CPU_Utilization'
Dimensions:
- Name: DBInstanceIdentifier
Value:
Ref: AuroraDBSecondInstance
MetricName: CPUUtilization
Statistic: Maximum
Namespace: 'AWS/RDS'
Threshold: '80'
Unit: Percent
ComparisonOperator: 'GreaterThanOrEqualToThreshold'
Period: '60'
EvaluationPeriods: '5'
TreatMissingData: 'notBreaching'
MaxUsedTxIDsAlarm1:
Type: "AWS::CloudWatch::Alarm"
Properties:
ActionsEnabled: true
AlarmActions:
- Ref: DBSNSTopic
AlarmDescription: 'Maximum Used Transaction IDs'
Dimensions:
- Name: DBInstanceIdentifier
Value:
Ref: AuroraDBFirstInstance
MetricName: 'MaximumUsedTransactionIDs'
Statistic: Average
Namespace: 'AWS/RDS'
Threshold: '600000000'
Unit: Count
ComparisonOperator: 'GreaterThanOrEqualToThreshold'
Period: '60'
EvaluationPeriods: '5'
TreatMissingData: 'notBreaching'
MaxUsedTxIDsAlarm2:
Condition: IsReplica
Type: "AWS::CloudWatch::Alarm"
Properties:
ActionsEnabled: true
AlarmActions:
- Ref: DBSNSTopic
AlarmDescription: 'Maximum Used Transaction IDs'
Dimensions:
- Name: DBInstanceIdentifier
Value:
Ref: AuroraDBSecondInstance
MetricName: 'MaximumUsedTransactionIDs'
Statistic: Average
Namespace: 'AWS/RDS'
Threshold: '600000000'
Unit: Count
ComparisonOperator: 'GreaterThanOrEqualToThreshold'
Period: '60'
EvaluationPeriods: '5'
TreatMissingData: 'notBreaching'
FreeLocalStorageAlarm1:
Type: "AWS::CloudWatch::Alarm"
Properties:
ActionsEnabled: true
AlarmActions:
- Ref: DBSNSTopic
AlarmDescription: 'Free Local Storage'
Dimensions:
- Name: DBInstanceIdentifier
Value:
Ref: AuroraDBFirstInstance
MetricName: 'FreeLocalStorage'
Statistic: Average
Namespace: 'AWS/RDS'
Threshold: '5368709120'
Unit: Bytes
ComparisonOperator: 'LessThanOrEqualToThreshold'
Period: '60'
EvaluationPeriods: '5'
TreatMissingData: 'notBreaching'
FreeLocalStorageAlarm2:
Condition: IsReplica
Type: "AWS::CloudWatch::Alarm"
Properties:
ActionsEnabled: true
AlarmActions:
- Ref: DBSNSTopic
AlarmDescription: 'Free Local Storage'
Dimensions:
- Name: DBInstanceIdentifier
Value:
Ref: AuroraDBSecondInstance
MetricName: 'FreeLocalStorage'
Statistic: Average
Namespace: 'AWS/RDS'
Threshold: '5368709120'
Unit: Bytes
ComparisonOperator: 'LessThanOrEqualToThreshold'
Period: '60'
EvaluationPeriods: '5'
TreatMissingData: 'notBreaching'
DatabaseClusterEventSubscription:
Type: 'AWS::RDS::EventSubscription'
Properties:
EventCategories:
- failover
- failure
- notification
SnsTopicArn: !Ref DBSNSTopic
SourceIds: [!Ref AuroraDBCluster]
SourceType: 'db-cluster'
DatabaseInstanceEventSubscription:
Type: 'AWS::RDS::EventSubscription'
Properties:
EventCategories:
- availability
- configuration change
- deletion
- failover
- failure
- maintenance
- notification
- recovery
SnsTopicArn: !Ref DBSNSTopic
SourceIds:
- !Ref AuroraDBFirstInstance
- !If [IsReplica, !Ref AuroraDBSecondInstance, !Ref "AWS::NoValue"]
SourceType: 'db-instance'
DBParameterGroupEventSubscription:
Type: 'AWS::RDS::EventSubscription'
Properties:
EventCategories:
- configuration change
SnsTopicArn: !Ref DBSNSTopic
SourceIds: [!Ref DBParamGroup]
SourceType: 'db-parameter-group'
DBBootStrapLambdaFn:
Condition: DoDBBootStrap
Type: AWS::Lambda::Function
DependsOn:
- DBBootStrapLambdaRole
Properties:
Code:
S3Bucket: !Ref LambdaBootStrapS3Bucket
S3Key: !Ref LambdaBootStrapS3Key
Description: >-
BootStrap newly Created Aurora PostgreSQL Database
Handler: dbbootstrap.handler
MemorySize: 128
Role: !GetAtt DBBootStrapLambdaRole.Arn
Runtime: python3.9
Timeout: 60
VpcConfig:
SecurityGroupIds: !Split [',', {'Fn::ImportValue': !Sub '${ParentVPCStack}-SecretRotationLambdaSecurityGroup'}]
SubnetIds: !Split [',', {'Fn::ImportValue': !Sub '${ParentVPCStack}-SubnetsPrivate'}]
Environment:
Variables:
DBHost: !GetAtt 'AuroraDBCluster.Endpoint.Address'
DBPort: !GetAtt 'AuroraDBCluster.Endpoint.Port'
DBUser: !Ref DBUsername
DBName: !Ref DBName
Secret_ARN: !Ref AuroraMasterSecret
Region_Name: !Ref "AWS::Region"
DBBootStrapLambdaFnTrigger:
Condition: DoDBBootStrap
Type: Custom::LambdaAPGBootStrap
DependsOn:
- AuroraDBFirstInstance
- AuroraSecretRotationSchedule
Version: "1.0"
Properties:
ServiceToken: !GetAtt 'DBBootStrapLambdaFn.Arn'
###############################################################################
# Outputs
###############################################################################
Outputs:
ClusterEndpoint:
Description: 'Aurora Cluster/Writer Endpoint'
Value: !GetAtt 'AuroraDBCluster.Endpoint.Address'
ReaderEndpoint:
Description: 'Aurora Reader Endpoint'
Value: !GetAtt 'AuroraDBCluster.ReadEndpoint.Address'
Port:
Description: 'Aurora Endpoint Port'
Value: !GetAtt 'AuroraDBCluster.Endpoint.Port'
DBUsername:
Description: 'Database master username'
Value: !Ref DBUsername
DBName:
Description: 'Database Name'
Value: !Ref DBName
PSQLCommandLine:
Description: PSQL Command Line
Value: !Join
- ''
- - 'psql --host='
- !GetAtt 'AuroraDBCluster.Endpoint.Address'
- ' --port='
- !GetAtt 'AuroraDBCluster.Endpoint.Port'
- ' --username='
- !Ref DBUsername
- ' --dbname='
- !Ref DBName